Wait for a child process to change state.
Syntax
#include <sys/wait.h>
pid_t waitpid (pid_t pid, int *status, int options);
Parameters
-
pid
The process to check on. If this argument is negative, any state change from any child process will be returned. The return value will always be the PID of the process that changed state.
-
status
A pointer to an integer that will receive the status code and flags. Use the macros below to extract useful information.
-
options
Options for
waitpid
:-
WNOHANG
: Do not letwaitpid
block, and return immediately if no state change has occured. -
WUNTRACED
: Ignored, but definition available for compatibility.
-
Return Value
The return value is the PID of the process that changed state if successful. Otherwise, the return value is negative and errno is set:
-
ECHILD
No such child process.
The process specified is not a child of this process.
-
EINTR
Interrupted system call. May happen if a signal was delivered to a process waiting with
waitpid
.
Remarks
Use the following macros to read the value of status
:
-
WIFEXITED(status)
: True if the process terminated normally. -
WEXITSTATUS(x)
: Get the return code of the process. -
WIFSIGNALED(status)
: State change due to signal delivery. -
WTERMSIG(status)
: Terminated due to a signal. -
WIFSTOPPED(status)
: Stopped.