在 unix 或 unix-like 的系统中,当一个子进程退出后,它就会变成一个僵尸进程,如果父进程没有通过 wait 系统调用来读取这个子进程的退出状态的话,这个子进程就会一直维持僵尸进程状态。

Zombie process - Wikipedia 中是这样描述的:

On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the "Terminated state". This occurs for child processes, where the entry is still needed to allow the parent process to read its child's exit status: once the exit status is read via the wait system call, the zombie's entry is removed from the process table and it is said to be "reaped". A child process always first becomes a zombie before being removed from the resource table. In most cases, under normal system operation zombies are immediately waited on by their parent and then reaped by the system – processes that stay zombies for a long time are generally an error and cause a resource leak.

并且僵尸进程无法通过 kill 命令来清除。

本文将探讨如何手动制造一个僵尸进程以及清除僵尸进程的办法。

手动制造一个僵尸进程

为了便于后面讲解清除僵尸进程的方法,我们使用日常开发中经常使用的 multiprocessing 模块来制造僵尸进程(准确的来说是制造一个长时间维持僵尸进程状态的子进程):

$ cat test_a.py

from multiprocessing import Process, current_process

import logging

import os

import time

logging.basicConfig(

level=logging.DEBUG,

format='%(asctime)-15s - %(levelname)s - %(message)s'

)

def run():

logging.info('exit child process %s', current_process().pid)

os._exit(3)

p = Process(target=run)

p.start()

time.sleep(100)

测试:

$ python test_a.py &

[1] 10091

$ 2017-07-20 21:28:14,792 - INFO - exit child process 10106

$ ps aux |grep 10106

mozillazg 10126 0.0 0.0 2434836 740 s006 R+ 0:00.00 grep 10106

mozillazg 10106 0.0 0.0 0 0 s006 Z 0:00.00 (Python)

可以看到,子进程 10091 变成了僵尸进程。

既然已经可以控制僵尸进程的产生了,那我们就可以进入下一步如何清除僵尸进程了。

清除僵尸进程有两种方法:

•第一种方法就是结束父进程。当父进程退出的时候僵尸进程随后也会被清除。

• 第二种方法就是通过 wait 调用来读取子进程退出状态。我们可以通过处理 SIGCHLD 信号,在处理程序中调用 wait 系统调用来清除僵尸进程。

处理 SIGCHLD 信号

子进程退出时系统会向父进程发送 SIGCHLD 信号,父进程可以通过注册 SIGCHLD 信号处理程序,在信号处理程序中调用 wait

系统调用来清理僵尸进程。 $ cat test_b.py

import errno

from multiprocessing import Process, current_process

import logging

import os

import signal

import time

logging.basicConfig(

level=logging.DEBUG,

format='%(asctime)-15s - %(levelname)s - %(message)s'

)

def run():

exitcode = 3

logging.info('exit child process %s with exitcode %s',

current_process().pid, exitcode)

os._exit(exitcode)

def wait_child(signum, frame):

logging.info('receive SIGCHLD')

try:

while True:

# -1 表示任意子进程

# os.WNOHANG 表示如果没有可用的需要 wait 退出状态的子进程,立即返回不阻塞

cpid, status = os.waitpid(-1, os.WNOHANG)

if cpid == 0:

logging.info('no child process was immediately available')

break

exitcode = status >> 8

logging.info('child process %s exit with exitcode %s', cpid, exitcode)

except OSError as e:

if e.errno == errno.ECHILD:

logging.error('current process has no existing unwaited-for child processes.')

else:

raise

logging.info('handle SIGCHLD end')

signal.signal(signal.SIGCHLD, wait_child)

p = Process(target=run)

p.start()

while True:

time.sleep(100)

效果:

$ python test_b.py &

[1] 10159

$ 2017-07-20 21:28:56,085 - INFO - exit child process 10174 with exitcode 3

2017-07-20 21:28:56,088 - INFO - receive SIGCHLD

2017-07-20 21:28:56,089 - INFO - child process 10174 exit with exitcode 3

2017-07-20 21:28:56,090 - ERROR - current process has no existing unwaited-for child processes.

2017-07-20 21:28:56,090 - INFO - handle SIGCHLD end

$ ps aux |grep 10174

mozillazg 10194 0.0 0.0 2432788 556 s006 R+ 0:00.00 grep 10174

可以看到,子进程退出变成僵尸进程后,系统给父进程发送了 SIGCHLD 信号,我们在 SIGCHLD 信号的处理程序中通过 os.waitpid 调用 wait 系统调用后阻止了子进程一直处于僵尸进程状态,从而实现了清除僵尸进程的效果。

python function terminated un_python僵尸进程产生的原因相关推荐

  1. python function terminated un_Python: 僵尸进程的产生和清除方法

    僵尸进程产生的原因¶ 在 unix 或 unix-like 的系统中,当一个子进程退出后,它就会变成一个僵尸进程,如果父进程没有通过 wait 系统调用来读取这个子进程的退出状态的话,这个子进程就会一 ...

  2. linux僵尸进程产生的原因以及如何避免产生僵尸进程defunct

    给进程设置僵尸状态的目的是维护子进程的信息,以便父进程在以后某个时间获取.这些信息包括子进程的进程ID.终止状态以及资源利用信息(CPU时间,内存使用量等等).如果一个进程终止,而该进程有子进程处于僵 ...

  3. linux僵尸进程产生的原因以及如何避免产生僵尸进程

    给进程设置僵尸状态的目的是维护子进程的信息,以便父进程在以后某个时间获取.这些信息包括子进程的进程ID.终止状态以及资源利用信息(CPU时间,内存使用量等等).如果一个进程终止,而该进程有子进程处于僵 ...

  4. python function terminated un_绕过 RestrictedUnpickler

    阅读: 892上周跑模型的间隙,看到一个关于 Python Pickle 反序列化的 CTF 题,和以往见到的不太一样,感觉很有意思,遂打算研究一下. 0x00 Pickle 反序列化 我们知道,当我 ...

  5. python function terminated_calibre 打不开也转不了 并且出现错误 mobi转docx

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 'verbose': 2} InputFormatPlugin: MOBI Input running on C:\Users\ADMINI~1\AppD ...

  6. python function terminated_〔求助〕一份12.3M的txt文档用calibre转换格式失败

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 calibre, version 0.9.35 (win32, isfrozen: True)转换错误: 失败: 转换书籍 第 1 本,共 1 本 (时光 ...

  7. linux 僵尸进程 defunct

    在 Unix系统管理中,当用ps命令观察进程的执行状态时,经常看到某些进程的状态栏为defunct,这就是所谓的"僵尸"进程. "僵尸"进程是一个早已 死亡的进 ...

  8. 08 | 案例篇:系统中出现大量不可中断进程和僵尸进程怎么办?(下)

    上一节,我给你讲了 Linux 进程状态的含义,以及不可中断进程和僵尸进程产生的原因,我们先来简单复习下. 使用 ps 或者 top 可以查看进程的状态,这些状态包括运行.空闲.不可中断睡眠.可中断睡 ...

  9. linux僵尸进程理解,聊聊Linux系统中的僵尸进程

    车祸现场 今天下午,笔者正在认真搬砖,日志集群中有一台机器忽然报init进程占用100% CPU.strace之,发现疯狂输出如下系统调用. ~ strace -p 1 rt_sigprocmask( ...

  10. 转:Linux 僵尸进程详解

    Linux 僵尸进程详解 转载:http://www.51testing.com/?uid-225738-action-viewspace-itemid-206225 1.僵尸进程概念: 僵尸进程(Z ...

最新文章

  1. UA MATH564 概率论 QE练习题 概率极限理论
  2. Docker Review - 图形化工具 Portainer
  3. python 多条件判断 生成新列_pandas DataFrame 根据多列的值做判断,生成新的列值
  4. 微信小程序几种常用弹窗提示
  5. 关于Redis配置主从复制踩到的坑,主机不显示从机的连接信息
  6. python-发邮件脚本
  7. Linux 截取线上日志
  8. MogDB如何配置IPv6?
  9. LeetCode-105:从前序与中序遍历序列构造二叉树
  10. map文件分析 stm32_浅谈STM32的启动过程
  11. 一文搞懂前端对象的深拷贝与浅拷贝
  12. 从零基础入门Tensorflow2.0 ----二、5.2实战sklearn封装keras模型(超参数搜索)
  13. idcsystem源码php_最新PHP宝塔IDC分销系统源码
  14. JZOJ【NOI2017模拟3.30】原谅
  15. 2019年南京大学计算机考研分数线,南京大学2019年考研复试分数线已公布
  16. 数的大家庭——虚数和复数的由来
  17. ppt怎么制作抖音快手快闪效果的倒计时动画?
  18. pc端和移动端集成第三方快捷登录 --- 微博为例
  19. Python挑战游戏( PythonChallenge)闯关之路Level- 1
  20. Top10响应式手机网页开发软件

热门文章

  1. 双足机器人的稳定性判据_仿人双足机器人步态规划——零力矩点(ZMP)
  2. vue项目添加emoji表情
  3. 2020-10-13 Comsol学习1
  4. uniapp横竖屏切换
  5. 利用cad计算型材的弹性模量_已知阶梯形直杆受力如图所示,材料的弹性模量
  6. vue中手机号码+座机号码、邮箱正则校验规则封装
  7. 浅谈信息学奥赛NOIP
  8. ehcache使用java_Java分布式缓存框架Ehcache 使用(一)
  9. TabLayout+Viewpage滑动
  10. 安信可推荐 | 安信可ESP-C3模组和ESP32-S3模组的软件和硬件对比区别