linux的僵尸进程和孤儿进程及解决方法

本文详细解释了僵尸进程和孤儿进程的概念,展示了如何通过代码创建这两种进程,并提供了使用wait()函数处理僵尸进程的方法。

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

1.僵尸进程:父进程还在执行,子进程已经结束,子进程依然占用部分资源,这时的子进程就是僵尸进程。

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

int main()
{
        pid_t pid;
        pid = fork();
        if(pid < 0)
        {
                fprintf(stderr, "fork error %s\n", strerror(errno));
                exit(1);
        }
        else if(pid > 0)//parent process
        {
                while(1)
                {
                        sleep(3);
                }
                printf("pid: %d, ppid: %d\n", getpid(), getppid());
        }
        else
        {
                printf("pid: %d, ppid: %d\n", getpid(), getppid());
                exit(1);
        }
        return 0;
}

执行结果:

[root@localhost process]# ./03process 
pid: 3384, ppid: 3383

这时打开另一个终端查看进程状态:

[root@localhost Desktop]# ps -ef | grep 03process
root      3383  2718  0 08:34 pts/0    00:00:00 ./03process
root      3384  3383  0 08:34 pts/0    00:00:00 [03process] <defunct>
root      5445  3265  1 08:35 pts/1    00:00:00 grep 03process

此时,3383是父进程,3384是子进程(僵尸进程),只能通过杀死父进程来杀死僵尸进程:

[root@localhost Desktop]# kill -9 3383
[root@localhost Desktop]# ps -ef | grep 03process
root      5455  3265  2 08:37 pts/1    00:00:00 grep 03process

杀死父进程后查看执行终端:

[root@localhost process]# ./03process 
pid: 3384, ppid: 3383
Killed

通过wait()函数来处理僵尸进程(最合理的方式):

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

int main()
{
        pid_t pid;
        pid = fork();
        if(pid < 0)
        {
                fprintf(stderr, "fork error %s\n", strerror(errno));
                exit(1);
        }
        else if(pid > 0)//parent process
        {
                wait(0);
                while(1)
                {
                        sleep(3);
                }
                printf("pid: %d, ppid: %d\n", getpid(), getppid());
        }
        else
        {
                printf("pid: %d, ppid: %d\n", getpid(), getppid());
                exit(1);
        }
        return 0;
}

上面程序中的wait(0)会感知子进程一旦执行完毕,就替它回收资源,从而不会产生僵尸进程。


2.孤儿进程:父进程退出,它的子进程还在运行,那么这些子进程就是孤儿进程,孤儿进程被init进程(1号进程)所收养,并由init进程负责他们的退出时清理工作。

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


int main()
{
        pid_t pid;
        pid = fork();
        if(pid < 0)
        {
                fprintf(stderr, "fork error %s\n", strerror(errno));
                exit(1);
        }
        else if(pid > 0)//parent process
        {
                sleep(1);
                printf("pid: %d, ppid: %d\n", getpid(), getppid());
                exit(1);//parent process exit
        }
        else
        {
                sleep(3);
                printf("pid: %d, ppid: %d\n", getpid(), getppid());
        }
        printf("pid: %d, ppid: %d\n", getpid(), getppid());
        return 0;
}

执行结果:

[root@localhost process]# ./03process 
pid: 5573, ppid: 5572
pid: 5572, ppid: 2718
[root@localhost process]# pid: 5573, ppid: 1

可以看到父进程先结束,子进程成为孤儿进程继续执行,主进程退出,孤儿进程交由1号进程,他负责回收孤儿进程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值