From: Derek Lewis Date: 2003-12-13T02:55:29+09:00 Subject: Re: Bug with fifos in Linux? On Sat, 13 Dec 2003, ts wrote: > >>>>> "D" == Derek Lewis writes: > > D> I don't know if this is a new bug, known bug, or some sort of feature... > > Something which is intentional can't be called a bug :-) > > D> When I try to open a fifo, the entire ruby interpreter locks until > D> something else opens the other "end" of the fifo. Here's some example > D> code: > > You can use IO::NONBLOCK with IO::WRONLY or IO::RDONLY Should that be File::NONBLOCK and File::RDONLY? I just changed my code to use those, but it's still behaving strangly. Some more sample code based on the previous: FIFO_FILE='/tmp/rubyfifo' `mkfifo "#{FIFO_FILE}"` unless test(?e, FIFO_FILE) thread = Thread.new { puts "Opening fifo for writing" File.open(FIFO_FILE, File::NONBLOCK | File::WRONLY) { |file| puts "Starting write loop" while true puts "Thread is running" sleep 1 file.puts "Thread is running" end } } puts "Opening fifo for reading" File.open(FIFO_FILE, File::NONBLOCK | File::RDONLY) { |file| puts "Reading from fifo" sleep 2 while !file.eof line = file.gets("\n") puts "Read from fifo: #{line}" end puts "exited read loop" } I'm still never getting the "Starting write loop" output, but I do get the "Reading from fifo": lewisd@derlewi:/tmp$ ruby fifo.rb Opening fifo for writing Opening fifo for reading Reading from fifo exited read loop lewisd@derlewi:/tmp$ Also, if I comment out the entire thread bit, I get this output: lewisd@derlewi:/tmp$ ruby fifo.rb Opening fifo for reading Reading from fifo exited read loop lewisd@derlewi:/tmp$ With a 2 second delay between "Reading..." and "exited...", as expected. If, before I start the ruby program, I run this: $ echo "lala" > /tmp/rubyfifo The echo blocks, as the fifo hasn't been opened for reading yet, and when I run the ruby program, I get this: lewisd@derlewi:/tmp$ ruby fifo.rb Opening fifo for reading Reading from fifo Read from fifo: lala exited read loop lewisd@derlewi:/tmp$ If I remove the "File::NONBLOCK" from the open command, it waits until I open the fifo, as expected. I'm not sure how to read from a fifo reliably, without leaving out the NONBLOCK, which will then block the rest of the program. :( > > > Guy Decoux > > Derek Lewis =================================================================== Java Web-Application Developer Email : email@lewisd.com Cellular : 604.312.2846 Website : http://www.lewisd.com "If you've got a 5000-line JSP page that has "all in one" support for three input forms and four follow-up screens, all controlled by "if" statements in scriptlets, well ... please don't show it to me :-). Its almost dinner time, and I don't want to lose my appetite :-)." - Craig R. McClanahan