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

2 minute read


Linux 进程有哪些状态


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去除。


#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) {  
	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  






另外需要注意,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进程的父进程,发起waitpid调用,清理process table中对应子进程的信息。

如果父进程没有处理,可以手工向他发起 SIGCHLD 信号,让他处理。

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



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


man 2 exit

man 2 wait

man 7 signal

PostgreSQL 如何处理退出的子进程


src/backend/postmaster/postmaster.c:    pqsignal(SIGCHLD, reaper);      /* handle child termination */  
 * Reaper -- signal handler to cleanup after a child process dies.  
static void  
        int                     save_errno = errno;  
        int                     pid;                    /* process id of dead child process */  
        int                     exitstatus;             /* its exit status */  
                        (errmsg_internal("reaping dead processes")));  
        while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)  

Flag Counter

digoal’s 大量PostgreSQL文章入口