python僵尸進(jìn)程產(chǎn)生的原因
在 unix 或 unix-like 的系統(tǒng)中,當(dāng)一個(gè)子進(jìn)程退出后,它就會(huì)變成一個(gè)僵尸進(jìn)程,如果父進(jìn)程沒(méi)有通過(guò) wait 系統(tǒng)調(diào)用來(lái)讀取這個(gè)子進(jìn)程的退出狀態(tài)的話,這個(gè)子進(jìn)程就會(huì)一直維持僵尸進(jìn)程狀態(tài)。
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.
并且僵尸進(jìn)程無(wú)法通過(guò) kill 命令來(lái)清除。
本文將探討如何手動(dòng)制造一個(gè)僵尸進(jìn)程以及清除僵尸進(jìn)程的辦法。
手動(dòng)制造一個(gè)僵尸進(jìn)程
為了便于后面講解清除僵尸進(jìn)程的方法,我們使用日常開(kāi)發(fā)中經(jīng)常使用的 multiprocessing 模塊來(lái)制造僵尸進(jìn)程(準(zhǔn)確的來(lái)說(shuō)是制造一個(gè)長(zhǎng)時(shí)間維持僵尸進(jìn)程狀態(tài)的子進(jìn)程):
$ 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)
測(cè)試:
$ 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)
可以看到,子進(jìn)程 10091 變成了僵尸進(jìn)程。
既然已經(jīng)可以控制僵尸進(jìn)程的產(chǎn)生了,那我們就可以進(jìn)入下一步如何清除僵尸進(jìn)程了。
清除僵尸進(jìn)程有兩種方法:
•第一種方法就是結(jié)束父進(jìn)程。當(dāng)父進(jìn)程退出的時(shí)候僵尸進(jìn)程隨后也會(huì)被清除。
• 第二種方法就是通過(guò) wait 調(diào)用來(lái)讀取子進(jìn)程退出狀態(tài)。我們可以通過(guò)處理 SIGCHLD 信號(hào),在處理程序中調(diào)用 wait 系統(tǒng)調(diào)用來(lái)清除僵尸進(jìn)程。
處理 SIGCHLD 信號(hào)
子進(jìn)程退出時(shí)系統(tǒng)會(huì)向父進(jìn)程發(fā)送 SIGCHLD 信號(hào),父進(jìn)程可以通過(guò)注冊(cè) SIGCHLD 信號(hào)處理程序,在信號(hào)處理程序中調(diào)用 wait
系統(tǒng)調(diào)用來(lái)清理僵尸進(jìn)程。 $ 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 表示任意子進(jìn)程
# os.WNOHANG 表示如果沒(méi)有可用的需要 wait 退出狀態(tài)的子進(jìn)程,立即返回不阻塞
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
可以看到,子進(jìn)程退出變成僵尸進(jìn)程后,系統(tǒng)給父進(jìn)程發(fā)送了 SIGCHLD 信號(hào),我們?cè)?SIGCHLD 信號(hào)的處理程序中通過(guò) os.waitpid 調(diào)用 wait 系統(tǒng)調(diào)用后阻止了子進(jìn)程一直處于僵尸進(jìn)程狀態(tài),從而實(shí)現(xiàn)了清除僵尸進(jìn)程的效果。
相關(guān)文章
Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實(shí)際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07
Django ORM判斷查詢結(jié)果是否為空,判斷django中的orm為空實(shí)例
這篇文章主要介紹了Django ORM判斷查詢結(jié)果是否為空,判斷django中的orm為空實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Python&&GDAL實(shí)現(xiàn)NDVI的計(jì)算方式
今天小編就為大家分享一篇Python&&GDAL實(shí)現(xiàn)NDVI的計(jì)算方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python中線程threading.Thread的使用詳解
python的thread模塊是比較底層的模塊,python的threading模塊是對(duì)thread做了一些包裝的,可以更加方便的被使用。本文將為大家詳細(xì)介紹一下python中的線程threading.Thread()的使用,需要的可以參考一下2022-07-07
tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法
今天小編就為大家分享一篇tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
jupyter?notebook加載和運(yùn)行.py文件方式
這篇文章主要介紹了jupyter?notebook加載和運(yùn)行.py文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Python使用xlrd模塊操作Excel數(shù)據(jù)導(dǎo)入的方法
這篇文章主要介紹了Python使用xlrd模塊操作Excel數(shù)據(jù)導(dǎo)入的方法,涉及Python操作xlrd模塊的技巧,需要的朋友可以參考下2015-05-05
Python supervisor強(qiáng)大的進(jìn)程管理工具的使用
這篇文章主要介紹了Python supervisor強(qiáng)大的進(jìn)程管理工具的使用,本文主要跟大家分享在類unix操作系統(tǒng)下supervisor的使用以及一些關(guān)于進(jìn)程的知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03

