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.
复制代码
posted @ 2015-04-14 22:57  450606975  阅读(266)  评论(0编辑  收藏  举报