562 Chapter 26
26.5 Exercises
26-1. Write a program to verify that when a child’s parent terminates, a call to getppid()
returns 1 (the process ID of init).
26-2. Suppose that we have three processes related as grandparent, parent, and child,
and that the grandparent doesn’t immediately perform a wait() after the parent
exits, so that the parent becomes a zombie. When do you expect the grandchild to
be adopted by init (so that getppid() in the grandchild returns 1): after the parent
terminates or after the grandparent does a wait()? Write a program to verify your
answer.
26-3. Replace the use of waitpid() with waitid() in Listing 26-3 (child_status.c). The call to
printWaitStatus() will need to be replaced by code that prints relevant fields from
the siginfo_t structure returned by waitid().
26-4. Listing 26-4 (make_zombie.c) uses a call to sleep() to allow the child process a chance
to execute and terminate before the parent executes system(). This approach
produces a theoretical race condition. Modify the program to eliminate the race
condition by using signals to synchronize the parent and child.