Talk:IPC via named pipe: Difference between revisions

OpenBSD blocks the entire process.
(reading the docs helps)
(OpenBSD blocks the entire process.)
Line 1:
== Tricky to open "out" ==
This is tricky! The “<tt>in</tt>” reader side is normal asynchronous IO (the pipe becomes readable when there's data available) but the “<tt>out</tt>” 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! –[[User:Dkf|Donal Fellows]] 09:38, 30 September 2011 (UTC)
 
: <s>Actually I don't think <code>O_WRONLY|O_NONBLOCK</code> on a fifo will ever succeed on a POSIX system.</s>(unless there is already a reader) On Linux, you can <code>O_RDWR</code> open a fifo, and it will not block (because the program itself is the reader then), but then you can't <code>select</code> 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. --[[User:Ledrug|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 <code>open("out", O_WRONLY)</code> 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 <code>irb</code> 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 <code>irb</code> process! I must hit Control-C to cancel the open.
 
OpenBSD is also among the last systems to implement threads in userspace. OpenBSD [http://www.openbsd.org/cgi-bin/man.cgi?query=pthreads&apropos=0&sektion=3&manpath=OpenBSD+4.9&arch=i386&format=html pthreads(3)] is a "user-level library" that [http://archives.zmanda.com/amanda-archives/viewtopic.php?t=4815 uses non-blocking IO with the kernel]. We know that <code>open("out", O_WRONLY|O_NONBLOCK)</code> 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. --[[User:Kernigh|Kernigh]] 04:21, 2 October 2011 (UTC)
Anonymous user