From: Josh Date: 2010-09-07T05:10:18+09:00 Subject: trouble with mkfifo and forks # In a nutshell: 1) Create a pipe 2) Fork twice 3) Each for sends 10 strings over the pipe 4) See what comes out the other side So, why isn't what I send coming out? class WaitListTest < Test::Unit::TestCase NUMBER_OF_ORDERS = 2 PIPE = "test/wl_pipe" def setup `rm #{PIPE}` `mkfifo #{PIPE}` end def test_registering_last_race term_count = 0 responses = [] pids = [] NUMBER_OF_ORDERS.times do |i| pids << Process.fork do sleep 1 + (0.1 * i) 10.times do |a| File.open("#{RAILS_ROOT}/#{PIPE}", "w+") do |pipe| sleep 0.1 pipe.puts "Fork #{i} - #{a}" end end File.open("#{RAILS_ROOT}/#{PIPE}", "a") do |pipe| sleep 0.1 pipe.puts "DONE" end end end while term_count < NUMBER_OF_ORDERS File.open("#{RAILS_ROOT}/#{PIPE}", "r+") do |pipe| val = pipe.gets if val.match(/DONE/) term_count += 1 else responses << val end end end puts responses.sort puts responses.count end end # output Started Fork 0 - 0 Fork 0 - 1 Fork 0 - 2 Fork 0 - 3 Fork 0 - 4 Fork 0 - 5 Fork 0 - 7 Fork 0 - 9 Fork 1 - 0 Fork 1 - 1 Fork 1 - 2 Fork 1 - 3 Fork 1 - 4 Fork 1 - 5 Fork 1 - 7 Fork 1 - 8 Fork 1 - 9 17 Why isn't this 20?