聊聊wait和waitpid

一句话总结:wait来自于waitpid的简单封装,waitpid更灵活,可等待同组或其他组进程,可设置不阻塞。详细介绍man一下


1、先上个wait例子看下

#include <stdio.h>
#include <sys/types.h> //wait
#include <sys/wait.h>  //wait
#include <unistd.h> //fork sleep 
#include <stdlib.h> //exit
void main()
{
  pid_t pid1,pid2;
  pid1=fork();

  if(pid1<0) /* 如果出错 */
    printf("error ocurred!\n");
  else if(pid1==0){ /* 如果是子进程 */
    printf("child process, pid:%d\n",getpid());
    sleep(10); /* 睡眠10秒钟 */
  }
  else{ /* 如果是父进程 */
    int status = 0;
    pid2=wait(&status); /* 在这里等待 */
    printf("parent catched a child process with pid of %d, status:%d\n", pid2,status);
  }
  exit(0);
}

父进程阻塞,直到子进程结束

zjy@ubuntu:~/cpp$ ./main 

child process, pid:21532

parent catched a child process with pid of 21532, status:0

zjy@ubuntu:~/cpp

原型:pid_t wait(int *status)
进程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出,如果让它找到了这样一个已经变成僵尸的子进程,wait就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。
参数status用来保存被收集进程退出时的一些状态,它是一个指向int类型的指针。但如果我们对这个子进程是如何死掉的毫不在意,只想把这个僵尸进程消灭掉,(事实上绝大多数情况下,我们都会这样想),我们就可以设定这个参数为NULL,就象下面这样:
pid = wait(NULL);
如果成功,wait会返回被收集的子进程的进程ID,如果调用进程没有子进程,调用就会失败,此时wait返回-1,同时errno被置为ECHILD。


#include <stdio.h>
#include <sys/types.h> //wait
#include <sys/wait.h>  //wait
#include <unistd.h> //fork sleep 
#include <stdlib.h> //exit
void main()
{
  pid_t pid1,pid2;
  pid1=fork();

  if(pid1<0) /* 如果出错 */
    printf("error ocurred!\n");
  else if(pid1==0){ /* 如果是子进程 */
    printf("child process, pid:%d\n",getpid());
    sleep(5); /* 睡眠5秒钟 */
    exit(2);
  }
  else{ /* 如果是父进程 */
    int status = 0;
    pid2=wait(&status); /* 在这里等待 */
    if (WIFEXITED(status)) //非0表示子进程正常退出
    {
      printf("child process exit normally, pid:%d.\n", pid2);
      printf("return code:%d\n", WEXITSTATUS(status));
    }

  }
  exit(0);
}

zjy@ubuntu:~/cpp$ ./main 

child process, pid:27671

child process exit normally, pid:27671.

return code:2

zjy@ubuntu:~/cpp


2、再看下waitpid

函数原型

pid_t waitpid(pid_t pid,int *status,int options)
看下内核源码

static inline pid_t wait(int * wait_stat)
{
    return waitpid(-1,wait_stat,0);
}
wait其实也来自于waitpid。

参数介绍:
pid:从参数的名字pid和类型pid_t中就可以看出,这里需要的是一个进程ID。但当pid取不同的值时,在这里有不同的意义。
pid>0时,只等待进程ID等于pid的子进程,不管其它已经有多少子进程运行结束退出了,只要指定的子进程还没有结束,waitpid就会一直等下去。
pid=-1时,等待任何一个子进程退出,没有任何限制,此时waitpid和wait的作用一模一样。
pid=0时,等待同一个进程组中的任何子进程,如果子进程已经加入了别的进程组,waitpid不会对它做任何理睬。
pid<-1时,等待一个指定进程组中的任何子进程,这个进程组的ID等于pid的绝对值。
options:options提供了一些额外的选项来控制waitpid,目前在Linux中只支持WNOHANG和WUNTRACED两个选项,这是两个常数,可以用"|"运算符把它们连接起来使用,比如:
ret=waitpid(-1,NULL,WNOHANG | WUNTRACED);
如果我们不想使用它们,也可以把options设为0,如:
ret=waitpid(-1,NULL,0);
如果使用了WNOHANG参数调用waitpid,即使没有子进程退出,它也会立即返回,不会像wait那样永远等下去。
而WUNTRACED参数,由于涉及到一些跟踪调试方面的知识,用得很少。

返回值一共有3种情况:
1、当正常返回的时候,waitpid返回收集到的子进程的进程ID;
2、如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
3、如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
当pid所指示的子进程不存在,或此进程存在,但不是调用进程的子进程,waitpid就会出错返回,这时errno被设置为ECHILD

上个例子:

#include <stdio.h>
#include <sys/types.h> //wait
#include <sys/wait.h>  //wait
#include <unistd.h> //fork sleep 
#include <stdlib.h> //exit
int main()
{
  pid_t pid1,pid2;
  pid1=fork();

  if(pid1<0) /* 如果出错 */
    printf("error ocurred!\n");
  else if(pid1==0){ /* 如果是子进程 */
    printf("child process, pid:%d\n",getpid());
    sleep(5); /* 睡眠5秒钟 */
    exit(2);
  }
  else{ /* 如果是父进程 */
    int status = 0;
    pid2=waitpid(pid1, &status, WNOHANG); /* wait no hangup, 不阻塞 */
    while(0 == pid2)
    {
      sleep(1);
      printf("execute work...\n");
      pid2=waitpid(pid1, &status, WNOHANG);
    }
    if (pid2 == pid1)
      printf("get the child process, status:%d, wexitstatus:%d\n", status, WEXITSTATUS(status));
    else
      printf("error\n");
  }
  exit(0);
  return 0;
}

zjy@ubuntu:~/cpp$ ./main 

child process, pid:32417

execute work...

execute work...

execute work...

execute work...

execute work...

get the child process, status:512, wexitstatus:2

zjy@ubuntu:~/cpp






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值