天天看點

Python父程序退出後,子程序自動退出的辦法

PR_SET_PDEATHSIG (since Linux 2.1.57)

Set the parent process death signal of the calling process to arg2

(either a signal value in the range 1..maxsig, or 0 to clear). This is

the signal that the calling process will get when its parent dies.

This value is cleared for the child of a fork(2).

import ctypes 

libc = ctypes.CDLL('libc.so.6')  

pid = os.fork() 
if pid == 0:  
    libc.prctl(1, 15)  
    # something in child

elif pid > 0:
    # something in child      

libc.prctl(1, 15) 是起作用的語句。

繼續閱讀