创建fifo管道的时候,open会阻塞?
还是我的代码有问题?
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/types.h>
- #Include <sys/stat.h>
- #define FIFO1 "/tmp/fifo.1"
- #define FIFO2 "/tmp/fifo.2"
- #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
- int main()
- {
- int rfd, wfd;
- if ((mkfifo(FIFO1, FILE_MODE) < 0) && errno != EEXIST)
- {//error
- exit(0);
- }
- if ((mkfifo(FIFO2, FILE_MODE) < 0) && errno != EEXIST)
- {//error
- exit(0);
- }
- fprintf(stderr, "before open\n");
- rfd = open(FIFO1, O_RDONLY, 0);
- wfd = open(FIFO2, O_WRONLY, 0);
- fprintf(stderr, "after open\n");
- //到这里出现问题,after open一直没有打印出来。
- }
man 3 open.一般3的解释较2的解释详细.
- O_NONBLOCK
- When opening a FIFO with O_RDONLY or O_WRONLY set:
- * 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.
- * 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.
复制代码
时间: 2024-12-26 03:21:58