使用prctl函数,可以在父进程终止时将SIGKILL发送到分叉进程 . 这个C的实现将在分支进程后简单地添加 prctl(PR_SET_PDEATHSIG, SIGKILL) 行 . 但是,我试图在python中做同样的事情;即使父进程成功完成(或者我用kill终止它),子进程也会继续运行 . 在下面的演示实现中,父节点在一段时间后完成,但子进程继续运行 . 任何想法如何解决这一问题?

def child():
    print 'Child Process created with ID: %d ' %os.getpid()
    while True: 
        print 'PROCESS ID: %d ' %os.getpid() 
        time.sleep(2)  

def parent():
    x = 0
    newpid = os.fork()
    if newpid > 0:
        libc.prctl(PR_SET_PDEATHSIG, SIGKILL)
    child()
    else:
        print "Parent process running with ID:  %d " % os.getpid()
        while (x<5):
            print 'PROCESS ID: %d ' %os.getpid() 
            time.sleep(2)
            x+=1 
parent()