故事
当一个里程终止时,可以是正常的终止,也可以是不正常的终止,linux kernel会发一个SIGCHLD的signal给parent。终止一个child是一个异步的事件,所以parent必须要能够处理child的事件,这就需要wait或waitpid来帮忙实现。
wait或waitpid可以
- Block, if all of its children are still running
- Return immediately with the termination status of a child, if a child has
terminated and is waiting for its termination status to be fetched - Return immediately with an error, if it doesn’t have any child processes
函数原形
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
Both return: process ID if OK, 0 (see later), or −1 on error
wait与waitpid的不同点
- The wait function can block the caller until a child process terminates, whereas
waitpid has an option that prevents it from blocking. - The waitpid function doesn’t wait for the child that terminates first; it has a
number of options that control which process it waits for
专用的宏用于处理status
If status is not NULL, wait() and waitpid() store status information in the int to which it points. This integer
can be inspected with the following macros (which take the integer itself as an argument, not a pointer to it, as
is done in wait() and waitpid()!):
WIFEXITED(status)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning
from main().
WEXITSTATUS(status)
returns the exit status of the child. This consists of the least significant 8 bits of the status argu‐
ment that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement
in main(). This macro should be employed only if WIFEXITED returned true.
WIFSIGNALED(status)
returns true if the child process was terminated by a signal.
WTERMSIG(status)
returns the number of the signal that caused the child process to terminate. This macro should be
employed only if WIFSIGNALED returned true.
WCOREDUMP(status)
returns true if the child produced a core dump. This macro should be employed only if WIFSIGNALED
returned true. This macro is not specified in POSIX.1-2001 and is not available on some UNIX implementa‐
tions (e.g., AIX, SunOS). Only use this enclosed in #ifdef WCOREDUMP ... #endif.
WIFSTOPPED(status)
returns true if the child process was stopped by delivery of a signal; this is possible only if the call
was done using WUNTRACED or when the child is being traced (see ptrace(2)).
WSTOPSIG(status)
returns the number of the signal which caused the child to stop. This macro should be employed only if
WIFSTOPPED returned true.
WIFCONTINUED(status)
(since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.
waitpid的pid参数
The value of pid can be:
< -1 meaning wait for any child process whose process group ID is equal to the absolute value of pid.
-1 meaning wait for any child process.
0 meaning wait for any child process whose process group ID is equal to that of the calling process.
> 0 meaning wait for the child whose process ID is equal to the value of pid.
waitpid的option参数
The value of options is an OR of zero or more of the following constants:
WNOHANG return immediately if no child has exited.
WUNTRACED also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which
have stopped is provided even if this option is not specified.
WCONTINUED (since Linux 2.6.10)
also return if a stopped child has been resumed by delivery of SIGCONT.
小结:wait VS waitpid
1. The waitpid function lets us wait for one particular process, whereas the wait
function returns the status of any terminated child. We’ll return to this feature
when we discuss the popen function.
2. The waitpid function provides a nonblocking version of wait. There are
times when we want to fetch a child’s status, but we don’t want to block.
3. The waitpid function provides support for job control with the WUNTRACED
and WCONTINUED options.