Bug when using Kernel.fork with File.print

Hello,

the following stripped down script prints one time “hello” to
stdout but four times to testfile which IMHO is not correct:

···

class Test
def do_something
fork do
end
Process.wait
end
end

aFile = File.new(“testfile”, “w”);
print(“hello\n”)
aFile.print(“hello\n”)

[1, 2, 3].each { |x| Test.new.do_something }

aFile.close

Tested with Ruby 1.8.0-preview1 (with errara patch) on Linux
and Ruby 1.6.8 on Windows (cygwin). Can anyone confirm that
this is a bug or ist it just me?

Regards,
Ralf

Hi,

the following stripped down script prints one time “hello” to
stdout but four times to testfile which IMHO is not correct:

This is not really a bug (but I admit it’s surprising result). It’s
caused by stdio behavior along with fork. Fork creates copy of the
parent process. Each child process you’ve forked has copy of the
output buffer attached to aFile. stdio library flushes output buffers
that remain opened. Thus, stdio flushed “hello\n” for each copy of
aFile output buffer.

						matz.
···

In message “Bug when using Kernel.fork with File.print” on 03/01/07, Ralf Papenkordt ralf.papenkordt@gmx.de writes: