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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 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)
        {
...
相关文章
|
2天前
|
存储 Linux Shell
Linux:进程等待 & 进程替换
Linux:进程等待 & 进程替换
29 9
|
2天前
|
存储 Linux C语言
Linux:进程创建 & 进程终止
Linux:进程创建 & 进程终止
24 6
|
1天前
|
存储 安全 Linux
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
|
1天前
|
消息中间件 算法 Linux
【Linux】详解如何利用共享内存实现进程间通信
【Linux】详解如何利用共享内存实现进程间通信
|
1天前
|
Linux
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
|
1天前
|
Linux
【Linux】匿名管道实现简单进程池
【Linux】匿名管道实现简单进程池
|
1天前
|
Linux
【Linux】进程通信之匿名管道通信
【Linux】进程通信之匿名管道通信
|
1天前
|
Linux C++
【Linux】详解进程程序替换
【Linux】详解进程程序替换
|
2天前
|
存储 安全 Linux
Linux:进程地址空间
Linux:进程地址空间
21 10
|
2天前
|
存储 弹性计算 Linux
Linux:进程调度
Linux:进程调度
20 7