wait和waitpid
wait函数
调用该函数使进程阻塞,直到任一个子进程结束【SIGCHLD】或者是该进程接收到了一个信号为止。如果该进程没有子进程或者其子进程已经结束,wait函数会立即返回。
头文件:#include<sys/types.h>
#include<sys/wait.h>
函数原型: pid_t wait(int *status)
函数参数:status 保存子进程结束时的状态。使用地址传递,父进程获得该变量。若无需获得状态,则参数设置为NULL。包含返回值+错误信息
返回值:成功:已回收的子进程的PID
失败:-1
waitpid 函数
头文件:#include<sys/types.h>
#include<sys/wait.h>
函数原型:pid_t waitpid(pid_t pid, int *status,int options)
函数参数:pid pid是一个整数,具体的数值含义为:
pid >0 回收PID等于参数pid的子进程
pid =-1 回收任何一个子进程。此时同wait()
pid =0 回收其组ID等于调用进程的组ID的任一子进程
pid <-1 回收其组ID等于pid的绝对值的任一子进程
status 同wait()
options 0:同wait(),此时父进程会阻塞等待子进程退出。
WNOHANG:若指定的进程未结束,waitpid不阻塞,则立即返回0。
WUNTRACED:若某实现支持作业控制,则由pid指定的任一子进程状态已暂停,且其状态自暂停以来还未报告过,则返回其状态。
返回值:>0 已经结束运行的子进程号
0 使用WNOHANG选项且子进程未退出
-1 错误
WIFEXITED(status): 如果子进程正常结束则为非0值。
WEXITSTATUS(status): 取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。
wait示例代码:
/*************************************************************************
@Author: wanghao
@Created Time : Mon 21 May 2018 04:20:58 AMPDT
@File Name: wait_demo.c
@Description:
************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
/* wait */
int main(int argc, char **argv)
{
pid_tpid;
intstatus;
printf("parent[pid=%d]is born\n", getpid());
if(-1 == (pid = fork())) {
perror("forkerror");
return-1;
}
if(pid == 0){
printf("child[pid=%d]is born\n", getpid());
sleep(5);
printf("childis over\n");
return1;
}
else{
pid_tpid_w;
pid_w= wait(&status);
if(pid_w < 0) {
perror("waiterror");
return1;
}
if(WIFEXITED(status)) {
status= WEXITSTATUS(status);
printf("waitreturns with pid = %d. return status is %d\n", pid_w, status);
}else {
printf("waitreturns with pid = %d. the child is terminated abnormally\n", pid_w);
}
printf("fatheris over\n");
return0;
}
}
运行结果如下:
parent[pid=4286] is born
child[pid=4287] is born
child is over
wait returns with pid = 4287. return statusis 1
father is over
waitpid示例代码:
/*************************************************************************
@Author: wanghao
@Created Time : Mon 21 May 2018 04:20:58 AMPDT
@File Name: wait_demo.c
@Description:
************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
/* wait */
int main(int argc, char **argv)
{
pid_tpid;
intstatus;
printf("parent[pid=%d]is born\n", getpid());
if(-1 == (pid = fork())) {
perror("forkerror");
return-1;
}
if(pid == 0){
printf("child[pid=%d]is born\n", getpid());
sleep(5);
printf("childis over\n");
return1;
}
else{
pid_tpid_w;
pid_w= wait(&status);
if(pid_w < 0) {
perror("waiterror");
return1;
}
if(WIFEXITED(status)) {
status= WEXITSTATUS(status);
printf("waitreturns with pid = %d. return status is %d\n", pid_w, status);
}else {
printf("waitreturns with pid = %d. the child is terminated abnormally\n", pid_w);
}
printf("fatheris over\n");
return0;
}
}
运行结果如下:
parent[pid=4393] is born
parent wait child return
child[pid=4394] is born
parent wait child return
parent wait child return
parent wait child return
parent wait child return
child is over
waitpid returns with pid = 4394.
father is over