wait, waitPID |
Prototype: |
#include <sys/types.h>
#include <sys/wait.h>
PID_t wait(int *status);
PID_t waitPID(PID_t PID, int *status, int options);
|
General Description: |
Wait for and acknowledge the termination of a child process. This is important to keep zombie processes from lingering in the process table and to free up valuable resources. The wait() call waits for any process to terminate, and the waitPID() call permits you to specify a specific process or group. You can use the following macros to get the meaning from the status: WIFEXITED(status) is non-zero if the child exited normally. WEXITSTATUS(status) evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call to exit() or as the argument for a return statement in the main program. This macro can only be evaluated if WIFEXITED returned non-zero. WIFSIGNALED(status) returns true if the child process exited because of a signal which was not caught. WTERMSIG(status) returns the number of the signal that caused the child process to terminate. This macro can only be evaluated if WIFSIGNALED returned non-zero. WIFSTOPPED(status) returns true if the child process which caused the return is currently stopped; this is only possible if the call was done using WUNTRACED. WSTOPSIG(status) returns the number of the signal which caused the child to stop. This macro can only be evaluated if WIFSTOPPED returned non-zero. |
Return Value: |
Both calls return the PID of the child that terminated. |
Parameters |
status |
Returns the ending status of the child. If not zero or NULL, this parameter picks up the child's termination code and exit() value. |
PID |
Indicates which process to wait for:< -1 - Wait for any child process whose process group ID is equal to the absolute value of PID.== -1 - Wait for any child process; this is the same behavior which wait exhibits.== 0 - Wait for any child process whose process group ID is equal to that of the calling process.> 0 - Wait for the child whose process ID is equal to the value of PID. |
options |
WNOHANG - Return immediately if no child has exited.WUNTRACED - Return for children which are stopped, and whose status has not been reported. |
Possible Errors |
ECHILD |
If the process specified in PID does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN.) |
EINVAL |
If the options argument was invalid. |
EINTR |
If WNOHANG was not set and an unblocked signal or a SIGCHLD was caught. Just try again. |
Examples |
void sig_child(int signum) /* This handler only gets one waiting zombie */
{ int status;
wait(&status);
if ( WIFEXITED(status) )
printf("Child exited with the value of %d\n", WEXITSTATUS(status));
if ( WIFSIGNALED(status) )
printf("Child aborted due to signal #%d\n", WTERMSIG(status));
if ( WIFSTOPPED(status) )
printf("Child stopped on signal #%d\n", WSTOPSIG(signal));
}
|
void sig_child(int signum) /* This handler removes all waiting zombies */
{
while ( waitPID(-1, 0, WNOHANG) > 0 );
}
(c)Threads
|