在 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 变成了僵尸进程。
既然已经可以控制僵尸进程的产生了,那我们就可以进入下一步如何清除僵尸进程了。
清除僵尸进程有两种方法:
"htmlcode">
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 系统调用后阻止了子进程一直处于僵尸进程状态,从而实现了清除僵尸进程的效果。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]