linux孤儿进程回收,Linux-孤儿进程与僵尸进程

 孤儿进程的概念:

若子进程的父进程已经死掉,而子进程还存活着,这个进程就成了孤儿进程。

 为了保证每个进程都有一个父进程,孤儿进程会被init进程领养,init进程成为了孤儿进程的养父进程,当孤儿进程退出之后,由init进程完成对孤儿进程的回收。

 模拟孤儿进程的案例

编写模拟孤儿进程的代码讲解孤儿进程,验证孤儿进程的父进程是否由原来的父进程变成了init进程。

孤儿进程: 父进程先退出, 子进程就变成了孤儿进程, 此时被init进程领养,

当孤儿进程退出之后, 就会被init进程回收.

//模拟孤儿进程 函数测试

#include

#include

#include

#include

#include

int main()

{

//创建子进程

//pid_t fork(void);

pid_t pid = fork();

if(pid<0)

{

perror("fork error");

return -1;

}

else if(pid>0)//父进程

{

/*

man getpid for help

*/

sleep(5);

printf("father: pid==[%d] fpid==[%d]\n",getpid(),getppid());

}

else if(pid == 0)//子进程

{

printf("child: pid==[%d] fpid==[%d]\n",getpid(),getppid());

sleep(10);

printf("child: pid==[%d] fpid==[%d]\n",getpid(),getppid());

}

return 0;

}

$ gcc -g -Wall orphan.c -o orphan

$ ./orphan

child: pid==[95035] fpid==[95034]

father: pid==[95034] fpid==[91160]

child: pid==[95035] fpid==[1] //被init进程回收

僵尸进程的概念:

若子进程死了,父进程还活着, 但是父进程没有调用wait或waitpid函数完成对子进程的回收,则该子进程就成了僵尸进程。

 如何解决僵尸进程

 由于僵尸进程是一个已经死亡的进程,所以不能使用kill命令将其杀死

 通过杀死其父进程的方法可以消除僵尸进程。

杀死其父进程后,这个僵尸进程会被init进程领养,由init进程完成对僵尸进程的回收。

 模拟僵尸进程的案例

编写模拟僵尸进程的代码讲解僵尸进程, 验证若子进程先于父进程退出, 而父进程没有调用wait或者waitpid函数进行回收, 从而使子进程成为了僵尸进程.

//模拟僵尸进程 函数测试

#include

#include

#include

#include

#include

int main()

{

//创建子进程

//pid_t fork(void);

pid_t pid = fork();

if(pid<0)

{

perror("fork error");

return -1;

}

else if(pid>0)//父进程

{

/*

man getpid for help

*/

sleep(20);//sleep 让子进程先结束

printf("father: pid==[%d] fpid==[%d]\n",getpid(),getppid());

}

else if(pid == 0)//子进程

{

printf("child: pid==[%d] fpid==[%d]\n",getpid(),getppid());

}

return 0;

}

$ ps -ef | grep zombie

wym 95932 95707 0 23:29 pts/6 00:00:00 grep --color=auto zombie

$ ps -ef | grep zombie

wym 96155 91160 0 23:29 pts/5 00:00:00 ./zombie

wym 96156 96155 0 23:29 pts/5 00:00:00 [zombie]

#defunct 表明是僵尸进程,无法接受信号,kill -9 也无法杀死

#如何解决僵尸进程:

应该使用杀死僵尸进程父进程的方法来解决僵尸进程

解决僵尸进程的更好方法是使用进程回收函数

进程回收函数

 wait函数

 函数原型:

pid_t wait(int *status);

 函数作用:

 阻塞并等待子进程退出

 回收子进程残留资源

 获取子进程结束状态(退出原因)。

 返回值:

 成功:清理掉的子进程ID;

 失败:-1 (没有子进程)

 status参数:子进程的退出状态 – 传出参数

 WIFEXITED(status):为非0 → 进程正常结束

WEXITSTATUS(status):获取进程退出状态

 WIFSIGNALED(status):为非0 → 进程异常终止

WTERMSIG(status):取得进程终止的信号编号。

 wait函数练习

使用wait函数完成父进程对子进程的回收

//父进程调用wait完成对子进程地回收 函数测试

#include

#include

#include

#include

#include/*for wait*/

#include

int main()

{

//创建子进程

//pid_t fork(void);

pid_t pid = fork();

if(pid<0)

{

perror("fork error");

return -1;

}

else if(pid>0)//父进程

{

printf("father: pid==[%d] fpid==[%d]\n",getpid(),getppid());

int status = 0;

pid_t wpid = wait(&status);

printf("wpid==[%d]\n",wpid);

if(WIFEXITED(status))//正常退出

{

printf("child normal exit,status==[%d]\n",WEXITSTATUS(status));

}else if(WIFSIGNALED(status))//被信号杀死

{

printf("child killed by signal,status==[%d]\n",WTERMSIG(status));

}

}

else if(pid == 0)//子进程

{

printf("child: pid==[%d] fpid==[%d]\n",getpid(),getppid());

sleep(20);

/*

sleep 20 可以测试被信号 15(Terminated) 杀死

$ ps -ef | grep wait

wym 111409 91160 0 00:04 pts/5 00:00:00 ./wait

wym 111410 111409 0 00:04 pts/5 00:00:00 ./wait

wym 111436 95707 0 00:04 pts/6 00:00:00 grep --color=auto wait

$ kill -15 111409

$ ./wait

father: pid==[111409] fpid==[91160]

child: pid==[111410] fpid==[111409]

Terminated

*/

return 9;

/*

* 不使用kill 信号

$ ./wait

father: pid==[112977] fpid==[91160]

child: pid==[112978] fpid==[112977]

wpid==[112978]

child normal exit,status==[9]

*

*/

}

return 0;

}

 waitpid函数

 函数原型:

pid_t waitpid(pid_t pid, int *status, in options);

 函数作用

同wait函数

 函数参数

参数:

pid:

pid = -1 等待任一子进程。

pid > 0 等待其进程ID与pid相等的子进程。与wait等效。

pid = 0 等待进程组ID与目前进程相同的任何子进程,也就是说任何和调用

waitpid()函数的进程在同一个进程组的进程。

pid < -1 等待其组ID等于pid的绝对值的任一子进程。(适用于子进程在其他组的情况)

status: 子进程的退出状态,用法同wait函数。

options:设置为WNOHANG,函数非阻塞,设置为0,函数阻塞。

 函数返回值

0:返回回收掉的子进程ID;

-1:无子进程

=0:参3为WNOHANG,且子进程正在运行。

 waitpid函数练习

使用waitpid函数完成对子进程的回收

//父进程调用waitpid完成对子进程地回收 函数测试

#include

#include

#include

#include

#include/*for wait*/

#include

int main()

{

//创建子进程

//pid_t fork(void);

pid_t pid = fork();

if(pid<0)

{

perror("fork error");

return -1;

}

else if(pid>0)//父进程

{

printf("father: pid==[%d] fpid==[%d]\n",getpid(),getppid());

int status = 0;

//pid_t wpid = waitpid(pid,&status,0);//same to pid_t wpid = wait(&status);

while(1)//循环等待子进程退出

{

pid_t wpid = waitpid(-1,&status,WNOHANG);//等待任一子进程,WNOHANG不阻塞

printf("wpid==[%d]\n",wpid);

if(wpid>0)

{

if(WIFEXITED(status))//正常退出

{

printf("child normal exit,status==[%d]\n",WEXITSTATUS(status));

}else if(WIFSIGNALED(status))//被信号杀死

{

printf("child killed by signal,status==[%d]\n",WTERMSIG(status));

}

}

else if(wpid==0)//子进程还活着

{

printf("child is living,wpid==[%d]\n",wpid);

}

else if(wpid==-1)//没有子进程

{

printf("no child is living,wpid==[%d]\n",wpid);

break;

}

}

}

else if(pid == 0)//子进程

{

printf("child: pid==[%d] fpid==[%d]\n",getpid(),getppid());

sleep(2);

return 9;

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值