waitpid()系统调用学习

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取不同的值时,在这里有不同的意义。

                pid>0时,只等待进程ID等于pid的子进程,不管其它已经有多少子进程运行结束退出了, 

              只要指定的子进程还没有结束,waitpid就会一直等下去。

  • pid=-1时,等待任何一个子进程退出,没有任何限制,此时waitpid和wait的作用一模一样。
  • pid=0时,等待同一个进程组中的任何子进程,如果子进程已经加入了别的进程组,waitpid不会对它做任何理睬。
  • pid<-1时,等待一个指定进程组中的任何子进程,这个进程组的ID等于pid的绝对值。

options

 

ret=waitpid(-1,NULL,WNOHANG | WUNTRACED);

如果我们不想使用它们,也可以把options设为0,如:

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种情况:

当正常返回的时候,waitpid返回收集到的子进程的进程ID;

  • 如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
  • 如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;

当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