达尔文:如何在不杀害孩子的情况下杀死父母进程?

Sta*_*dog 0 unix macos bsd

在OS X 10.4/5/6上:

我有一个产生孩子的父进程.我想在不杀死孩子的情况下杀死父母.可能吗?我可以在任一app上修改源代码.

gah*_*ooa 5

正如NSD所问,这实际上取决于它是如何产生的.例如,如果您使用的是shell脚本,则可以使用该nohup命令来运行子进程.

如果你正在使用fork/exec,那么它有点复杂,但没有太多.

来自http://code.activestate.com/recipes/66012/

import sys, os 

def main():
    """ A demo daemon main routine, write a datestamp to 
        /tmp/daemon-log every 10 seconds.
    """
    import time

    f = open("/tmp/daemon-log", "w") 
    while 1: 
        f.write('%s\n' % time.ctime(time.time())) 
        f.flush() 
        time.sleep(10) 


if __name__ == "__main__":
    # do the UNIX double-fork magic, see Stevens' "Advanced 
    # Programming in the UNIX Environment" for details (ISBN 0201563177)
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit first parent
            sys.exit(0) 
    except OSError, e: 
        print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) 
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/") 
    os.setsid() 
    os.umask(0) 

    # do second fork
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit from second parent, print eventual PID before
            print "Daemon PID %d" % pid 
            sys.exit(0) 
    except OSError, e: 
        print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) 
        sys.exit(1) 

    # start the daemon main loop
    main() 
Run Code Online (Sandbox Code Playgroud)

这是有史以来最好的书之一.它非常详细地介绍了这些主题.

UNIX环境下的高级编程,第二版(Addison-Wesley专业计算系列)(平装)

ISBN-10:0321525949
ISBN-13:978-0321525949

5星亚马逊评论(我给它6).