免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: liuzhuan23
打印 上一主题 下一主题

问一个关于fork后的文件操作的IO问题 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2012-10-19 19:53 |只看该作者
每个进程都有自己的文件表项,因此每个进程都有自己对文件的偏移量。lseek和write不是一个原子操作。因此b进程可能替换了a进程写入的内容。要达到楼主想要的效果有两种方式:
1.不加记录锁,打开文件的时候用O_APPEND状态标记,这样内核在写的时候会把当前位移量移动到文件的末尾处。
if ((fd = open(fn, O_WRONLY | O_CREAT|O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
                perror("open:");
                exit(-1);
        }
2.把记录锁加到循环外面
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #define CNT 100

  8. void write_file(int fd, char data, size_t len);

  9. int main(int argc, char *argv[])
  10. {
  11.         char fn[]="flock-demo";
  12.         int fd;
  13.         pid_t pid;

  14.         if (argc > 1)
  15.                 strcpy(fn,argv[1]);

  16.         if ((fd = open(fn, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  17.                 perror("open:");
  18.                 exit(-1);
  19.         }

  20.         for (int i = 0; i < CNT; i++) {
  21.                 if ((pid = fork()) < 0) {
  22.                         perror("fork:");
  23.                         exit(-1);
  24.                 } else {
  25.                         if (pid == 0) {
  26.                                 /* child process */
  27.                                 write_file(fd, i, sizeof(i));
  28.                                 exit(0);
  29.                         }
  30.                 }
  31.         }

  32.         return 0;
  33. }

  34. void write_file(int fd, char data, size_t len)
  35. {
  36.         struct flock fl;

  37.         fl.l_whence = SEEK_SET;
  38.         fl.l_start  = 0;
  39.         fl.l_len    = 0;
  40.         fl.l_type = F_WRLCK;
  41.         if (fcntl(fd, F_SETLKW, &fl) < 0) {
  42.                 perror("fcntl:");
  43.                 exit(-1);
  44.         }*
  45.         for (int i = 0; i < CNT; i++) {
  46.        

  47.                 ssize_t wlen = write(fd, &data, len);
  48.                 if (wlen < 0) {
  49.                         perror("write:");
  50.                         exit(-1);
  51.                 }
  52.                 else {
  53.                         if ((size_t)wlen < len) {
  54.                                 fprintf(stderr, "Other write error in process %d\n", getpid());
  55.                                 exit(-1);
  56.                         }
  57.                 }

  58.                
  59.                 }
  60.         fl.l_type = F_UNLCK;
  61.         if (fcntl(fd, F_SETLKW, &fl) < 0) {
  62.                 perror("fcntl:");
  63.                 exit(-1);
  64.         }
  65. }
复制代码

论坛徽章:
0
22 [报告]
发表于 2012-10-20 22:40 |只看该作者
经测试  木有问题

论坛徽章:
0
23 [报告]
发表于 2012-10-21 19:39 |只看该作者
大概的看了下,首先你这个程序加父进程才是100个,而且循环也就999,不是1000, 而且你的记录锁只加锁了一次
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP