如何杀死僵尸进程

子进程

进程的状态: fork , execution, dead , exit 这个几个状态
父进程fork() 后创建一个子进程,得到父亲一样的资源 ,文件描述符等情况,子进程完成任务后进入dead状态;但此时子进程并没有完全从进程树中清除掉,还是占用着系统的资源,比如进程号pid; 子进程向父亲进程发送信号,等待确认;如果父亲进程确定子进程的终止信息,然后子进程才真正算是从进程树中完成清楚掉,释放占用的资源等情况。如果父亲进程选择忽略子进程发送过来的确认信号,或者没有调用wait() 或者 waitid() , 子进程就变成来僵尸进程(linux系统默认的进程号是 0到 1023,容量是 1024)

A process in Unix or Unix-like operating systems becomes a zombie process when it has completed execution but one or some of its entries are still in the process table. If a process is ended by an “exit” call, all memory associated with it is reallocated to a new process; in this way, the system saves memory. But the process’ entry in the process table remains until the parent process acknowledges its execution, after which it is removed. The time between the execution and the acknowledgment of the process is the period when the process is in a zombie state.

僵尸进程定义?

查看僵尸进程,利用命令ps,可以看到有标记为Z(zombie)的进程就是僵尸进程。
To understand what a zombie process is and what causes zombie processes to appear, you’ll need to understand a bit about how processes work on Linux.
When a process dies on Linux, it isn’t all removed from memory immediately — its process descriptor stays in memory (the process descriptor only takes a tiny amount of memory). The process’s status becomes EXIT_ZOMBIE and the process’s parent is notified that its child process has died with the SIGCHLD signal. The parent process is then supposed to execute the wait() system call to read the dead process’s exit status and other information. This allows the parent process to get information from the dead process. After wait() is called, the zombie process is completely removed from memory.

This normally happens very quickly, so you won’t see zombie processes accumulating on your system. However, if a parent process isn’t programmed properly and never calls wait(), its zombie children will stick around in memory until they’re cleaned up.

Utilities like GNOME System Monitor, the top command, and the ps command display zombie processes.

僵尸进程的危害?

僵尸进程会占用系统资源,如果很多,则会严重影响服务器的性能;
孤儿进程不会
Zombie processes don’t use up any system resources. (Actually, each one uses a very tiny amount of system memory to store its process descriptor.) However, each zombie process retains its process ID (PID). Linux systems have a finite number of process IDs – 32767 by default on 32-bit systems. If zombies are accumulating at a very quick rate – for example, if improperly programmed server software is creating zombie processes under load — the entire pool of available PIDs will eventually become assigned to zombie processes, preventing other processes from launching.

However, a few zombie processes hanging around are no problem – although they do indicate a bug with their parent process on your system.

僵尸进程产生的原因?

  • 子进程结束后向父进程发出SIGCHLD信号,父进程默认忽略了它
  • 父进程没有调用wait()或waitpid()函数来确认子进程的结束
  • 网络原因有时会引起僵尸进程;
    To understand what a zombie process is and what causes zombie processes to appear, you’ll need to understand a bit about how processes work on Linux.
    When a process dies on Linux, it isn’t all removed from memory immediately — its process descriptor stays in memory (the process descriptor only takes a tiny amount of memory). The process’s status becomes EXIT_ZOMBIE and the process’s parent is notified that its child process has died with the SIGCHLD signal. The parent process is then supposed to execute the wait() system call to read the dead process’s exit status and other information. This allows the parent process to get information from the dead process. After wait() is called, the zombie process is completely removed from memory.

This normally happens very quickly, so you won’t see zombie processes accumulating on your system. However, if a parent process isn’t programmed properly and never calls wait(), its zombie children will stick around in memory until they’re cleaned up.

Utilities like GNOME System Monitor, the top command, and the ps command display zombie processes

如何杀死僵尸进程?

僵尸进程用kill命令是无法杀掉的,但是我们可以结果掉僵尸进程的爸爸,僵尸daddy挂了之后,僵尸进程就成了孤儿进程,孤儿进程不会占用系统资源,会被init进程**(init process (PID 1))**收养,然后init进程(init process (PID 1))将其回收。
(补充:init(init process (PID 1))是linux系统中的根进程,linux中的进程采用树结构来划分)
僵尸进程解决办法

  1. 通过信号机制
      子进程退出时向父进程发送SIGCHILD信号,父进程处理SIGCHILD信号。在信号处理函数中调用wait进行处理僵尸进程。测试程序如下所示:

You can’t kill zombie processes as you can kill normal processes with the SIGKILL signal — zombie processes are already dead. Bear in mind that you don’t need to get rid of zombie processes unless you have a large amount on your system – a few zombies are harmless. However, there are a few ways you can get rid of zombie processes.

One way is by sending the SIGCHLD signal to the parent process. This signal tells the parent process to execute the wait() system call and clean up its zombie children. Send the signal with the kill command, replacing pid in the command below with the parent process’s PID:

kill -s SIGCHLD pid

However, if the parent process isn’t programmed properly and is ignoring SIGCHLD signals, this won’t help. You’ll have to kill or close the zombies’ parent process. When the process that created the zombies ends, init inherits the zombie processes and becomes their new parent. (init is the first process started on Linux at boot and is assigned PID 1.) init periodically executes the wait() system call to clean up its zombie children, so init will make short work of the zombies. You can restart the parent process after closing it.

If a parent process continues to create zombies, it should be fixed so that it properly calls wait() to reap its zombie children. File a bug report if a program on your system keeps creating zombies.
2. 杀死父进程
  僵尸进程用kill命令是无法杀掉的,但是我们可以结果掉僵尸进程的爸爸,僵尸daddy挂了之后,僵尸进程就成了孤儿进程,孤儿进程不会占用系统资源,会被init程序收养,然后init程序将其回收

英文参考文献

详细文件保存在evernote Linux 笔记本:父进程和子进程,僵尸进程,孤儿进程

### 查找并杀死 Linux 系统中的僵尸进程 在 Linux 系统中,僵尸进程是指已经完成执行但其父进程尚未通过 `wait()` 系统调用回收其状态信息的进程。僵尸进程本身并不会消耗系统资源,但会占用进程 ID,可能导致系统资源耗尽。因此,及时清理僵尸进程非常重要。 以下是一些用于查找和杀死僵尸进程的命令及方法: 1. **查找僵尸进程** 使用 `ps` 命令结合 `grep` 找出僵尸进程: ```bash ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]' ``` 上述命令解释如下: - `-A` 参数列出所有进程。 - `-o` 参数自定义输出字段,包括 `stat`(状态)、`ppid`(父进程 ID)、`pid`(进程 ID)和 `cmd`(命令)。 - `grep -e '^[Zz]'` 用于过滤状态为 `Z` 或 `z` 的僵尸进程[^1]。 2. **杀死僵尸进程的父进程** 僵尸进程本身无法直接被杀死,因为它们已经处于终止状态。需要杀死其父进程,以便孤儿进程被 `init`(PID 为 1 的进程)接管并清理。以下是实现此目的的命令: ```bash ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]' | awk '{print $2}' | xargs kill -9 ``` 解释: - `awk '{print $2}'` 提取僵尸进程的父进程 ID(PPID)。 - `xargs kill -9` 强制杀死这些父进程。 3. **自动化清理僵尸进程** 如果需要定期清理僵尸进程,可以编写一个脚本并使用 `cron` 定时任务运行。以下是一个示例脚本: ```python import os import time while True: # 杀死僵尸进程的父进程 os.system('ps -A -ostat,ppid,pid,cmd | grep -e "^[Zz]" | awk \'{print $2}\' | xargs kill -9') time.sleep(5) # 每隔 5 秒检查一次 ``` 4. **其他方法** 如果僵尸进程是由特定程序(如 `chromedriver` 或其他服务)产生的,可以直接定位并杀死相关进程: ```bash ps -ef | grep 'chromedriver' | grep -v grep | awk '{print $2}' | xargs kill -9 ``` 该命令会查找所有与 `chromedriver` 相关的进程,并强制杀死它们[^2]。 5. **预防僵尸进程的产生** - 确保父进程正确调用 `wait()` 或 `waitpid()` 系统调用来回收子进程的状态信息。 - 使用信号处理机制捕获 `SIGCHLD` 信号以自动回收子进程。 ### 注意事项 - 杀死父进程可能会导致依赖该父进程的其他子进程受到影响,请谨慎操作。 - 在生产环境中,建议先分析僵尸进程的来源并修复根本问题,而不是频繁清理僵尸进程。 ```bash # 示例综合命令 ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]' | awk '{print $2}' | xargs kill -9 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值