Zach Saw
Zach Saw

Reputation: 4378

Multiple reader/writer on FIFO (named pipe)

I've created a named pipe using mkfifo and opened a reader and writer on it. I then went on to open a second reader/writer on the same fifo but open returns ENXIO instead.

std::string n = "/tmp/test";
int err;
err = mkfifo(n.c_str(), 0666);
if (err == -1)
    return NULL;

int pipefd[2];
pipefd[0] = open(n.c_str(), O_RDONLY | O_NONBLOCK);
pipefd[1] = open(n.c_str(), O_WRONLY | O_NONBLOCK);
open(n.c_str(), O_RDONLY | O_NONBLOCK); // fails - ENXIO
open(n.c_str(), O_WDONLY | O_NONBLOCK); // fails - ENXIO

Is there any specific flags I need to set when opening the pipe to allow it to be opened multiple times? I've read the docs but found no explanation as to why the above should fail (I've only tested it on Cygwin so far). As described here, it is perfectly valid to open multiple readers/writers on a fifo.

I'll be using this to replicate WinAPI's OpenEvent functionality which needs to be used by a separate project.

EDIT: Tested this on Debian and Ubuntu - both complies to POSIX and allow multiple writers (thus the above code does not exhibit any problems). Cygwin's implementation is broken (i.e. does not conform to POSIX).

Upvotes: 0

Views: 6582

Answers (2)

nj-ath
nj-ath

Reputation: 3146

Try with O_NONBLOCK removed

And also the fourth time you call open function it must be with O_WRONLY.

Upvotes: 1

clover
clover

Reputation: 5170

There is only one reader process and writer process possible for pipes. In POSIX pipes are unidirectional.

Use socket files instead. It's full-duplex and allows multiple processes communication.

Upvotes: 2

Related Questions