您的位置:首页 > 大数据 > 人工智能

聊聊wait和waitpid

2017-10-24 22:38 232 查看
一句话总结: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  waitpid