7.3.6 waitpid (), A Parent Waiting for a Child

7.3.6  waitpid (), A Parent Waiting for a Child

It is quite possible that sometimes we want a parent process and a child process to communicate. Only very limited communication is possible between a parent and a child. In fact, the parent can usually just wait for a child to die and nothing more. In the human context, usually a parent dies before a child. However, in the context of Unix processes, quite commonly it is the opposite. Usually, a parent process creates a child and engages it in doing something useful. Once the child process finishes the designated task, the child dies. If later another child is needed for performing another task, the parent creates a second child. Thus, usually, the parent is long-lived whereas the children are
more ephemeral. A parent process can wait to be informed when a child dies. There are two waiting functions: wait and waitpid.

A parent process can execute waitpid after forking a child. waitpid takes the child’s process ID as argument. waitpid causes the parent process to suspend itself and wait till the child executes its given task and dies. Thus, the parent process just waits, doing nothing, till the child with the process ID expires. At this point, the system informs the parent process and it wakes up and starts executing from where it was suspended. The following program illustrates this.

 Program 7.24

#!/usr/bin/perl
use strict;
my ($childProcessID, $i);

print "I AM THE ONLY PROCESS.\n";

$childProcessID = fork ();

if ($childProcessID){
        print "I am the parent process.\n";
        print "I spawned a new process with ID $childProcessID\n";
        waitpid ($childProcessID, 0);
        print "The child process is terminated with status $?\n";
    }
else{
    for ($i = 0; $i <= 10; $i++){
         print "I am the child process: Counting $i\n";
    }
}

The output may look like the following. It is possible that the “I spawned” line is printed after the child process finishes printing at count 10

I AM THE ONLY PROCESS.
I am the parent process.
I spawned a new process with ID 7610
I am the child process: Counting 0
I am the child process: Counting 1
I am the child process: Counting 2
I am the child process: Counting 3
I am the child process: Counting 4
I am the child process: Counting 5
I am the child process: Counting 6
I am the child process: Counting 7
I am the child process: Counting 8
I am the child process: Counting 9
I am the child process: Counting 10
The child process is terminated with status 0

It shows that the parent creates a child process and then it waits doing nothing, specifically waiting morbidly for the recently created child to die. The child process prints a sequence of lines using the for loop and dies. Once the child dies, the operating system informs the parent process of the death of the child. This causes the parent to get active again and print the line informing the child has been terminated.

To be precise, waitpid suspends execution of the current process until the child process specified by the process ID argument has died, or until a signal has been delivered whose action is to terminate the current process or call a signal handling function. We do not discuss signals much in this book. If a child as requested by the process ID has already exited by the time of the call, the function returns immediately.

The second argument to waitpid is called the options argument. The meaning of this argument is complicated and we do not discuss it here. The value of zero suffices for our purpose. To understand the value of the argument, the reader is instructed to consult the Unix man page on waitpid or wait. There are discussions on processes in Unix for Programmers and Users [GA99] and Unix System Administration Handbook [NSSH01].

The special variable $? stores the status returned by the waitpid function. The value can vary from system to system, situation to situation.

The wait function is similar to the waitpid function except that wait waits for any child and not a specific numbered child to die.