Child doing parent's printf ??

Kirtikumar Satam, ECE, UMASS Amherst satam at ecs.umass.edu
Mon Feb 12 19:19:37 AEST 1990


I was playing with fork() and wrote the following program. The program
works as expected when its stdout is tty. But, when I redirect it to
a file an additional printf comes. Here it is.
-----------------------------Child.c-------------------------
/*
 * This program demonstrates the use of fork() system call
 * to create child processes. It also shows the use of wait()
 * system call for process synchronization. Other than that the
 * the program is trivial.
 */

#include <stdio.h>
#include <sys/wait.h>

union wait *status_1; /* argument to wait()*/

int parent_pid, child_pid; /* pids of processes */ 
int got_child_pid; /* return values of wait() */
void main()
{
	parent_pid = getpid(); /* first get My pid */
	fprintf(stdout,"Parent : I am parent with pid %d\n",parent_pid); 
	if ((child_pid = fork()) == 0) /* enter child code */
	{
	   fprintf(stdout,"Child : I am child of parent %d and my pid is %d\n",
	   parent_pid, getpid());  
	   exit(0); /* exit with status 1 */
	} /* end child code */
	else if (child_pid != -1) 
	{
	   /* all set, then wait */
	   got_child_pid = wait(status_1); 
		/* get the pid from the dying child */
	   fprintf(stdout,"Parent : My child with pid %d died\n", 
		got_child_pid);
	   exit(0); /* exit with satisfaction */
	}
	/* fork() failed, so bye bye */
	perror("fork failed for child"); /* get the error */
	exit(1); /* exit with status 1 */
}
-----------------------------------------------------------------
Now, the output on tty is
----------------------tty output --------------------
Parent : I am parent with pid 7704
Child : I am child of parent 7704 and my pid is 7705
Parent : My child with pid 7705 died
----------------------------------------------------

ANd when I redirect it is
----------------------Redirected-------------------
Parent : I am parent with pid 7708
Child : I am child of parent 7708 and my pid is 7709
Parent : I am parent with pid 7708
Parent : My child with pid 7709 died
----------------------------------------------------

Clearly the third line is being printed by the child.
It can be construed that the string in the parent's virtual
space is being copied into the child's space before
it is removed from the system. This behaviour can be
attributed to slow tty rather than to a fast disk (here
actually disk buffers), but in reality it is the reverse.

The above problem can be removed by using fflush(stdout)
before forking. But, why it happens in the first place?

By the way, I am using ULTRIX-32 V3.1 (Rev. 9).

Any insight fellow wizards?
-satam
------------------------------------------------------------------------
Kirtikumar "Mumbaichaa" Satam
INTERNET : satam at ecs.umass.edu
BITNET : satam at umaecs.bitnet
217 Northwood Apts, Sunderland, MA 01375   Tel# 413-665-3222
------------------------------------------------------------------------



More information about the Comp.unix.wizards mailing list