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

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
简介: 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
AI 代码解读

进程_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;
}
AI 代码解读

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.
AI 代码解读

如果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.
AI 代码解读

如何清理Zombie状态的进程

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

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

如果发这个信号还不行,那可能是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)
        {
...
AI 代码解读
目录
打赏
0
0
0
0
20695
分享
相关文章
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
在计算机系统的底层架构中,操作系统肩负着资源管理与任务调度的重任。当我们启动各类应用程序时,其背后复杂的运作机制便悄然展开。程序,作为静态的指令集合,如何在系统中实现动态执行?本文带你一探究竟!
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
119 67
深入理解Linux操作系统的进程管理
本文旨在探讨Linux操作系统中的进程管理机制,包括进程的创建、执行、调度和终止等环节。通过对Linux内核中相关模块的分析,揭示其高效的进程管理策略,为开发者提供优化程序性能和资源利用率的参考。
155 1
【Linux】进程概念和进程状态
本文详细介绍了Linux系统中进程的核心概念与管理机制。从进程的定义出发,阐述了其作为操作系统资源管理的基本单位的重要性,并深入解析了task_struct结构体的内容及其在进程管理中的作用。同时,文章讲解了进程的基本操作(如获取PID、查看进程信息等)、父进程与子进程的关系(重点分析fork函数)、以及进程的三种主要状态(运行、阻塞、挂起)。此外,还探讨了Linux特有的进程状态表示和孤儿进程的处理方式。通过学习这些内容,读者可以更好地理解Linux进程的运行原理并优化系统性能。
49 4
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
120 34
|
1月前
|
Linux:守护进程(进程组、会话和守护进程)
守护进程在 Linux 系统中扮演着重要角色,通过后台执行关键任务和服务,确保系统的稳定运行。理解进程组和会话的概念,是正确创建和管理守护进程的基础。使用现代的 `systemd` 或传统的 `init.d` 方法,可以有效地管理守护进程,提升系统的可靠性和可维护性。希望本文能帮助读者深入理解并掌握 Linux 守护进程的相关知识。
63 7
|
1月前
|
Linux 进程前台后台切换与作业控制
进程前台/后台切换及作业控制简介: 在 Shell 中,启动的程序默认为前台进程,会占用终端直到执行完毕。例如,执行 `./shella.sh` 时,终端会被占用。为避免不便,可将命令放到后台运行,如 `./shella.sh &`,此时终端命令行立即返回,可继续输入其他命令。 常用作业控制命令: - `fg %1`:将后台作业切换到前台。 - `Ctrl + Z`:暂停前台作业并放到后台。 - `bg %1`:让暂停的后台作业继续执行。 - `kill %1`:终止后台作业。 优先级调整:
83 5
Linux 进程管理基础
Linux 进程是操作系统中运行程序的实例,彼此隔离以确保安全性和稳定性。常用命令查看和管理进程:`ps` 显示当前终端会话相关进程;`ps aux` 和 `ps -ef` 显示所有进程信息;`ps -u username` 查看特定用户进程;`ps -e | grep &lt;进程名&gt;` 查找特定进程;`ps -p &lt;PID&gt;` 查看指定 PID 的进程详情。终止进程可用 `kill &lt;PID&gt;` 或 `pkill &lt;进程名&gt;`,强制终止加 `-9` 选项。
31 3
|
4月前
|
Linux缓存管理:如何安全地清理系统缓存
在Linux系统中,内存管理至关重要。本文详细介绍了如何安全地清理系统缓存,特别是通过使用`/proc/sys/vm/drop_caches`接口。内容包括清理缓存的原因、步骤、注意事项和最佳实践,帮助你在必要时优化系统性能。
364 78
c++ linux通过实现独立进程之间的通信和传递字符串 demo
的进程间通信机制,适用于父子进程之间的数据传输。希望本文能帮助您更好地理解和应用Linux管道,提升开发效率。 在实际开发中,除了管道,还可以根据具体需求选择消息队列、共享内存、套接字等其他进程间通信方
79 16
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等