unix 网络编程卷2 第43页 管道:open竟然会阻塞?

 

创建fifo管道的时候,open会阻塞?
还是我的代码有问题?

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #Include <sys/stat.h>
  7. #define FIFO1 "/tmp/fifo.1"
  8. #define FIFO2 "/tmp/fifo.2"
  9. #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  10. int main()
  11. {
  12.     int rfd, wfd;
  13.     if ((mkfifo(FIFO1, FILE_MODE) < 0) && errno != EEXIST)
  14.     {//error
  15.         exit(0);
  16.     }
  17.     if ((mkfifo(FIFO2, FILE_MODE) < 0) && errno != EEXIST)
  18.     {//error
  19.         exit(0);
  20.     }
  21. fprintf(stderr, "before open\n");
  22.     rfd = open(FIFO1, O_RDONLY, 0);
  23.     wfd = open(FIFO2, O_WRONLY, 0);
  24. fprintf(stderr, "after open\n");
  25. //到这里出现问题,after open一直没有打印出来。
  26. }

 

 

 

man 3 open.一般3的解释较2的解释详细.

  1. O_NONBLOCK
  2.       When opening a FIFO with O_RDONLY or O_WRONLY set:
  3.       * If  O_NONBLOCK  is set, an open() for reading-only shall return without delay. An open() for writing-only shall return an error if no process currently has the file open for reading.
  4.        * If O_NONBLOCK is clear, an open() for reading-only shall block the calling thread until  a  thread  opens  the  file for writing. An open() for writing-only shall block the calling thread until a thread opens the  file for reading.
复制代码

转载于:https://www.cnblogs.com/YL450606975/p/4427238.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值