From: Derek Lewis Date: 2003-12-13T04:46:58+09:00 Subject: Re: Bug with fifos in Linux? I tried IO::NONBLOCK: lewisd@derlewi:/tmp$ ruby p IO::NONBLOCK -:1: uninitialized constant NONBLOCK at IO (NameError) I'm using 1.6.8, is it perhaps only in 1.8? I feel rather silly about missing that abort_on_exception bit, seeing as I just recommended the same thing to someone else on the mailing list here. ;) I think understand why I'm getting a "No such device or address" error opening it for writing, but not while opening it for reading. It seems like it has to be opened for reading before it can be opened for writing. Does that sound right? If I put a sleep before the WRONLY open, so that the RDONLY open happens first, everything seems to work, with the caveat being that I have to put a file.flush after the file.puts, or I never seem to read anything in the read loop. Is there a reason ruby behaves like this, rather than having the open calls succeede without blocking (provided the file exists), and having the blocking happen on the read/write calls? My only past experience with reading/writing fifos was in Java, and that was how it behaved, if memory serves me correctly. On Sat, 13 Dec 2003, ts wrote: > >>>>> "D" == Derek Lewis writes: > > D> Should that be File::NONBLOCK and File::RDONLY? I just changed my code to > > IO:: is just fine > > D> use those, but it's still behaving strangly. > > D> Some more sample code based on the previous: > > D> FIFO_FILE='/tmp/rubyfifo' > D> `mkfifo "#{FIFO_FILE}"` unless test(?e, FIFO_FILE) > > add this > > Thread.abort_on_exception = true > > and you'll see the error > > D> thread = Thread.new { > D> puts "Opening fifo for writing" > D> File.open(FIFO_FILE, File::NONBLOCK | File::WRONLY) { |file| > D> puts "Starting write loop" > D> while true > D> puts "Thread is running" > D> sleep 1 > D> file.puts "Thread is running" > D> end > D> } > D> } > > > > svg% cat b.rb > #!/usr/bin/ruby > FIFO_FILE='/tmp/rubyfifo' > `mkfifo "#{FIFO_FILE}"` unless test(?e, FIFO_FILE) > > Thread.abort_on_exception = true > thread = Thread.new { > begin > puts "Opening fifo for writing" > File.open(FIFO_FILE, IO::NONBLOCK | IO::WRONLY) { |file| > puts "Starting write loop" > while true > puts "Thread is running" > sleep 1 > file.puts "Thread is running" > file.flush > end > } > rescue > p $! > sleep 1 > retry > end > } > > puts "Opening fifo for reading" > File.open(FIFO_FILE, IO::NONBLOCK | IO::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" > } > svg% > > svg% b.rb > Opening fifo for writing > # > Opening fifo for reading > Reading from fifo > Opening fifo for writing > Starting write loop > Thread is running > Thread is running > Read from fifo: Thread is running > Thread is running > Read from fifo: Thread is running > Thread is running > Read from fifo: Thread is running > Thread is running > Read from fifo: Thread is running > ^Z > [4]+ Stopped b.rb > svg% > > > > > 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