问题
In my code I create a fifo named "my_fifo", if I open it in O_WRONLY | O_NONBLOCK
mode, open() returns a -1 and an error number of "No such device or address", on the other hand, if I open the fifo in O_RDONLY | O_NONBLOCK
mode, it works perfectly. Why is this happening? Is there something I'm doing wrong?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *fifoname = "my_fifo";
mkfifo(fifoname, 0666);
int fd;
if ((fd = open(fifoname, O_WRONLY | O_NONBLOCK)) == -1)
{
perror("open pipe");
exit(EXIT_FAILURE);
}
close(fd);
exit(EXIT_SUCCESS);
}
回答1:
Check out the Linux fifo man page:
A process can open a FIFO in nonblocking mode. In this case, opening for read-only will succeed even if no-one has opened on the write side yet, opening for write-only will fail with
ENXIO
(no such device or address) unless the other end has already been opened.
If you want non-blocking mode, you need to make sure the reader opens the fifo before the writer.
来源:https://stackoverflow.com/questions/24972911/when-i-try-to-open-a-fifo-o-wronly-i-get-a-no-such-device-or-address-error