父子进程间文件描述符的共享

在习题10.6,测试父子进程同步,同时也测试了进程间文件共享。

代码如下:

  1 #include "apue.h"
  2 #include <fcntl.h>
  3
  4 static void charatatime(char *);
  5 #define BUFSIZE 1000
  6 int
  7 main(void)
  8 {
  9     pid_t   pid;
 10     int fd, n;
 11     int currVal;
 12     char buf[BUFSIZE];
 13
 14     if ((fd = open("filett.temp",O_RDWR|O_CREAT|O_TRUNC, FILE_MODE)) < 0)
 15     {
 16         err_sys("create error");
 17     }
 18     buf[0] = '0';
 19     buf[1] = 0;
 20     n = write(fd, buf, 2);
 21     if (-1 == lseek(fd, 0, SEEK_SET))
 22             err_sys("seek error");
 23     TELL_WAIT();
 24
 25     if ((pid = fork()) < 0) {
 26         err_sys("fork error");
 27     } else if (pid == 0) {
 28         for (;;)
 29         {
 30             WAIT_PARENT();      /* parent goes first */
 31             n = read(fd, buf,BUFSIZE);
 32             //printf("%d, in son\n", n);
 33             if (n < 0)
 34                 err_sys("read error");
 35             currVal = atoi(buf);
 36             currVal++;
 37             lseek(fd, 0, SEEK_SET);
 38             sprintf(buf, "%d", currVal);
 39             write(fd, buf, strlen(buf)+1);
 40             lseek(fd, 0, SEEK_SET);
 41             printf("son proc add currVaL to %d\n", currVal);
 42             sleep(1);
 43             TELL_PARENT(getppid());
 44         }
 45     } else {
46         for(;;)
 47         {
 48             n = read(fd, buf,BUFSIZE);
 49             lseek(fd, 0, SEEK_SET);
 50
 51             //printf("%d, in parent fd is %d\n", n, fd);
 52             if (n < 0)
 53                 err_sys("read error");
 54             currVal = atoi(buf);
 55             currVal++;
 56             sprintf(buf, "%d", currVal);
 57             write(fd, buf, strlen(buf)+1);
 58             lseek(fd, 0, SEEK_SET);
 59             printf("parent proc add currVaL to %d\n", currVal);
 60             sleep(1);
 61             TELL_CHILD(pid);
 62             WAIT_CHILD();

 63         }
 64     }
 65     exit(0);
 66 }
在父进程中创建的文件,其fd和文件偏移量在子进程中也是有效的,从而使得父进程和子进程交替的向一个文件的同一位置写入数字,表现为数字交替上升的情况。

执行情况如下:

liyifeng@liyifeng-ThinkCentre:~/unixcode$ a.out
parent proc add currVaL to 1
son proc add currVaL to 2
parent proc add currVaL to 3
son proc add currVaL to 4
parent proc add currVaL to 5
son proc add currVaL to 6
parent proc add currVaL to 7
son proc add currVaL to 8
parent proc add currVaL to 9
son proc add currVaL to 10
parent proc add currVaL to 11
son proc add currVaL to 12
parent proc add currVaL to 13
son proc add currVaL to 14
parent proc add currVaL to 15
son proc add currVaL to 16
parent proc add currVaL to 17
son proc add currVaL to 18
parent proc add currVaL to 19
son proc add currVaL to 20
parent proc add currVaL to 21
son proc add currVaL to 22


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值