码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-15 00:39:47      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

 

创建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.
复制代码

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

标签:

原文地址:http://www.cnblogs.com/YL450606975/p/4427238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!