0

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).

Zach Saw
  • 4,308
  • 3
  • 33
  • 49
  • 2
    Unless you're doing something really weird, the way to do this is normally to open two pipes, one in each direction. It makes no sense to read *and* write the same pipe from the same process. With two pipes, process 1 reads first and writes to second, and process 2 reads the second and writes to the first. – Amadan Jan 22 '13 at 02:20
  • @Amadan I'm simulating Set/ResetEvent WaitForSingle/MultipleObjects using pipes, so it needs to be just the one pipe. – Zach Saw Jan 22 '13 at 02:46

2 Answers2

2

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.

clover
  • 4,910
  • 1
  • 18
  • 26
  • I don't think that's feasible. There's no such thing as named sockets. – Zach Saw Jan 22 '13 at 02:59
  • I mean Local Domain Sockets, often called “UNIX Domain Sockets” – clover Jan 22 '13 at 03:11
  • 1
    "There is only one reader process and writer process possible for pipes." This is wrong! Multiple readers and writers are perfectly possible for pipes. – Zach Saw Jan 22 '13 at 03:21
  • I just tested pipe's behavior under Debian and it works as POSIX described. Cygwin's implementation is broken. – Zach Saw Jan 22 '13 at 03:22
  • For named pipes it's impossible to determine sender's process by the architecture. Maybe some realizations allows multiple readers, but it is not guaranteed and only on NON_BLOCKING modes. – clover Jan 22 '13 at 03:28
  • POSIX specifically says that FIFO allows multiple readers and multiple writers. If it does not work as specified, then it is *not* POSIX compliant. – Zach Saw Jan 22 '13 at 03:30
  • Classic 100% working pipe in any -nix system is (posix): one required reader, one required writer, unidirectional. – clover Jan 22 '13 at 03:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23121/discussion-between-clover-and-zach-saw) – clover Jan 22 '13 at 03:40
  • how can we multiplex named pipes? multiplexed sockets seems standard, but why can't the same feature be implemented with named pipes? don't understand it – Alexander Mills Apr 20 '18 at 09:24
1

Try with O_NONBLOCK removed

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

nj-ath
  • 3,028
  • 2
  • 25
  • 41