Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I try to open a fifo O_WRONLY I get a "No such device or address" error

Tags:

c

linux

fifo

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);
}
like image 822
presa Avatar asked Jul 26 '14 16:07

presa


1 Answers

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.

like image 156
Mat Avatar answered Oct 14 '22 05:10

Mat