Talk:IPC via named pipe

From Rosetta Code
Revision as of 04:21, 2 October 2011 by rosettacode>Kernigh (OpenBSD blocks the entire process.)

Tricky to open "out"

This is tricky! The “in” reader side is normal asynchronous IO (the pipe becomes readable when there's data available) but the “out” writer side is very awkward because you can't open the writer side of a FIFO if there is no reader there. You've either got to use a blocking open() — which might or might not work with a threaded solution — or deal with the failure to open() it in non-blocking mode by polling regularly. Tricky stuff! –Donal Fellows 09:38, 30 September 2011 (UTC)

Actually I don't think O_WRONLY|O_NONBLOCK on a fifo will ever succeed on a POSIX system.(unless there is already a reader) On Linux, you can O_RDWR open a fifo, and it will not block (because the program itself is the reader then), but then you can't select it for write readiness because it always returns immediately with success. Luckily here one thread blocking on open will not hold up the entire process, so it's relatively easy to deal with. --Ledrug 11:44, 30 September 2011 (UTC)

OpenBSD blocks the entire process

<lang ruby>$ irb -rthread irb(main):001:0> q = Queue.new; Thread.new {q << open("out", "w")} => #<Thread:0x0000020da6a630 run> irb(main):002:0> ^C irb(main):002:0> q.empty? => true</lang>

With OpenBSD, I observed that open("out", O_WRONLY) does block the entire process. This is a bug in the thread library, because an IO system call must block only the current thread; it is wrong to also block other threads. This irb session comes from Ruby MRI 1.9.4dev above OpenBSD. My first line tries to open "out" in another thread. This blocks the other thread, but because of this bug, also blocks my entire irb process! I must hit Control-C to cancel the open.

OpenBSD is also among the last systems to implement threads in userspace. OpenBSD pthreads(3) is a "user-level library" that uses non-blocking IO with the kernel. We know that open("out", O_WRONLY|O_NONBLOCK) is not possible, so I guess that the library blocks the entire process. This bug should only happen with OpenBSD. Other OS (like Linux, FreeBSD, NetBSD) might have kernel threads.

Some interpreters, like Ruby MRI 1.8.x, have "green threads". These interpreters might also use non-blocking IO, so they might block the entire process when opening "out" with any OS. --Kernigh 04:21, 2 October 2011 (UTC)