本文章主要是man手册翻译及摘录
wait()
NAME
wait, waitpid, waitid - wait for process to change state
//等待进程状态发生变化
SYNOPSIS
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *wstatus);
//参数:进程状态,把从子进程中收尸收回来的状态放到一个变量
//里去,所以是一个整形变量,有一个实实在在的空间,即使指针
//也可以,但必须指向一个空间
RETURN VALUE
wait(): on success, returns the process ID of the terminated child; on error, -1 is returned.
//返回终止的子进程的ID号,失败返回-1
(For Linux-only options, see below.)
If wstatus is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the follow‐
ing macros (which take the integer itself as an argument, not a pointer to it, as is done in wait() and waitpid()!):
WIFEXITED(wstatus) // wait it exited是否是正常结束
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
WEXITSTATUS(wstatus) //进程结束的状态
returns the exit status of the child. This consists of the least significant 8 bits of the status argument 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.
/返回子进程的退出状态码,使用规则为WIFEXITED为真
WIFSIGNALED(wstatus)
returns true if the child process was terminated by a signal.
//如果子进程是由一个信号终止的,有这个宏返回值为真
WTERMSIG(wstatus)
returns the number of the signal that caused the child process to terminate. This macro should be employed only if WIFSIGNALED returned true.
//如果是由信号打断的,打印出那个信号
WCOREDUMP(wstatus)
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 implementations (e.g., AIX, SunOS). Therefore, enclose its use inside #ifdef WCOREDUMP ... #endif.
WIFSTOPPED(wstatus)
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(wstatus)
returns the number of the signal which caused the child to stop. This macro should be employed only if WIFSTOPPED returned true.
WIFCONTINUED(wstatus)
(since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.
waitpid()
NAME
wait, waitpid, waitid - wait for process to change state
//等待进程状态发生变化
SYNOPSIS
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *wstatus);
//参数:进程状态,把从子进程中收尸收回来的状态放到一个变量
//里去,所以是一个整形变量,有一个实实在在的空间,即使指针
//也可以,但必须指向一个空间
pid_t waitpid(pid_t pid, int *wstatus, int options);
//这个wait是死等,如果进程结束了,会有信号通知,才会去收尸,如果没有信号,就会死。 第三个参数很重要
RETURN VALUE
waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by
pid exist, but have not yet changed state, then 0 is returned. On error, -1 is returned.
The waitpid() system call suspends execution of the calling thread until a child specified by pid argument has changed state. By default, waitpid()
waits only for terminated children, but this behavior is modifiable via the options argument, as described below.
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.
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.
(For Linux-only options, see below.)
If wstatus is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the follow‐
ing macros (which take the integer itself as an argument, not a pointer to it, as is done in wait() and waitpid()!):
WIFEXITED(wstatus)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
WEXITSTATUS(wstatus)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument 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(wstatus)
returns true if the child process was terminated by a signal.