Linux Zombie进程状态介绍 以及 如何清理

简介: Linux 进程有哪些状态 通过ps的帮助手册,能看到进程有几种状态 man ps D uninterruptible sleep (usually IO) R running or runnable (on run

Linux 进程有哪些状态

通过ps的帮助手册,能看到进程有几种状态

man ps
               D    uninterruptible sleep (usually IO)
               R    running or runnable (on run queue)
               S    interruptible sleep (waiting for an event to complete)
               T    stopped, either by a job control signal or because it is being traced
               W    paging (not valid since the 2.6.xx kernel)
               X    dead (should never be seen)
               Z    defunct ("zombie") process, terminated but not reaped by its parent

进程_exit退出后,进程占用的内存和其他资源会被回收,同时在操作系统的process table中依旧保留一条记录(存储PID, termination status, resource usage information),此时进程的状态是zombie / defunct的 。

父进程会使用waitpid系统调用,回收处于zombie状态的子进程,回收后进程的信息才会从process table去除。
wiki里的例子

#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pids[10];
    int i;

    for (i = 9; i >= 0; --i) {
        pids[i] = fork();
        if (pids[i] == 0) {
            sleep(i+1);
            _exit(0);
        }
    }

    for (i = 9; i >= 0; --i)
        waitpid(pids[i], NULL, 0);

    return 0;
}

man 2 wait 里的解释

       A child that terminates, but has not been waited for  becomes  a  "zom-
       bie".  The kernel maintains a minimal set of information about the zom-
       bie process (PID, termination status, resource  usage  information)  in
       order to allow the parent to later perform a wait to obtain information
       about the child.  As long as a zombie is not removed  from  the  system
       via  a wait, it will consume a slot in the kernel process table, and if
       this table fills, it will not be possible to create further  processes.
       If a parent process terminates, then its "zombie" children (if any) are
       adopted by init(8), which automatically performs a wait to  remove  the
       zombies.

如果zombie进程的父进程被terminate了,那么zombie进程将会被init进程接管,init进程也会异步的调用wait回收处于zombie的进程。

什么时候会进入Zombie状态

前面已经解释了,当进程exit后,会进入zombie状态。
然后它的父进程会通过waitpid调用,回收处于zombie状态的进程。
如果处于zombie状态进程的父进程被terminate了,没有被回收的zombie进程就会被init接管,init也会调用waitpid来回收它的zombie子进程。

另外需要注意,POSIX.1-2001标准的系统,允许设置SIGCHLD to SIG_IGN,这样不需要通过wait来回收zombie子进程。 因为它fork的子进程进程不会进入zombie状态,exit后不会在process table中保留任何信息。

On modern UNIX-like systems (that comply with SUSv3 specification in this respect), the following special case applies: if the parent explicitly ignores SIGCHLD by setting its handler to SIG_IGN (rather than simply ignoring the signal by default) or has the SA_NOCLDWAIT flag set, all child exit status information will be discarded and no zombie processes will be left

       POSIX.1-1990 disallowed setting the  action  for  SIGCHLD  to  SIG_IGN.
       POSIX.1-2001  allows  this possibility, so that ignoring SIGCHLD can be
       used to prevent the creation of zombies (see  wait(2)).   Nevertheless,
       the  historical BSD and System V behaviors for ignoring SIGCHLD differ,
       so that the only completely portable method of ensuring that terminated
       children  do not become zombies is to catch the SIGCHLD signal and per-
       form a wait(2) or similar.

如何清理Zombie状态的进程

通过zombie进程的父进程,发起waitpid调用,清理process table中对应子进程的信息。
如果父进程没有处理,可以手工向他发起 SIGCHLD 信号,让他处理。

kill -SIGCHLD $(ps -A -ostat,ppid | awk '/[zZ]/{print $2}')

如果发这个信号还不行,那可能是parent进程没有处理这个信号,把父进程也干掉,然后让init去回收zombie进程。

如果没有清理掉会有什么危险

zombie进程会占用process table的slot,如果有非常多的zombie,可能最终会导致process table slot满,导致系统不能创建新的进程。

参考

http://stackoverflow.com/questions/16944886/how-to-kill-zombie-process
https://manpages.debian.org/cgi-bin/man.cgi?query=wait&sektion=2
https://en.wikipedia.org/wiki/Zombie_process
http://unix.stackexchange.com/questions/11172/how-can-i-kill-a-defunct-process-whose-parent-is-init
http://stackoverflow.com/questions/20535438/cant-cleanup-a-zombie-process-whose-parent-is-init
man 2 exit
man 2 wait
man 7 signal

PostgreSQL 如何处理退出的子进程

也能看到waitpid的影子

注册信号处理函数  
src/backend/postmaster/postmaster.c:    pqsignal(SIGCHLD, reaper);      /* handle child termination */

信号处理函数内容
/*
 * Reaper -- signal handler to cleanup after a child process dies.
 */
static void
reaper(SIGNAL_ARGS)
{
        int                     save_errno = errno;
        int                     pid;                    /* process id of dead child process */
        int                     exitstatus;             /* its exit status */

        PG_SETMASK(&BlockSig);

        ereport(DEBUG4,
                        (errmsg_internal("reaping dead processes")));

        while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)
        {
...
目录
相关文章
|
10月前
|
存储 Linux API
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
在计算机系统的底层架构中,操作系统肩负着资源管理与任务调度的重任。当我们启动各类应用程序时,其背后复杂的运作机制便悄然展开。程序,作为静态的指令集合,如何在系统中实现动态执行?本文带你一探究竟!
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
|
4月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
1653 10
|
5月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
685 0
|
8月前
|
并行计算 Linux
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
310 67
|
7月前
|
Web App开发 Linux 程序员
获取和理解Linux进程以及其PID的基础知识。
总的来说,理解Linux进程及其PID需要我们明白,进程就如同汽车,负责执行任务,而PID则是独特的车牌号,为我们提供了管理的便利。知道这个,我们就可以更好地理解和操作Linux系统,甚至通过对进程的有效管理,让系统运行得更加顺畅。
230 16
|
7月前
|
Unix Linux
对于Linux的进程概念以及进程状态的理解和解析
现在,我们已经了解了Linux进程的基础知识和进程状态的理解了。这就像我们理解了城市中行人的行走和行为模式!希望这个形象的例子能帮助我们更好地理解这个重要的概念,并在实际应用中发挥作用。
147 20
|
6月前
|
监控 Shell Linux
Linux进程控制(详细讲解)
进程等待是系统通过调用特定的接口(如waitwaitpid)来实现的。来进行对子进程状态检测与回收的功能。
135 0
|
6月前
|
存储 负载均衡 算法
Linux2.6内核进程调度队列
本篇文章是Linux进程系列中的最后一篇文章,本来是想放在上一篇文章的结尾的,但是想了想还是单独写一篇文章吧,虽然说这部分内容是比较难的,所有一般来说是简单的提及带过的,但是为了让大家对进程有更深的理解与认识,还是看了一些别人的文章,然后学习了学习,然后对此做了总结,尽可能详细的介绍明白。最后推荐一篇文章Linux的进程优先级 NI 和 PR - 简书。
209 0
|
6月前
|
存储 Linux Shell
Linux进程概念-详细版(二)
在Linux进程概念-详细版(一)中我们解释了什么是进程,以及进程的各种状态,已经对进程有了一定的认识,那么这篇文章将会继续补全上篇文章剩余没有说到的,进程优先级,环境变量,程序地址空间,进程地址空间,以及调度队列。
135 0
|
6月前
|
Linux 调度 C语言
Linux进程概念-详细版(一)
子进程与父进程代码共享,其子进程直接用父进程的代码,其自己本身无代码,所以子进程无法改动代码,平时所说的修改是修改的数据。为什么要创建子进程:为了让其父子进程执行不同的代码块。子进程的数据相对于父进程是会进行写时拷贝(COW)。
187 0