父进程等待子进程退出

本文探讨了为何父进程需要等待子进程退出以防止僵尸进程的产生,介绍了wait()和waitpid()函数的区别,以及孤儿进程的概念。在非阻塞状态下,waitpid()无法获取退出状态码,而孤儿进程则会被init进程收养。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.为什么要等待子进程退出

父进程等待子进程退出 并收集子进程的退出状态。子进程退出状态不被收集,变成僵死进程(僵尸进程)。

 2.相关函数(wait()、waitpid())

 区别: wait使调用者阻塞,waitpid有一个选项,可以使调用者不阻塞。

 wait()代码实现:

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

int main (int argc, char *argv[])
{
        int cnt = 0;
        int status = 10;
        pid_t pid;

        pid = fork();

        if (pid > 0){
                wait(&status);
                printf("the status of the child exits is %d\n", WEXITSTATUS(status));   //通过WEXITSTATUS()可以解析出子进程退出状态码,也就是exit()括号内的数字
                while(1){
                        printf("cnt = %d\n", cnt);
                        printf("this is parent pid: %d\n", getpid());
                        sleep(1);
                }

        }
        else if(pid == 0){
                while(1){
                        printf("this is child pid: %d\n", getpid());
                        sleep(1);
                        cnt++;

                        if (cnt == 3){
                                exit(5);
                        }
                }
        }


        return 0;
}

 运行结果:

  waitpid()代码实现:

 

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

int main (int argc, char *argv[])
{
        int cnt = 0;
        int status = 10;
        pid_t pid;

        pid = fork();

        if (pid > 0){
                //pid_t wait(int *wstatus);
                //pid_t waitpid(pid_t pid, int *wstatus, int options);
                waitpid(pid, &status, WNOHANG);     //WNOHANG表示父进程不阻塞等待子进程退出再运行,父子进程一起竞争cpu
                printf("the status of exit is %d\n", WEXITSTATUS(status));
                while(1){
                        printf("cnt = %d\n", cnt);
                        printf("this is parent pid: %d\n", getpid());
                        sleep(1);
                }

        }
        else if(pid == 0){
                while(1){
                        printf("this is child pid: %d\n", getpid());
                        sleep(1);
                        cnt++;

                        if (cnt == 3){
                                exit(5);
                        }
                }
        }


        return 0;
}

 运行结果:

 可以看出,在非阻塞的状态下,是无法解析出退出状态码,父子进程存在竞争关系

status参数: 是一个整型数指针

非空: 子进程退出状态放在它所指向的地址中。

空: 不关心退出状态。

 

 3.孤儿进程

父进程如果不等待子进程退出,在子进程之前就结束了自己的“生命”,此时子进程叫做孤儿进程。

Linux避免系统存在过多孤儿进程,init进程收留孤儿进程,变成孤儿进程的父进程。

代码实现:

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

int main (int argc, char *argv[])
{
        pid_t pid;

        pid = fork();

        if (pid > 0){
                printf("this is parent pid: %d\n", getpid());   //在子进程结束前,父进程执行一次后结束进程,这是子进程就是孤儿进程了

        }
        else if(pid == 0){
                while(1){
                        printf("this is child pid: %d\n", getpid());
                        sleep(1);
                }
        }

        return 0;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值