1.wait 与waitpid的关系
wait() and waitpid()
The wait() system call suspends execution of the calling process until
one of its children terminates. The call wait(&status) is equivalent
to:
waitpid(-1, &status, 0);
2.status 与 option
The waitpid() system call suspends execution of the calling process
until a child specified by pid argument has changed state. By default,
waitpid() waits only for terminated children, but this behavior is mod‐
ifiable 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 con‐
stants:
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.
函数的返回值
RETURN VALUE
wait(): on success, returns the process ID of the terminated child; on error, -1 is
returned.
waitpid(): on success, returns the process ID of the child whose state has changed; if WNO‐
HANG 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.
waitid(): returns 0 on success or if WNOHANG was specified and no child(ren) specified by id
has yet changed state; on error, -1 is returned. Each of these calls sets errno to an
appropriate value in the case of an error.
ERRORS
ECHILD (for wait()) The calling process does not have any unwaited-for children.
ECHILD (for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id
(waitid()) 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. See also the Linux
Notes section about threads.)
EINTR WNOHANG was not set and an unblocked signal or a SIGCHLD was caught; see signal(7).
EINVAL The options argument was invalid.
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int fd[2];
pipe(fd);
int status;
int pid=fork();
if(pid==0){
printf("in child,sleep 4s \n");
sleep(4);
printf("exit...\n");
exit(-4);//the same to return(-50)
}else if(pid<0){
printf("fork fail!\n");
}
printf("wait for child!\n");
fflush(stdout);
printf("return code is child pid[%d]\n",wait(&status));
if(WIFEXITED(status)){
printf("return normally!\n");
printf("return value is[%d]\n",WEXITSTATUS(status));
}
return 0;
}
wait() 回收子孙进程,而非仅仅儿子进程
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid,i;
if(fork()==0)
{
for(i=0; i<1000; i++)
{
pid=fork();
if(pid==0)
{
exit(0);
}
}
}
printf("wait for 20s\n");
fflush(stdout);
sleep(20);
if((pid=wait(NULL))>0)
{
printf("clooect [%d]\n",pid);
while(pid>0)
{
pid=wait(NULL);
printf("clooect [%d]\n",pid);
fflush(stdout);
}
printf("wait for 50s again\n");
fflush(stdout);
sleep(50);
return 0;
}
}