Subject: Re: possible libc bug...
To: John Franklin <franklin@elfie.org>
From: Chris Pinnock <cjep@fawlty.net>
List: current-users
Date: 04/06/2004 10:28:28
On Mon, Apr 05, 2004 at 10:47:23AM -0400, John Franklin wrote:
> IMHO, this is a bug.  However, I see it happen on Linux and NetBSD.  I
> have a sneaking suspicion that it'll happen on other *NIX boxes as well.
> 
> Question: What should be the output of the following program?
> 
> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> 
> int main()
> {
>     printf("This will be buffered\n");
>     printf("This will also be buffered.");
>     fork();
>     return 0;
> }
> 
> I would expect: 
> [1]% ./fbt
> This will be buffered
> This will also be buffered[2]%

No, (depending on the libc implementation of course - i.e. when buffers
are flushed) I would expect two "This will also be buffered" on the terminal.

The output buffers get copied to the child's space at fork time (or something
like that).

(I've been asked this "what happens when you run this code" question 
at interviews.)

Best wishes,
Chris