On Jul 12, 2009, at 3:04 PM, Janus Bor wrote:
>
> It goes on until both values are 10, as if there were two loops within
> each other. Note that 7,1,1 appears before 6,1,1 though. I have
> absolutely no idea how this is produced, but it must be caused by
> forking. If I remove Process.fork I get the expected output.

You need to flush the output stream before forking so that the child  
process doesn't get any buffered data. Otherwise, every time the child  
process exits, it will flush the duplicate data out to the  
'output.txt' file.

> test = open("output.txt", "w")
> 1.upto(10) do |i|
>    test.print(i.to_s + ",")
>    test.flush  # ensures that forked child has clean IO buffers.
>     Process.fork {sleep(10)}
>    test.puts i
> end
> Process.waitall