Buffering question: interleaved output from child processes

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

Hi,

At Sat, 14 Feb 2004 17:18:07 +0900,
Joel VanderWerf wrote in [ruby-talk:92863]:

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?

$stdout.sync = true

···


Nobu Nakada

“Joel VanderWerf” wrote:

[…]

$stdout.sync

That’s a no-op :slight_smile:

daz

daz wrote:

“Joel VanderWerf” wrote:

[…]

$stdout.sync

That’s a no-op :slight_smile:

Ok. No more posting after midnight for me! :slight_smile:

Thanks to both of you.