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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS PostgreSQL Serverless,0.5-4RCU 50GB 3个月
推荐场景:
对影评进行热评分析
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介: 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)
        {
...
目录
相关文章
|
5月前
|
存储 Linux API
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
在计算机系统的底层架构中,操作系统肩负着资源管理与任务调度的重任。当我们启动各类应用程序时,其背后复杂的运作机制便悄然展开。程序,作为静态的指令集合,如何在系统中实现动态执行?本文带你一探究竟!
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
|
3月前
|
并行计算 Linux
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
189 67
|
2月前
|
Web App开发 Linux 程序员
获取和理解Linux进程以及其PID的基础知识。
总的来说,理解Linux进程及其PID需要我们明白,进程就如同汽车,负责执行任务,而PID则是独特的车牌号,为我们提供了管理的便利。知道这个,我们就可以更好地理解和操作Linux系统,甚至通过对进程的有效管理,让系统运行得更加顺畅。
78 16
|
2月前
|
Unix Linux
对于Linux的进程概念以及进程状态的理解和解析
现在,我们已经了解了Linux进程的基础知识和进程状态的理解了。这就像我们理解了城市中行人的行走和行为模式!希望这个形象的例子能帮助我们更好地理解这个重要的概念,并在实际应用中发挥作用。
72 20
|
1月前
|
监控 Shell Linux
Linux进程控制(详细讲解)
进程等待是系统通过调用特定的接口(如waitwaitpid)来实现的。来进行对子进程状态检测与回收的功能。
48 0
|
1月前
|
存储 负载均衡 算法
Linux2.6内核进程调度队列
本篇文章是Linux进程系列中的最后一篇文章,本来是想放在上一篇文章的结尾的,但是想了想还是单独写一篇文章吧,虽然说这部分内容是比较难的,所有一般来说是简单的提及带过的,但是为了让大家对进程有更深的理解与认识,还是看了一些别人的文章,然后学习了学习,然后对此做了总结,尽可能详细的介绍明白。最后推荐一篇文章Linux的进程优先级 NI 和 PR - 简书。
41 0
|
1月前
|
存储 Linux Shell
Linux进程概念-详细版(二)
在Linux进程概念-详细版(一)中我们解释了什么是进程,以及进程的各种状态,已经对进程有了一定的认识,那么这篇文章将会继续补全上篇文章剩余没有说到的,进程优先级,环境变量,程序地址空间,进程地址空间,以及调度队列。
36 0
|
1月前
|
Linux 调度 C语言
Linux进程概念-详细版(一)
子进程与父进程代码共享,其子进程直接用父进程的代码,其自己本身无代码,所以子进程无法改动代码,平时所说的修改是修改的数据。为什么要创建子进程:为了让其父子进程执行不同的代码块。子进程的数据相对于父进程是会进行写时拷贝(COW)。
46 0
|
7月前
|
算法 Linux 调度
深入理解Linux操作系统的进程管理
本文旨在探讨Linux操作系统中的进程管理机制,包括进程的创建、执行、调度和终止等环节。通过对Linux内核中相关模块的分析,揭示其高效的进程管理策略,为开发者提供优化程序性能和资源利用率的参考。
218 1
|
4月前
|
存储 Linux 调度
【Linux】进程概念和进程状态
本文详细介绍了Linux系统中进程的核心概念与管理机制。从进程的定义出发,阐述了其作为操作系统资源管理的基本单位的重要性,并深入解析了task_struct结构体的内容及其在进程管理中的作用。同时,文章讲解了进程的基本操作(如获取PID、查看进程信息等)、父进程与子进程的关系(重点分析fork函数)、以及进程的三种主要状态(运行、阻塞、挂起)。此外,还探讨了Linux特有的进程状态表示和孤儿进程的处理方式。通过学习这些内容,读者可以更好地理解Linux进程的运行原理并优化系统性能。
152 4