# 僵尸进程

僵尸进程（zombie process，又称已终止进程，defunct process）是 Unix 或类 Unix 操作系统上的一种进程：它已完成执行，但在进程表中仍保留条目，因为其父进程尚未读取它的退出状态。内核保留这份最小记录，其中包含进程 ID（PID）、终止状态和资源使用信息，以便父进程日后可以通过 `wait` 系统调用回收它；在此之前，已终止的子进程一直处于僵尸状态。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup>

| 关键事实 | 说明 |
|---|---|
| 定义 | 已终止但父进程尚未对其执行等待的子进程；内核保留其 PID、终止状态和资源使用信息。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup> |
| 消耗什么 | 除进程表中的一个槽位外，不消耗 CPU 或内存；若进程表被占满，则无法再创建新进程。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup> |
| 清除 | 父进程通过调用 `wait()`、`waitpid()` 或 `waitid()` 回收僵尸进程；若父进程先退出，僵尸进程会被 init（PID 1）或最近的子收割者收养，由后者对其执行等待。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup> |
| 识别 | 在 `ps` 和 `/proc/PID/status` 中显示为状态 `Z`（“defunct”）；命令名显示为 `<defunct>`。<sup>[2](https://www.man7.org/linux/man-pages/man1/ps.1.html)</sup><sup> • </sup><sup>[3](https://www.kernel.org/doc/html/v6.8/filesystems/proc.html)</sup> |
| 杀死它 | 包括 SIGKILL 在内的信号均无效，因为该进程已经死亡；解决办法是让父进程回收它，或终止父进程。<sup>[4](https://www.baeldung.com/cs/process-lifecycle-zombie-state)</sup> |
| 避免 | 在 POSIX.1-2001 下，将 SIGCHLD 设为 SIG_IGN 或设置 SA_NOCLDWAIT 标志，可防止已终止的子进程变成僵尸进程。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup> |
| 容器实践 | Docker 的 `--init` 标志会运行一个小型 init（docker-init，底层为 tini）作为 PID 1，从而在容器内部执行僵尸进程回收。<sup>[5](https://docs.docker.com/reference/cli/docker/container/run)</sup> |

## 进程如何变成僵尸

在类 Unix 系统中，进程通过 `fork()` 形成父子树。子进程终止时，无论是正常结束还是因信号终止，内核都会回收其大部分资源，包括地址空间和缓冲区，但会保留带有退出状态的进程描述符。在 Linux 上，终止的子进程还会向父进程发送 SIGCHLD 信号，宣告自己的终止。<sup>[4](https://www.baeldung.com/cs/process-lifecycle-zombie-state)</sup> 此后子进程便停留在僵尸状态，直到父进程调用 wait 系列系统调用之一——`wait()`、`waitpid()` 或 `waitid()`——将子进程的退出状态传给父进程，并释放剩余资源。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup>

这一状态是设计使然。若没有它，操作系统会在子进程结束的瞬间丢弃关于它的一切信息，父进程便无从得知子进程的退出状态或资源使用情况（例如消耗的 CPU 时间）。僵尸状态保证了这封从子进程寄往父进程的“漂流瓶”永不丢失，无论哪一方先结束。<sup>[6](https://courses.cs.vt.edu/cs3214/spring2026/lecturesli/process-part3.pdf)</sup>

## 僵尸进程为何重要

僵尸进程不执行任何指令，也不消耗 CPU 或内存；它的代价是所占用的进程表条目。<sup>[7](https://www.gatesmashers.com/resources/operating-system-interview-questions/zombie-vs-orphan-processes)</sup> 这个条目并非没有成本。内核进程表大小有限，只要僵尸进程未被 wait 清除，它就占据一个槽位；若进程表被占满，系统就完全无法再创建新进程。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup> 在 Linux 上，进程 ID 的数量受 `/proc/sys/kernel/pid_max` 限制，通常为 32,768；一旦 PID 耗尽，应用程序便无法 fork 新任务，症状包括 SSH 登录失败、cron 任务失败，以及 `fork: Resource temporarily unavailable` 错误。<sup>[8](https://topdowncs.com/docs/os-essential/17-zombie-vs-orphan-processes/)</sup>

僵尸进程也无法被再次杀死。由于它已经终止，包括强力的 SIGKILL 在内的信号对它均无效。<sup>[4](https://www.baeldung.com/cs/process-lifecycle-zombie-state)</sup> 实际的解决办法是让父进程回收子进程、向父进程发送 SIGCHLD 之类的信号促使其回收，或终止父进程，使僵尸进程被重新托付给某个会回收它的进程。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup>

## 回收与预防

**正常回收。** 编写良好的父进程会用 `wait()` 或 `waitpid()` 收集每个子进程的状态，通常是在安装 SIGCHLD 处理函数后循环调用，因此僵尸进程只在子进程退出到父进程等待之间的短暂时间内存在。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup> `WNOHANG` 选项让父进程可以非阻塞地轮询已终止的子进程。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup>

**自动回收。** POSIX.1-2001 规定，若进程将 SIGCHLD 的处理方式设为 SIG_IGN，或为 SIGCHLD 设置 SA_NOCLDWAIT 标志，其已终止的子进程不会变成僵尸进程；随后的 wait 会阻塞到所有子进程终止，然后以 ECHILD 失败。显式设置 SIG_IGN 与 SIGCHLD 的默认处理方式（同样是“忽略”）行为不同。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup>

**收养。** 若父进程未回收便终止，其僵尸子进程（以及仍在运行的子进程，即孤儿进程）会被 init——PID 为 1 的进程——收养，或被最近的、通过 `prctl(2)` 的 PR_SET_CHILD_SUBREAPER 操作标记为子收割者的祖先进程收养（该操作自 Linux 3.4 起可用）。Init 会自动执行 wait 以清除它继承的僵尸进程。<sup>[1](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)</sup><sup> • </sup><sup>[9](https://www.man7.org/linux/man-pages/man2/PR_SET_CHILD_SUBREAPER.2const.html)</sup> Init 作为僵尸进程清道夫的角色，是它在类 Unix 系统中的核心职责之一。<sup>[10](https://arxiv.org/abs/0706.2748)</sup>

## 僵尸进程与孤儿进程

僵尸进程与孤儿进程是父子生命周期中相反的两种情况。僵尸进程是先退出的子进程，已死亡但尚未被回收；孤儿进程则是父进程退出后仍在运行的子进程。<sup>[6](https://courses.cs.vt.edu/cs3214/spring2026/lecturesli/process-part3.pdf)</sup> 孤儿进程会被重新托付，传统上是托付给 init（PID 1），这样当它最终终止时，总有一个合法的父进程准备收集其状态，防止它变成无人可回收的僵尸进程。<sup>[11](https://devsunite.com/resources/core-subjects/operating-systems/zombie-vs-orphan-process)</sup> 第三类情况是守护进程，即长期存活的后台进程，通常有意通过让父进程退出来创建；守护进程与僵尸进程一样没有控制终端。<sup>[12](https://www.geeksforgeeks.org/operating-systems/difference-between-zombie-orphan-and-daemon-processes/)</sup>

## 在 Linux 上观察僵尸进程

`ps` 以状态码 `Z` 报告僵尸进程，描述为已终止进程（defunct process），即已终止但未被父进程回收，命令名显示为 `<defunct>`。<sup>[2](https://www.man7.org/linux/man-pages/man1/ps.1.html)</sup> 在 `/proc/PID/status` 中可以看到同样的状态，其中 State 字段读作 `Z (zombie)`。<sup>[3](https://www.kernel.org/doc/html/v6.8/filesystems/proc.html)</sup>

## 容器中的僵尸进程

容器让僵尸进程的处理以新的方式显现出来。在容器内以 PID 1 运行的进程会被 Linux 特殊对待；如果容器的主应用程序作为 PID 1 却不具备 init 行为，它派生的子进程退出后就可能堆积成僵尸进程。Docker 的 `--init` 标志会插入一个小型 init 进程作为 PID 1，其职责是回收僵尸进程并转发信号；随附的 `docker-init` 二进制文件底层为 tini 程序，后者派生一个子进程、等待它，并定期回收僵尸进程。<sup>[5](https://docs.docker.com/reference/cli/docker/container/run)</sup><sup> • </sup><sup>[13](https://github.com/krallin/tini)</sup> 若 tini 无法作为 PID 1 运行，也可以在 Linux 3.4 及更高版本上将其注册为进程子收割者，这样僵尸进程无论如何都会被重新托付给它。<sup>[13](https://github.com/krallin/tini)</sup>

## References

1. [wait(2) — Linux manual page, man7.org](https://www.man7.org/linux/man-pages/man2/waitpid.2.html)
2. [ps(1) — Linux manual page, man7.org](https://www.man7.org/linux/man-pages/man1/ps.1.html)
3. [The /proc Filesystem — The Linux Kernel documentation](https://www.kernel.org/doc/html/v6.8/filesystems/proc.html)
4. [Zombie Processes in Operating Systems, Baeldung on Computer Science](https://www.baeldung.com/cs/process-lifecycle-zombie-state)
5. [docker container run — Docker Docs](https://docs.docker.com/reference/cli/docker/container/run)
6. [Computer Systems, Lecture 4: Processes (Part III), Virginia Tech CS 3214](https://courses.cs.vt.edu/cs3214/spring2026/lecturesli/process-part3.pdf)
7. [Zombie vs Orphan Processes, Gate Smashers](https://www.gatesmashers.com/resources/operating-system-interview-questions/zombie-vs-orphan-processes)
8. [Zombie vs. Orphan Processes & Resource Reclamation, TopDown CS](https://topdowncs.com/docs/os-essential/17-zombie-vs-orphan-processes/)
9. [PR_SET_CHILD_SUBREAPER(2const) — Linux manual page, man7.org](https://www.man7.org/linux/man-pages/man2/PR_SET_CHILD_SUBREAPER.2const.html)
10. [Yvan Royon and Stéphane Frénot (2007). A Survey of Unix Init Schemes. arXiv.](https://arxiv.org/abs/0706.2748)
11. [Zombie vs Orphan Process, DevsUnite](https://devsunite.com/resources/core-subjects/operating-systems/zombie-vs-orphan-process)
12. [Difference between Zombie, Orphan and Daemon Processes, GeeksforGeeks](https://www.geeksforgeeks.org/operating-systems/difference-between-zombie-orphan-and-daemon-processes/)
13. [tini: A tiny but valid init for containers, GitHub (krallin/tini)](https://github.com/krallin/tini)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Operating system concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/zombie-process

English original: https://www.edgepedia.org/zombie-process
