This article mainly introduces Linux zombie processes and methods to clean up zombie processes.
1. What is a zombie process?
Linux中的僵尸进程(Zombie processes)有时也被称为失效或死亡进程。
它们是已执行完毕的进程,但其条目并未从进程表中删除。
1.1 进程状态
Linux 会维护一个进程表,其中包含所有正在运行的进程及其状态。
下面简要介绍一下各种进程状态:
(1)Running (R): 表示这些进程目前正在运行或可运行,用字母 R
表示。需要说明:进程是 R
状态,不代表正在运行,代表可被调度。换句话说,进程只有是 R
状态才可被调度,其他状态要先转为 R
状态,才能被 OS 调度;
(2)Waiting (S/D): 表示这些进程正在等待事件完成或某种资源就绪,用字母 S/D
表示,等待可以是可中断休眠 S
(interruptible sleep) 或不中断休眠 D
(uninterruptible sleep);
(3)Stopped (T): 可以通过发送 SIGSTOP
信号给进程来停止(T)进程。这个被暂停的进程可以通过发送 SIGCONT
信号让进程继续运行。
(4)Zombie (Z): 一个比较特殊的状态。当子进程退出并且父进程没有读取到子进程退出的返回代码时,就会产生僵死(尸)进程。僵死进程会以终止状态保持在进程表中,并且会一直在等待父进程读取退出状态代码。所以,只要子进程退出,父进程还在运行,但父进程没有读取子进程状态,子进程进入 Z
状态。
1.2 僵尸进程是如何产生的?
当一个进程完成其工作时,Linux 内核会通过发送 SIGCHLD
信号通知其父进程。然后,父进程执行 wait()
系统调用,读取子进程的状态并读取其退出代码。这会清除进程表中子进程条目,从而结束进程。
但是,如果父进程没有在创建子进程时执行 wait()
系统调用,就不会进行适当的清理。在这种情况下,父进程无法监控子进程的状态变化,最终会忽略 SIGCHLD
信号。这将导致已完成进程的僵尸状态留在进程表中,从而使其作为僵尸进程出现在进程列表中。
另一种情况是,父进程无法处理或接收来自子进程的 SIGCHLD
信号,这种情况也会导致僵尸的产生。
1.3 搜索僵尸进程
使用 ps
命令来检索僵尸进程列表:
ps ux USER PID %CPU %MEMVSZ RSS TTYSTAT START TIME COMMAND shubh90.00.0169162760 tty1 SDec19 0:00 /bin/bash --login shubh1080.00.00 0 tty1 Z16:25 0:00 [zombie] <defunct> shubh1090.00.0173841928 tty2 R16:25 0:00 ps ux
从输出中可以看出,STAT
列中的 Z
即为僵尸进程状态,或者使用 awk
命令根据 Z
进程状态进一步过滤输出:
ps ux | awk '{if($8=="Z") print}' shubh 1080.00.00 0 tty1 Z16:25 0:00 [zombie] <defunct>
另一种方法是使用 top
命令:
top Tasks: 8 total, 1 running, 6 sleeping, 0 stopped, 1 zombie %Cpu(s):0.7 us,1.6 sy,0.0 ni, 96.5 id,0.0 wa,1.2 hi,0.0 si,0.0 st KiB Mem :8269412 total,3161228 free,4878832 used, 229352 buff/cache KiB Swap: 15483260 total, 14830144 free, 653116 used.3256848 avail Mem PID USERPRNIVIRTRESSHR S%CPU %MEM TIME+ COMMAND 1 root20 08936192148 S 0.00.0 0:00.17 init 8 root20 08936 96 56 S 0.00.0 0:00.00 init 9 shubh 20 0 16916 2748 2640 S 0.00.0 0:00.43 bash 76 root20 08936224184 S 0.00.0 0:00.00 init 77 shubh 20 0 16784 3432 3332 S 0.00.0 0:00.35 bash 161 shubh 20 0 000 Z 0.00.0 0:00.00 zombie 162 shubh 20 0 17624 2084 1508 R 0.00.0 0:00.00 top
top
除了输出其他详细信息外,还可以在输出顶部的摘要中看到僵尸进程的数量。
2、清理僵尸进程
我们无法真正杀死僵尸进程,因为本身它已经结束了。但是,可以使用一些方法来清理僵尸进程。
2.1 使用 SIGCHLD 信号
可以手动向僵尸进程的父进程发送 SIGCHLD
信号。这样,父进程就会主动触发 wait()
系统调用,从而从进程表中清除已失效的子进程。
找到僵尸进程的父进程 PID:
ps -A -ostat,pid,ppid | grep -e '[zZ]' Z108 103
这里 108 表示僵尸进程 PID,103 表示其父进程 PID,接下来,可以使用 kill
命令向父进程发送 SIGCHLD
信号:
kill -s SIGCHLD 103
不过,并不能保证向父进程发送 SIGCHLD
信号就能杀死僵尸进程。只有在父进程可以处理 SIGCHLD
信号的情况下,它才会起作用。
2.2 kill 父过程
如果上一节的方法无法清除失效进程,就需要考虑杀死其父进程:
kill -9 103
但是,杀死父进程会影响其所有子进程。因此,应该格外谨慎,在杀死父进程之前必须确定其影响。
如果存在大量僵尸进程,或者僵尸进程的父进程是 init 进程(pid=1),可以考虑重启系统来清除失效进程。
The above is the detailed content of How to kill zombie processes in Linux. For more information, please follow other related articles on the PHP Chinese website!

What’s the difference between Linux and Mac? Do you want to install Linux on Mac? This post from php.cn will show you all. You can refer to this guide to dual boot Linux and macOS.

What is MHTML? How to open or view it? What are the differences between it and HTML? How to convert MHTML to HTML? If you are looking for the answers to the above questions, you can refer to this post from php.cn.

This article focuses on the topic that deleted files keep reappearing in Windows 10, introducing the responsible reasons and feasible solutions.

This post teaches you how to deactivate Windows 10/11 by removing product key or license. You can use that product key to activate another computer later if you want. For more computer tips and tricks, you can visit php.cn Software official website.

Some Windows users report that they are prompted by the error 0164 memory size decreased screen every time they boot the computer. What’s wrong with it? If you are in the same boat, congratulations! You’ve come to the right place! In this post from p

This Page Isn’t Available Right Now is an error message you may encounter when you visit Facebook using your web browser. In this php.cn post, we will list some effective methods you can try to get rid of this error.

Windows 11 KB5010414, a new optional update for Windows 11, is available now. Do you know what’s new and fixes in it? php.cn Software will show you this information in this post. Besides, it also tells you how to download and install it on your compu

Want to pause Windows Update on your Windows 11 computer? Want to set active hours to arrange a computer restart to complete the update process? You need to know how to change Windows Update settings in Windows 11. This php.cn post will show you the


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
