您的位置:首页 > 运维架构 > Linux

linux进程(三)之wait

2015-10-15 18:43 621 查看
         上一篇的博客中就有用到wait了,感觉wait还是有点重要的,得好好总结一下才可以。Linux中wait用法:系统中的僵尸进程都要由wait系统调用来回收。

函数原型:

#include<sys/types.h>

#include<sys/wait.h>

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 <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

int main(int argc,char *agrv[]){

pid_t pc,pr;

pc = fork();
if(pc < 0)
{
printf("create child process error:%s\n",strerror(errno));
exit(1);
}else if(pc == 0){

printf("I'am child process with pid%d\n",getpid());
sleep(3);
exit(0);
}else{

printf("now in parent process,pid=%d\n",getpid());
printf("I am waiting child process to exit\n");
pr = wait(NULL);
if(pr > 0 )/*子进程正常退出*/
{
printf("I catched a child process with pid of %d\n",pr);
}else{
printf("error:%s\n",strerror(errno));
}

}

exit(0);
}




可以明显注意到,在第2行结果打印出来前有3秒钟的等待时间,这就是我们设定的让子进程睡眠的时间,只有子进程从睡眠中苏醒过来,它才能正常退出,也就才能被父进程捕捉到。其实这里我们不管设定子进程睡眠的时间有多长,父进程都会一直等待下去。

如果参数status的值不是NULL,wait就会把子进程退出时的状态取出并存入其中, 这是一个整数值(int),指出了子进程是正常退出还是被非正常结束的,以及正常结束时的返回值,或被哪一个信号结束的等信息。由于这些信息 被存放在一个整数的不同二进制位中,所以用常规的方法读取会非常麻烦,人们就设计了一套专门的宏(macro)来完成这项工作,下面我们来学习一下其中最常用的两个:

1,WIFEXITED(status) 这个宏用来指出子进程是否为正常退出的,如果是,它会返回一个非零值。

(请注意,虽然名字一样,这里的参数status并不同于wait唯一的参数–指向整数的指针status,而是那个指针所指向的整数,切记不要搞混了。)

2, WEXITSTATUS(status) 当WIFEXITED返回非零值时,我们可以用这个宏来提取子进程的返回值,如果子进程调用exit(5)退出,WEXITSTATUS(status) 就会返回5;如果子进程调用exit(7),WEXITSTATUS(status)就会返回7。请注意,如果进程不是正常退出的,也就是说, WIFEXITED返回0,这个值就毫无意义。

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

int main()

{

int status;

pid_t pc, pr;

pc = fork();

if ( pc < 0) /* 如果出错 */

{

printf("error occured./n");

}

else if ( pc == 0 ) /* 子进程 */

{

printf("This is child process with pid of %d./n", getpid());

exit(3); /* 子进程返回3 */

}

else /* 父进程 */

{

pr = wait(&status);

if ( WIFEXITED(status) )   /* 如果WIFEXITED返回非零值 */

{

printf("The child process %d exit normally./n", pr);

printf("the return code is %d./n", WEXITSTATUS(status));

}

else /* 如果WIFEXITED返回零 */

{

printf("The child process %d exit abnormally./n", pr);

}

}

exit(0);

}




wait等待第一个终止的子进程,而waitpid则可以指定等待特定的子进程。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){

pid_t pid;
char *msg;
int k;
int exit_code;

printf("study how to get exit code\n");
pid = fork();
switch(pid){
case 0:
msg = "Child process is runing";
k = 5;
exit_code = 37;
break;
case -1:
perror("process creation is running\n");
exit(1);
default:
exit_code = 0;
break;
}
if (pid != 0) {
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);

printf("Child process has exited,pid = %d\n",child_pid);
if (WIFEXITED(stat_val))
printf("child exited with code %d\n",WEXITSTATUS(stat_val));
else
printf("child exited abnormally\n");

}else{
while(k-- > 0){
puts(msg);
sleep(1);
}
}

exit(exit_code);

}




父进程调用wait后被挂起,直到子进程结束,子进程正常结束后,wait函数返回刚刚结束运行的子进程pid,WEXITSTATUS获取子进程的退出码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: