先抄录网上一段对僵死进程的描述:



僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中。这种进程称之为僵死进程。在每个进程退出的时候,内核释放该进程所有的资源,包括打开的文件,占用的内存等。 但是仍然为其保留一定的信息(包括进程号the process ID,退出状态the termination status of the process,运行时间the amount of CPU time taken by the process等)。直到父进程通过wait
/ waitpid来取时才释放。 但这样就导致了问题,如果进程不调用wait / waitpid的话, 那么保留的那段信息就不会释放,其进程号就会一直被占用,但是系统所能使用的进程号是有限的,如果大量的产生僵死进程,将因为没有可用的进程号而导致系统不能产生新的进程. 此即为僵尸进程的危害,应当避免。



从系统角度来说,处理僵尸进程有两种方法:



1 找到僵死进程的父进程,kill掉父进程,那么僵死进程将变为孤儿进程,孤儿进程在系统中由init进程接管,init进程将回收僵死进程的资源

2 reboot系统,因为僵死进程是不可以被kill掉

如下测试:

[root@limt ~]# ps -ef|grep 21165
root 21165 7459 0 05:51 pts/1 00:00:00 ./a.out
root 21166 21165 0 05:51 pts/1 00:00:00 [a.out] <defunct>
root 21190 8866 0 05:52 pts/3 00:00:00 grep 21165
[root@limt ~]# kill 21165
[root@limt ~]#
[root@limt ~]#
[root@limt ~]# ps -ef|grep 21165 杀掉父进程后,子进程也消失
root 21196 8866 0 05:52 pts/3 00:00:00 grep 21165
[root@limt ~]# ps -ef|grep 16704
root 16704 16703 0 04:06 pts/1 00:00:00 [a.out] <defunct>
root 16719 8866 0 04:06 pts/3 00:00:00 grep 16704
[root@limt ~]#
[root@limt ~]#
[root@limt ~]# kill -9 16704
[root@limt ~]#
[root@limt ~]# ps -ef|grep 16704
root 16704 16703 0 04:06 pts/1 00:00:00 [a.out] <defunct>
root 16725 8866 0 04:06 pts/3 00:00:00 grep 16704
</pre><p></p><p>从开发角度,有两种方法来避免僵死进程:1 通过signal函数处理,因为每一个子进程退出都会向父进程发生一个SIGCHILD信号2 通过fork两次子进程实现,使子进程的父进程为init进程(),大多数守护进程就是这样实现的简单程序演示产生僵死进程的过程:</p><pre code_snippet_id="554626" snippet_file_name="blog_20141217_3_6668512" name="code" class="cpp">一 父进程fork一个子进程然后不使用waitpid函数,直接退出,而子进程sleep 120秒后退出

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> int main(int argc, char *argv[])
{
pid_t pid; pid = fork();
if (pid == 0) { int iPid = (int)getpid();
fprintf(stderr,"I am child,%d\n",iPid);
sleep(120);
fprintf(stderr, "Child exits\n"); return EXIT_SUCCESS;
}
int iPid = (int)getpid();
fprintf(stderr,"I am parent,%d\n",iPid);
fprintf(stderr, "parent exits\n");
return EXIT_SUCCESS;
} [root@limt ~]# gcc Zom.c
[root@limt ~]# ./a.out [root@limt ~]# ./a.out
I am parent,15019
parent exits
I am child,15020 从输出看父进程先退出,然后子进程运行sleep函数 [root@limt ~]# ps -ef|grep 15019 //查看父进程
root 15046 8866 0 03:29 pts/3 00:00:00 grep 15019
[root@limt ~]#
[root@limt ~]# ps -ef|grep 15020 //查看子进程
root 15020 1 0 03:29 pts/1 00:00:00 ./a.out
root 15056 8866 0 03:29 pts/3 00:00:00 grep 15020 从ps看父进程已经销毁,子进程的父进程号为1,也就是init进程 二 父进程fork一个子进程然后使用waitpid函数,然后退出,而子进程sleep 120秒后退出 #include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> int main(int argc, char *argv[])
{
pid_t pid; pid = fork();
if (pid == 0) { int iPid = (int)getpid();
fprintf(stderr,"I am child,%d\n",iPid);
sleep(120);
fprintf(stderr, "Child exits\n"); return EXIT_SUCCESS;
}
int iPid = (int)getpid();
fprintf(stderr,"I am parent,%d\n",iPid);
waitpid(pid,NULL,0);
fprintf(stderr, "parent exits\n");
return EXIT_SUCCESS;
} [root@limt ~]# gcc Zom.c
[root@limt ~]# ./a.out
I am parent,15187
I am child,15188
Child exits
parent exits 从输出看子进程先退出,父进程一直等待子进程退出后才退出 [root@limt ~]# ps -ef|grep 15187 //查看父进程
root 15187 7459 0 03:32 pts/1 00:00:00 ./a.out
root 15188 15187 0 03:32 pts/1 00:00:00 ./a.out
root 15197 8866 0 03:33 pts/3 00:00:00 grep 15187
[root@limt ~]# ps -ef|grep 15188 //查看子进程
root 15188 15187 0 03:32 pts/1 00:00:00 ./a.out
root 15207 8866 0 03:33 pts/3 00:00:00 grep 15188 三 父进程fork一个子进程,然后使用waitpid函数,最后使用sleep函数,而子进程sleep 40秒后退出
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> int main(int argc, char *argv[])
{
pid_t pid; pid = fork();
if (pid == 0) { int iPid = (int)getpid();
fprintf(stderr,"I am child,%d\n",iPid);
sleep(40);
fprintf(stderr, "Child exits\n"); return EXIT_SUCCESS;
}
int iPid = (int)getpid();
fprintf(stderr,"I am parent,%d\n",iPid);
waitpid(pid,NULL,0);
fprintf(stderr, "sleep....\n");
sleep(120);
fprintf(stderr, "parent exits\n");
return EXIT_SUCCESS;
} [root@limt ~]# ./a.out
I am parent,15673
I am child,15674
Child exits //等待40秒后输出
sleep.... //等待40秒后输出
parent exits 从上面的输出看sleep函数没有立刻执行,而是等待子进程运行完成才执行,实际是在等待waitpid函数返回 [root@limt ~]# ps -ef|grep 15673 //程序运行40秒内
root 15673 7459 0 03:43 pts/1 00:00:00 ./a.out
root 15674 15673 0 03:43 pts/1 00:00:00 ./a.out
root 15681 8866 0 03:44 pts/3 00:00:00 grep 15673
[root@limt ~]# ps -ef|grep 15674 //程序运行40秒内
root 15674 15673 0 03:43 pts/1 00:00:00 ./a.out
root 15692 8866 0 03:44 pts/3 00:00:00 grep 15674
[root@limt ~]#
[root@limt ~]# ps -ef|grep 15673 //程序运行40秒外
root 15673 7459 0 03:43 pts/1 00:00:00 ./a.out
root 15725 8866 0 03:44 pts/3 00:00:00 grep 15673
[root@limt ~]# ps -ef|grep 15674 //程序运行40秒外,子进程已经销毁
root 15727 8866 0 03:44 pts/3 00:00:00 grep 15674
[root@limt ~]#
[root@limt ~]# ps -ef|grep 15673 //程序运行120秒外
root 15798 8866 0 03:46 pts/3 00:00:00 grep 15673 四 父进程fork一个子进程,不使用waitpid函数然,而是sleep 120秒,而子进程sleep 1秒后退出 #include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> int main(int argc, char *argv[])
{
pid_t pid; pid = fork();
if (pid == 0) { int iPid = (int)getpid();
fprintf(stderr,"I am child,%d\n",iPid);
sleep(1);
fprintf(stderr, "Child exits\n"); return EXIT_SUCCESS;
}
int iPid = (int)getpid();
fprintf(stderr,"I am parent,%d\n",iPid);
fprintf(stderr, "sleep....\n");
sleep(120);
fprintf(stderr, "parent exits\n");
return EXIT_SUCCESS;
} [root@limt ~]# ./a.out
I am parent,16026
sleep....
I am child,16027
Child exits
parent exits 从上面输出看子进程先于父进程退出 [root@limt ~]# ps -ef|grep 16026 //查看父进程
root 16026 7459 0 03:51 pts/1 00:00:00 ./a.out
root 16027 16026 0 03:51 pts/1 00:00:00 [a.out] <defunct>
root 16039 8866 0 03:51 pts/3 00:00:00 grep 16026
[root@limt ~]#
[root@limt ~]# ps -ef|grep 16027 //查看子进程,子进程处于僵死状态
root 16027 16026 0 03:51 pts/1 00:00:00 [a.out] <defunct>
root 16046 8866 0 03:51 pts/3 00:00:00 grep 16027 [root@limt ~]# top //top 进程显示存在一个僵死进程 top - 04:02:20 up 2:46, 4 users, load average: 0.21, 0.15, 0.15
Tasks: 280 total, 1 running, 278 sleeping, 0 stopped, 1 zombie
Cpu(s): 1.2%us, 0.6%sy, 0.0%ni, 97.4%id, 0.7%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 2050752k total, 1819480k used, 231272k free, 141876k buffers
Swap: 6291448k total, 0k used, 6291448k free, 775368k cached [root@limt ~]# ps -ef|grep 16027 //120秒后僵死进程消失
root 16513 8866 0 04:01 pts/3 00:00:00 grep 16027

C程序演示产生僵死进程的过程的更多相关文章

  1. 【Linux】进程的结构,创建,结束,以及程序转化为的进程的过程

    本文内容: 1.进程的结构 2.程序转化为进程的过程 3.进程的创建 4.进程的结束 背景知识: 1.进程是计算机中处于运行的程序的实体 2.进程是线程的容器 3.程序本身只是指令,数据以及组织形式的 ...

  2. Android应用程序进程启动过程的源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址: http://blog.csdn.net/luoshengyang/article/details/6747696 Android 应用程序框架层创 ...

  3. Android应用程序进程启动过程(后篇)

    前言 在前篇中我们讲到了Android应用程序进程启动过程,这一篇我们来讲遗留的知识点:在应用程序进程创建过程中会启动Binder线程池以及在应用程序进程启动后会创建消息循环. 1.Binder线程池 ...

  4. Android应用程序进程启动过程(前篇)

    在此前我讲过Android系统的启动流程,系统启动后,我们就比较关心应用程序是如何启动的,这一篇我们来一起学习Android7.0 应用程序进程启动过程,需要注意的是“应用程序进程启动过程”,而不是应 ...

  5. 【UNIX网络编程(三)】TCP客户/server程序演示样例

    上一节给出了TCP网络编程的函数.这一节使用那些基本函数编写一个完毕的TCP客户/server程序演示样例. 该样例运行的过程例如以下: 1.客户从标准输入读入一行文本,并写给server. 2.se ...

  6. C/C++程序从编译到链接的过程

    编译器是一个神奇的东西,它能够将我们所编写的高级语言源代码翻译成机器可识别的语言(二进制代码),并让程序按照我们的意图按步执行.那么,从编写源文件代码到可执行文件,到底分为几步呢?这个过程可以总结为以 ...

  7. linux内核学习之六 进程创建过程学习

    一 关于linux进程概念的补充 关于进程的基本概念这里不多说,把自己的学习所得作一些补充: 1. 在linux内核中,系统最多可以有64个进程同时存在. 2.linux进程包含的关键要素:一段可执行 ...

  8. 【转载】详解CreateProcess调用内核创建进程的过程

    原文:详解CreateProcess调用内核创建进程的过程 昨天同学接到了腾讯的电面,有一题问到了CreateProcess创建进程的具体实现过程,他答得不怎么好吧应该是, 为了以防万一,也为了深入学 ...

  9. 是什么在.NET程序关闭时阻碍进程的退出?

    在平时使用软件或是.NET程序开发的过程中,我们有时会遇到程序关闭后但进程却没有退出的情况,这往往预示着代码中有问题存在,不能正确的在程序退出时停止代码执行和销毁资源.这个现象有时并不容易被察觉,但在 ...

随机推荐

  1. CocoaPods安装和使用教程(转摘)

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...

  2. QQ互联OAuth

    /** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = ' ...

  3. hdu 4272 2012长春赛区网络赛 dfs暴力 ***

    总是T,以为要剪枝,后来发现加个map就行了 #include<cstdio> #include<iostream> #include<algorithm> #in ...

  4. ASSM 的三级位图结构

    自动段空间管理(ASSM),它首次出现在Oracle920里(在920以前,段空间的管理方式叫做MSSM,它是由连接列表freelist来完成的,因为freelist存在串行的问题,因此容易引起段头的 ...

  5. Open judge C16H:Magical Balls 快速幂+逆元

    C16H:Magical Balls 总时间限制:  1000ms 内存限制:  262144kB 描述 Wenwen has a magical ball. When put on an infin ...

  6. 驱动中获取PsActiveProcessHead变量地址的五种方法也可以获取KdpDebuggerDataListHead

    PsActiveProcessHead的定义: 在windows系统中,所有的活动进程都是连在一起的,构成一个双链表,表头是全局变量PsActiveProcessHead,当一个进程被创建时,其Act ...

  7. HTML5火焰文字特效DEMO演示---转载

    只有google支持 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  8. 通信原理实践(五)——2PSK 与2DPSK 通信系统

    一.一些Matlab函数 二.2PSK调制解调,性能分析 1.2PSK调制 (1)图示 (2)Matlab代码 function [ s_t ,bb_t,bits] = psk2_module( nS ...

  9. 使用nginx做负载均衡的session共享问题

    查了一些资料,看了一些别人写的文档,总结如下,实现nginx session的共享PHP服务器有多台,用nginx做负载均衡,这样同一个IP访问同一个页面会被分配到不同的服务器上,如果session不 ...

  10. 【SQL 数据库】将一张数据表信息复制到另一张数据表

    一.MySQL数据库 1.如果目标表存在 INSERT INTO 目标表  SELECT  * FROM 源表; 2.如果目标表不存在 CREATE TABLE 目标表 SELECT * FROM   ...