waitpid()的头文件
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid,int *status,int options)
pid
从参数的名字pid和类型pid_t中就可以看出,这里需要的是一个进程ID。但当pid取不同的值时,在这里有不同的意义。
options
ret=waitpid(-1,NULL,WNOHANG | WUNTRACED);
ret=waitpid(-1,NULL,0);
而WUNTRACED参数,由于涉及到一些跟踪调试方面的知识,加之极少用到,这里就不多费笔墨了,有兴趣的读者可以自行查阅相关材料。
wait不就是经过包装的waitpid吗?没错,察看<内核源码目录>/include/unistd.h文件349-352行就会发现以下程序段:
static inline pid_t wait(int * wait_stat)
{
return waitpid(-1,wait_stat,0);
}
返回值和错误
waitpid的返回值比wait稍微复杂一些,一共有3种情况:
当pid所指示的子进程不存在,或此进程存在,但不是调用进程的子进程,waitpid就会出错返回,这时errno被设置为ECHILD;
#include<stdio.h> #include<unistd.h> #include<sys/wait.h> int main(int argc,char *argv[]){ int rc=fork(); if(rc==0){ printf("child pid=%d",(int)getpid()); }else{ int d =waitpid(rc,NULL,0); printf("father d=%d",d); } return 0;} [root@localhost codec5]# ./t6 child pid=2164father d=2164[
显然父进程调用返回的是子进程的pid(进程号)号
在子进程里调用waitpid()
#include<stdio.h> #include<unistd.h> #include<sys/wait.h> int main(int argc,char *argv[]){ int rc=fork(); if(rc==0){ int d =waitpid(rc,NULL,0); printf("child pid=%d d=%d",(int)getpid(),d); }else{ printf("father "); } return 0;} [root@localhost codec5]# ./t6 father [root@localhost codec5]# child pid=2208 d=-1
显然由于子进程并没有创建新的经常说以waitpid返回的是 -1
原文:https://www.cnblogs.com/lhyzdd/p/13839424.html