The following code, when connected to the terminal's stdio, interleaves 
the outputs in time-order.

When I pipe the stdout or redirect to a file, all the "B" outputs are 
grouped at the top (in time order), followed by the "A" outputs.

I assume this is due to buffering in the child processes. What can I do 
to make the piped/redirected output behave like terminal output?

$ ruby -v
ruby 1.9.0 (2004-01-08) [i686-linux]

----

$stdout.sync

START_TIME = Time.now.to_f

fork do
   srand 1
   10.times do
     sleep rand
     puts "A: #{Time.now.to_f - START_TIME}"
   end
end

fork do
   srand 2
   10.times do
     sleep rand
     puts "B: #{Time.now.to_f - START_TIME}"
   end
end

Process.wait
Process.wait