父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS

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。

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

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

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void f(){
printf("THIS MESSAGE WAS SENT BY PARENT PROCESS..\n");
}

main(){
int i,childid,status=1,c=1;
signal(SIGUSR1,f); //setup the signal value
i=fork(); //better if it was: while((i=fork)==-1);
if (i){
printf("Parent: This is the PARENT ID == %d\n",getpid());
sleep(3);
printf("Parent: Sending signal..\n");
kill(i,SIGUSR1); //send the signal

//status is the return code of the child process
wait(&status);
printf("Parent is over..status == %d\n",status);

//WIFEXITED return non-zero if child exited normally 
printf("Parent: WIFEXITED(status) == %d\n",WIFEXITED(status));

//WEXITSTATUS get the return code
printf("Parent: The return code WEXITSTATUS(status) == %d\n",WEXITSTATUS(status));
} else {
printf("Child: This is the CHILD ID == %d\n",getpid());
while(c<5){
sleep(1);
printf("CHLID TIMER: %d\n",c);
c++;
}
printf("Child is over..\n");
exit(2);
}
}

输出:
Child: This is the CHILD ID == 8158
Parent: This is the PARENT ID == 8157
CHLID TIMER: 1
CHLID TIMER: 2
Parent: Sending signal..
THIS MESSAGE WAS SENT BY PARENT PROCESS..
CHLID TIMER: 3
CHLID TIMER: 4
Child is over..
Parent is over..status == 512
Parent: WIFEXITED(status) == 1 //正常退出
Parent: The return code WEXITSTATUS(status) == 2 //拿到子进程返回值
  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
进程中,可以使用`waitpid`函数来等待子进程的结束,然后重新启动一个新的子进程。具体的步骤如下: 1. 在进程中使用`fork`函数创建一个子进程。 2. 在子进程中使用`execl`函数执行需要运行的程序。 3. 在进程中使用`waitpid`函数等待子进程结束。 4. 如果子进程正常结束,则使用`WIFEXITED`宏判断是否正常退出;如果子进程被信号终止,则使用`WIFSIGNALED`宏判断是否被信号终止。 5. 如果子进程正常结束或者被信号终止,则在进程中重新使用`fork`函数创建一个新的子进程并执行需要运行的程序。 代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { pid_t pid; int status; while (1) { pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { execl("/path/to/program", "program", NULL); perror("execl"); exit(EXIT_FAILURE); } else { waitpid(pid, &status, 0); if (WIFEXITED(status)) { printf("Child process exited with status %d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("Child process terminated by signal %d\n", WTERMSIG(status)); } } } return 0; } ``` 在这个示例中,进程会不断地创建子进程等待子进程结束。如果子进程正常结束,则在进程中重新创建一个新的子进程并执行需要运行的程序。如果子进程被信号终止,则在进程中也重新创建一个新的子进程并执行需要运行的程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值