Collecting the exit status of a child is called "waiting on the
process". Linux defines four functions that return the exits status.
The first, wait4(), takes four arguments, hence its name. wait4() has
the following prototype:
pid_t wait4(pid_t pid, int *status, int options, struct rusage
The first argument is the pid of the process whose status is returned.
It may contain one of the following ranges of values:
< -1 -- Wait for any child whose pgid is the same as the absolute
value of pid.
-1 -- Waits for any child to terminate.
0 -- Waits for any child in the same process group as the current
> 0 -- Waits for a process whose pid was passed as an argument.
The second argument is a pointer to int. wait4() assigns the exit
status of the examined process to it. The third argument controls the
behavior of wait4(). The WNOHANG flag causes wait4() to return
immediately. If no processes are available, then wait4() returns 0
rather than a valid pid. The WUNTRACED flag causes wait4() to return if
a child has been stopped. You can combine these two flags with the
bitwise or operator. The final argument is a pointer to a rusage struct
that wait4() fills with resource usage information of the examined
process and its children. If you pass a NULL value, then no resource
usage information is returned.
Each of the three additional functions offers a subset of wait4()'s
pid_t wait(int* status);
The call always blocks until a child has returned.
pid_t waitpid(pid_t pid, int *status, int options);
Identical to wait4() except that it doesn't retrieve resource usage
pid_t wait3(int *status, int options, struct rusage *usage);
Identical to wait4() except that the user cannot specify which child
should be checked.
--Analyzing the Exit Status
As the exit status format is somewhat convoluted, a set of macros
enables you to extract information about the causes of the examined
Returns true if the process exited normally (i.e., if it exited as a
result of calling exit() from the main() function or if its main()
executed a return statement). If WIFEXITED(status) is true, WEXITSTATUS
(status) returns the process's exit code.
Returns true if the process exited due to a signal. If WIFSIGNALED
(status) is true, WTERMSIG(status) returns the signal number that
terminated the process.
Returns true if the process has been stopped due to a signal (read more
on Linux processes' modes here:
http://www.itworld.com/nl/lnx_tip/03022001/). If WIFSTOPPED(status) is
true, then WSTOPSIG(status) returns the signal that stopped the
process. Note: wait4() returns information on stopped processes only if
you specify the WUNTRACED flag as on option.