waitParent Wait-For-Child Function
The left arrow above will return you to
the Home page
The left arrows below will return you to the Previous page
When a process terminates, either normally or abnormally, the parent is notified by the kernel sending the parent the SIGCHLD signal. The parent can choose to ignore the signal (the default) or it can provide a function that is called when the signal occurs. The system provides functions wait or waitpid that can
- block (if all of its children are still running), or
- return immediately with the termination status of a child (if a child has terminated and is waiting for its termination status to be fetched), or
- return immediately with an error (if it doesn't have any child processes).
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, int options);
Both return: process ID if OK, 0 or -1 on error
The differences between the two functions are:
- wait can block the caller until a child process terminates, while waitpid has an option that prevents it from blocking.
- waitpid doesn't wait for the first child to terminate - it has a number of options that control which process it waits for.
If a child has already terminated and is a zombie, wait returns immediately with that child's status. Otherwise it blocks the caller until a child terminates. If the caller blocks and has multiple children, wait returns when one terminates - the function returns the process ID of the particular child.
Both functions return an integer status, *statloc. The format of this status is implementation dependent. Macros are defined in <sys/wait.h>.
The pid parameter of waitpid specifies the set of child processes for which to wait.
- pid == -1
- waits for any child process. In this repect, waitpid is equivalent to wait
- pid == 0
- waits for any child process in the process group of the caller
- pid > 0
- waits for the process with process ID pid
- pid < -1
- waits for any process whose process group id equals the absolute value of pid.
The options for waitpid are a bitwise OR of any of the following options:
- the call should not block if there are no processes that wish to report status
- children of the current process that are stopped due to a SIGTTIN, SIGTTOU, SIGTSTP, or SIGSTOP signal also have their status reported
The descriptions of the system functions above are drawn from sources that include
manuals on the Sun Solaris system and the MAC OS X Darwin/BDS system, and also from 'Advanced Programming in the UNIX Environment', W. Richard Stevens, Addison-Wesley, 1993.
For use only by students and instructors using the supplementary material available with the text book: "Operating Systems - Internals and Design Principles", William Stallings, Prentice Hall, 5th Edition, 2004. Not to be printed out or copied by any other persons or used for any other purpose without written permission of the author(s).