python僵尸进程产生的原因


Posted in Python onJuly 21, 2017

在 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 相关文章推荐
浅析Python数据处理
May 02 Python
python实现两个经纬度点之间的距离和方位角的方法
Jul 05 Python
详解Django-channels 实现WebSocket实例
Aug 22 Python
python装饰器练习题及答案
Nov 01 Python
关于numpy数组轴的使用详解
Dec 05 Python
python3 pathlib库Path类方法总结
Dec 26 Python
python 伯努利分布详解
Feb 25 Python
Django+Celery实现动态配置定时任务的方法示例
May 26 Python
通过cmd进入python的步骤
Jun 16 Python
python自动从arxiv下载paper的示例代码
Dec 05 Python
python openpyxl模块的使用详解
Feb 25 Python
python - timeit 时间模块
Apr 06 Python
python下载图片实现方法(超简单)
Jul 21 #Python
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
Jul 20 #Python
Python使用内置json模块解析json格式数据的方法
Jul 20 #Python
Python轻量级ORM框架Peewee访问sqlite数据库的方法详解
Jul 20 #Python
Python函数式编程
Jul 20 #Python
python 换位密码算法的实例详解
Jul 19 #Python
python实现rsa加密实例详解
Jul 19 #Python
You might like
PHP5 操作MySQL数据库基础代码
2009/09/29 PHP
PHP入门教程之图像处理技巧分析
2016/09/11 PHP
THINKPHP5分页数据对象处理过程解析
2020/10/28 PHP
asp(javascript)全角半角转换代码 dbc2sbc
2009/08/06 Javascript
事件冒泡是什么如何用jquery阻止事件冒泡
2013/03/20 Javascript
关于JavaScript对象的动态选择及遍历对象
2014/03/10 Javascript
js原型继承的两种方法对比介绍
2014/03/30 Javascript
Node.js中的事件驱动编程详解
2014/08/16 Javascript
jquery中one()方法的用法实例
2015/01/16 Javascript
jQuery中triggerHandler()方法用法实例
2015/01/19 Javascript
jQuery表单事件实例代码分享
2016/08/18 Javascript
微信小程序 如何引入外部字体库iconfont的图标
2018/01/31 Javascript
通过jquery toggleClass()属性制作文章段落更改背景颜色
2018/05/21 jQuery
vue.js自定义组件directives的实例代码
2018/11/09 Javascript
微信小程序封装分享与分销功能过程解析
2019/08/13 Javascript
vue组件添加事件@click.native操作
2020/10/30 Javascript
Python写的Socks5协议代理服务器
2014/08/06 Python
跟老齐学Python之有点简约的元组
2014/09/24 Python
Python编程中对super函数的正确理解和用法解析
2016/07/02 Python
Python3.6连接Oracle数据库的方法详解
2018/05/18 Python
Python实现string字符串连接的方法总结【8种方式】
2018/07/06 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
Python Pandas 获取列匹配特定值的行的索引问题
2019/07/01 Python
Python如何将函数值赋给变量
2020/04/28 Python
Python并发爬虫常用实现方法解析
2020/11/19 Python
亚马逊西班牙购物网站:amazon西班牙
2017/03/06 全球购物
美国在线肉类和海鲜配送:Crowd Cow
2020/10/02 全球购物
工作自我评价怎么写
2014/01/29 职场文书
2014年培训工作总结范文
2014/11/27 职场文书
餐厅服务员岗位职责
2015/02/09 职场文书
技术员岗位职责范本
2015/04/11 职场文书
主题班会开场白
2015/06/01 职场文书
实习指导老师意见
2015/06/04 职场文书
六一亲子活动感想
2015/08/07 职场文书
2019假期福利管理制度!
2019/07/15 职场文书
MySQL Shell import_table数据导入的实现
2021/08/07 MySQL