Subject: Re: possible libc bug...
To: John Franklin <franklin@elfie.org>
From: Frank van der Linden <fvdl@netbsd.org>
List: current-users
Date: 04/05/2004 17:06:46
On Mon, Apr 05, 2004 at 10:47:23AM -0400, John Franklin wrote:
> What's happening here is the printf output is being buffered by libc
> before the fork().  When fork() is called, both processes have one or
> both of the printf()'s buffered (depending on buffering mode, line vs
> full) and the data gets flushed when the program exits and the stdout
> stream is implicitly closed.

Yep. The interaction of stdio and fork() is a bit of a problem, but
it's always been the responsibility of the programmer to avoid
issues like this. This is an issue on all systems I know.

One needs to flush the streams before forking, or, if the child doesn't
do any other stdio operations, call _exit(2) instead of exit(2), since
that doesn't flush buffers, etc.

But the bottom line is that it's the responsibility of the programmer
to deal with this.

- Frank