僵尸进程(Zombie Process)
进程已运行结束,但进程的占用的资源未被回收,这样的进程称为僵尸进程。
在每个进程退出的时候,内核释放该进程所有的资源、包括打开的文件、占用的内存等。 但是仍然为其保留一定的信息,这些信息主要主要指进程控制块的信息(包括进程号、退出状态、运行时间等)。直到父进程通过 wait() 或 waitpid() 来获取其状态并释放(具体用法,请看《等待进程结束》)。 这样就会导致一个问题,如果进程不调用wait() 或 waitpid() 的话, 那么保留的那段信息就不会释放,其进程号就会一直被占用,但是系统所能使用的进程号是有限的,如果大量的产生僵死进程,将因为没有可用的进程号而导致系统不能产生新的进程.此即为僵尸进程的危害,应当避免。
子进程已运行结束,父进程未调用 wait() 或 waitpid() 函数回收子进程的资源是子进程变为僵尸进程的原因。
僵尸进程测试程序如下:
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
-
- int main(int argc, char *argv[])
- {
- pid_t pid;
- pid = fork();
-
- if( pid < 0 ){
- perror("fork error:");
- exit(1);
- }else if( 0 == pid ){
-
- printf("I am child process.I am exiting.\n");
- printf("[son id]: %d\n", getpid() );
-
- exit(0);
- }else if( pid > 0){
-
- sleep(1);
- printf("I am father process.I will sleep two seconds\n");
- printf("[father id]: %d\n", getpid() );
-
- while(1);
- }
-
- return 0;
- }
我们在一个终端运行以上程序:
![](https://img-blog.csdn.net/20150603140211408)
在终端敲:ps -ef | grep defunct ,后面尖括号里是 defunct 的都是僵尸进程。
我们另启一个终端,查看进程的状态,有哪些是僵尸进程:
![](https://img-blog.csdn.net/20150525114301311)
或者:
![](https://img-blog.csdn.net/20150525115308816)
如何避免僵尸进程?
1)最简单的方法,父进程通过 wait() 和 waitpid() 等函数等待子进程结束,但是,这会导致父进程挂起。具体用法,请看《进程的控制:结束进程、等待进程结束》。
2)如果父进程要处理的事情很多,不能够挂起,通过 signal() 函数人为处理信号 SIGCHLD , 只要有子进程退出自动调用指定好的回调函数,因为子进程结束后, 父进程会收到该信号 SIGCHLD ,可以在其回调函数里调用 wait() 或 waitpid() 回收。关于信号的更详细用法,请看《信号中断处理》。
测试代码如下:
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <signal.h>
-
- void sig_child(int signo)
- {
- pid_t pid;
-
-
- while( (pid = waitpid(-1, NULL, WNOHANG)) > 0 ){
- printf("child %d terminated.\n", pid);
- }
- }
-
- int main()
- {
- pid_t pid;
-
-
-
- signal(SIGCHLD, sig_child);
-
- pid = fork();
-
- if (pid < 0){
- perror("fork error:");
- exit(1);
- }else if(pid == 0){
- printf("I am child process,pid id %d.I am exiting.\n",getpid());
- exit(0);
-
- }else if(pid > 0){
- sleep(2);
- printf("I am father, i am exited\n\n");
- system("ps -ef | grep defunct");
-
- }
-
- return 0;
- }
运行结果如下:
![](https://img-blog.csdn.net/20150525135437015)
3)如果父进程不关心子进程什么时候结束,那么可以用signal(SIGCHLD, SIG_IGN)通知内核,自己对子进程的结束不感兴趣,父进程忽略此信号,那么子进程结束后,内核会回收, 并不再给父进程发送信号。关于信号的更详细用法,请看《信号中断处理》。
测试代码如下:
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <signal.h>
-
- int main()
- {
- pid_t pid;
-
-
-
- signal(SIGCHLD, SIG_IGN);
-
- pid = fork();
-
- if (pid < 0){
- perror("fork error:");
- exit(1);
- }else if(pid == 0){
- printf("I am child process,pid id %d.I am exiting.\n",getpid());
- exit(0);
-
- }else if(pid > 0){
- sleep(2);
- printf("I am father, i am exited\n\n");
- system("ps -ef | grep defunct");
-
- }
-
- return 0;
- }
运行结果如下:
![](https://img-blog.csdn.net/20150525135831508)
4)还有一些技巧,就是 fork() 两次,父进程 fork() 一个子进程,然后继续工作,子进程 fork() 一 个孙进程后退出,那么孙进程被 init 接管,孙进程结束后,init (1 号进程)会回收。不过子进程的回收还要自己做。《UNIX环境高级编程》8.6节说的非常详细。原理是将子进程成为孤儿进程,从而其的父进程变为 init 进程(1 号进程),通过 init 进程(1 号进程)可以处理僵尸进程。更多详情,请看《特殊进程之孤儿进程》。
测试程序如下所示:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
-
- int main()
- {
- pid_t pid;
-
- pid = fork();
-
- if (pid < 0){
- perror("fork error:");
- exit(1);
- }else if (pid == 0){
-
-
- printf("I am the first child process.pid:%d\tppid:%d\n",getpid(),getppid());
- pid = fork();
- if (pid < 0){
- perror("fork error:");
- exit(1);
- }else if(pid == 0){
-
- sleep(3);
- printf("I am the second child process.pid: %d\tppid:%d\n",getpid(),getppid());
- exit(0);
-
- }else if (pid >0){
- printf("first procee is exited.\n");
- exit(0);
- }
-
- }else if(pid > 0){
-
-
- if (waitpid(pid, NULL, 0) != pid){
- perror("waitepid error:");
- exit(1);
- }
-
- exit(0);
- }
-
- return 0;
- }
运行结果如下:
![](https://img-blog.csdn.net/20150525141425169)
转载自http://blog.csdn.net/tennysonsky