erroneous "hello" from forked "hello world" process!

Conor P. Cahill cpcahil at virtech.uucp
Mon Oct 1 21:41:41 AEST 1990


In article <Sep.30.21.09.46.1990.2881 at romulus.rutgers.edu> mef at romulus.rutgers.edu (Marc Fiuczynski) writes:
>
>main ()
>{
>	int pid = 1;
>	printf("Hello\n");
>	pid=fork();
>	if(pid==0){
>		printf("World\n");
>	}
>}

The problem here is that when you redirect the program to a file, the
standard output is buffered.  So the hello is buffered until you flush
the buffer (something that is done automatically at exit) and is therefore
replicated when the program forks.

When the standard output is not redirected, the output will be line buffered
and therefore upon seeing the \n following the hello, it is written out and
hence gone before the program duplicates itself.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.lang.c mailing list