linux内核对孤儿进程处理,Linux下的僵尸进程和孤儿进程分析 | 术与道的分享

本篇就linux下两个负能量进程进行分析,僵尸与孤儿从表面上听起来很简单,但是在我们的操作系统上,是否还是一成不变的两种状态呢?下面我就具体为大家阐述僵尸与孤儿的由来与分析。

僵尸进程与孤儿进程

首先应该明确的是:僵尸进程和孤儿进程都是父进程不调用wait时子进程可能的两种状态。

僵尸进程(Zombies):指子进程结束运行(exit(0)),系统将会保留子进程的描述符信息(没有被回收,其他进程不可以重用该描述符),而父进程没有调用wait对退出的子进程进行处理,此时子进程称为了僵尸进程。

孤儿进程:父进程没有调用wait等待子进程退出。父进程结束后,子进程仍然在运行,此时子进程称为孤儿进程。孤儿进程会被init进程收养,即将init作为自己的父进程,此时所有的清除操作将交给init进程(init进程循环调用wait)。因此,孤儿进程是无害的。

像上面的那些解释,有些词汇不太容易理解,我简单总结了两句:(父进程是爹,子进程是孩子)

僵尸进程:爹在,孩子也在,但是爹不管孩子了,因为有爹不能被收养,此时孩子就是僵尸进程。

孤儿进程:爹死了,孩子还在,孩子被(init)收养,此时孩子就是孤儿进程。

僵尸进程测试

我们让子进程直接退出,父进程运行5秒后退出,且父进程sleep之后未调用wait或waitpid获取子进程的状态信息。

#include

#include

#include

#include

int main()

{

pid_t pid;

pid = fork();

if(pid < 0)//进程创建失败

{

printf("fork error:%d\n",errno);

exit(1);

}

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

{

printf("Child Process-> pid:%d ppid:%d\n",getpid(),getppid());

exit(0);

}

else//父进程

{

printf("Father Process-> pid:%d\n",getpid());

sleep(5);

}

system("ps -o pid,ppid,state,tty,command");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

#include

#include

#include

#include

intmain()

{

pid_tpid;

pid=fork();

if(pid<0)//进程创建失败

{

printf("fork error:%d\n",errno);

exit(1);

}

elseif(pid==0)//子进程

{

printf("Child  Process-> pid:%d   ppid:%d\n",getpid(),getppid());

exit(0);

}

else//父进程

{

printf("Father Process-> pid:%d\n",getpid());

sleep(5);

}

system("ps -o pid,ppid,state,tty,command");

return0;

}

@PL8@D21I0EVUQZ24SFB.png

jsjc.png

我们发现,子进程的pid是2898,其状态为Z状态,即Zombies,成为僵尸进程。

僵尸进程的安全性

在每个进程退出的时候,内核释放该进程所有的资源,包括打开的文件,占用的内存等。但是仍然为其保留一定的信息(包括进程号the process ID,退出状态the termination status of the process,运行时间the amount of CPU time taken by the process等)。

直到父进程通过wait / waitpid来取时才释放. 但这样就导致了问题,如果进程不调用wait / waitpid的话,那么保留的那段信息就不会释放,其进程号就会一直被占用,但是系统所能使用的进程号是有限的,如果大量的产生僵死进程,将因为没有可用的进程号而导致系统不能产生新的进程。

僵尸进程的解决方法

1> 子进程退出时像父进程发送SIGCHILD信号,在信号处理函数中调用wait进行处理僵尸进程。

#include

#include

#include

#include

static void sig_child(int sign);

int main()

{

pid_t pid;

signal(SIGCHLD,sig_child);//创建捕捉子进程退出信号

pid = fork();

if(pid < 0)//创建进程失败

{

printf("fork error:%d\n",errno);

exit(1);

}

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

{

printf("Child Process-> pid:%d ppid:%d\n",getpid(),getppid());

exit(0);

}

else//父进程

{

printf("Father Process-> pid:%d\n",getpid());

sleep(3);

}

system("ps -o pid,ppid,state,tty,command");

return 0;

}

static void sig_child(int sign)

{

pid_t pid;

int stat;

//处理僵尸进程

while((pid = waitpid(-1,&stat,WNOHANG)) > 0)

printf("Child:%d\n",pid);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

#include

#include

#include

#include

staticvoidsig_child(intsign);

intmain()

{

pid_tpid;

signal(SIGCHLD,sig_child);//创建捕捉子进程退出信号

pid=fork();

if(pid<0)//创建进程失败

{

printf("fork error:%d\n",errno);

exit(1);

}

elseif(pid==0)//子进程

{

printf("Child Process->  pid:%d ppid:%d\n",getpid(),getppid());

exit(0);

}

else//父进程

{

printf("Father Process-> pid:%d\n",getpid());

sleep(3);

}

system("ps -o pid,ppid,state,tty,command");

return0;

}

staticvoidsig_child(intsign)

{

pid_tpid;

intstat;

//处理僵尸进程

while((pid=waitpid(-1,&stat,WNOHANG))>0)

printf("Child:%d\n",pid);

}

cljsjc.png

2> fork两次:父进程fork一个子进程,然后继续工作,子进程fork一 个孙进程后退出,那么孙进程被init接管,孙进程结束后,init会回收。

#include

#include

#include

#include

int main()

{

pid_t pid;

pid = fork();

if(pid < 0)//进程创建失败

{

printf("fork error:%d\n",errno);

exit(1);

}

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

{

//第一个子进程

printf("Child Process(1)-> pid:%d ppid:%d\n",getpid(),getppid());

pid = fork();//第二个子进程

if(pid < 0)

{

printf("fork error:%d\n",errno);

exit(1);

}

else if(pid > 0)

{

printf("first process is exited\n");

exit(0);

}

sleep(3);//第一个子进程退出

printf("Child Process(2)-> pid:%d ppid:%d\n",getpid(),getppid());

exit(0);

}

if(waitpid(pid,NULL,0) != pid)

{

printf("waitpid error\n");

exit(1);

}

system("ps -o pid,ppid,state");

exit(0);//父进程处理第一个子进程

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

#include

#include

#include

#include

intmain()

{

pid_tpid;

pid=fork();

if(pid<0)//进程创建失败

{

printf("fork error:%d\n",errno);

exit(1);

}

elseif(pid==0)//子进程

{

//第一个子进程

printf("Child Process(1)->  pid:%d  ppid:%d\n",getpid(),getppid());

pid=fork();//第二个子进程

if(pid<0)

{

printf("fork error:%d\n",errno);

exit(1);

}

elseif(pid>0)

{

printf("first process is exited\n");

exit(0);

}

sleep(3);//第一个子进程退出

printf("Child Process(2)->  pid:%d  ppid:%d\n",getpid(),getppid());

exit(0);

}

if(waitpid(pid,NULL,0)!=pid)

{

printf("waitpid error\n");

exit(1);

}

system("ps -o pid,ppid,state");

exit(0);//父进程处理第一个子进程

return0;

}

fork2.png

孤儿进程测试

测试孤儿进程时,让父进程运行3秒后退出,子进程运行6s,pid为3097的子进程的ppid变成了1,即被init进程(pid=1)所领养,即init成为当前子进程(孤儿进程)的父进程。

#include

#include

#include

#include

int main()

{

pid_t pid;

pid = fork();

if(pid < 0)

{

printf("fork error:%d\n",errno);

}

else if(pid == 0)

{

printf("Child Process-> pid:%d ppid:%d\n",getpid(),getppid());

sleep(6);

printf("Child Process-> pid:%d ppid:%d\n",getpid(),getppid());

}

else

{

printf("Father Process-> pid:%d\n",getpid());

sleep(3);

exit(0);

}

system("ps -o pid,ppid,state,tty,command");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#include

#include

#include

#include

intmain()

{

pid_tpid;

pid=fork();

if(pid<0)

{

printf("fork error:%d\n",errno);

}

elseif(pid==0)

{

printf("Child Process->  pid:%d  ppid:%d\n",getpid(),getppid());

sleep(6);

printf("Child Process->  pid:%d  ppid:%d\n",getpid(),getppid());

}

else

{

printf("Father Process-> pid:%d\n",getpid());

sleep(3);

exit(0);

}

system("ps -o pid,ppid,state,tty,command");

return0;

}

gejc.png

父进程退出后,子进程的ppid由3096转换成1,即子进程的父进程发生改变,会被init进程收养,即将init作为自己的父进程,此时所有的清除操作将交给init进程。因此,孤儿进程是无害的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值