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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With