欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python僵尸進(jìn)程產(chǎn)生的原因

 更新時(shí)間:2017年07月21日 08:54:27   作者:Huang Huang  
這篇文章主要給大家講解的是在Python中是如何產(chǎn)生僵尸進(jìn)程的,以及如何清除僵尸進(jì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)文章

最新評(píng)論