Re: Process.fork weirdness



Joel VanderWerf wrote:
Gary Wright wrote:
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.

Huh, I never knew that. That's dangerous if the parent process is
multithreaded, isn't it? Because even if the forking thread flushes all
open files, other threads can keep filling buffers before the fork happens.

In process.c, rb_f_fork() flushes stdout and stderr before forking, so
at least these two buffers are protected from other ruby threads. It
seems like there should be an option to flush everything.

Maybe the best policy is to do any one of the following:

1. Avoid buffered output (puts, write), and instead use unbuffered calls
(syswrite, send) or set io.sync=true.

2. Don't mix threads and fork, and flush everything before forking.

3. Create children only as

fork {do_stuff; exit!}
fork {do_stuff; exec ...}
system "..."

and so on. (And be careful that the do_stuff doesn't flush any buffered
IO that belongs to the parent.)

Most libraries aren't going to follow #1, and #2 seems impractical to
enforce.

Actually, I guess #3 is why I haven't seen this problem before... it's
often a good idea to use exit! in fork to avoid duplicating at_exit()
handlers anyway. I wish there were a method that the child could call
before anything else to _clear_ (not flush) all buffers and also to
clear the at_exit handlers.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

.



Relevant Pages

  • Re: Can extra processing threads help in this case?
    ... Part of the issue here is what is meant by "flush", ... held in the application and the buffers held in the ... indicate that this would be required for my transaction log. ... yet pointed out any issues with Unix/Linux named pipes. ...
    (microsoft.public.vc.mfc)
  • Re: FLUSH [was: Mixed language programming Tcl/Tk and Fortran (Windows)]
    ... > determining where and when to FLUSH which buffers - even if it's ... > buffers from source code. ... library that is the first level of interface to the compiled code itself ...
    (comp.lang.fortran)
  • Re: Files not writing, closing files, finalize()
    ... Because PrintWriter.closeflushes the stream first but the OS's closing doesn't flush the stream. ... Constructing a FileOutputStream under default settings will truncate the file, but it doesn't write the data until buffers are flushed. ...
    (comp.lang.java.programmer)
  • Re: Bugg? in tableupdate - VFP8-SP1
    ... You might want to consider the FLUSH command, but please read on (it's form ... Buffers are automatically ... into the process of opening a file in VFP, so that if it detects the header ...
    (microsoft.public.fox.programmer.exchange)
  • Re: File Copy Without Win Buffering
    ... Reset Does NOT flush the buffers even though the docs say it does. ... I want to make sure the data is actually written to the disk surface. ...
    (microsoft.public.vb.general.discussion)