1

OS X 10.4/5/6 の場合:

子を生成する親プロセスがあります。子供を殺さずに親を殺したい。出来ますか?どちらのアプリでもソースを変更できます。

4

2 に答える 2

5

NSDが尋ねたように、それは本当にそれがどのように生成されるかに依存します. たとえば、シェル スクリプトを使用している場合は、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() 

これは、これまでに書かれた最高の本の 1 つです。これらのトピックを非常に詳細にカバーしています。

Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series) (ペーパーバック)

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

5つ星のAmazonレビュー(私は6つ付けます)。

于 2009-12-23T01:22:10.133 に答える
3

親がシェルであり、実行時間の長いプロセスを起動してからログアウトする場合は、nohup (1)またはを検討してdisownください。

子のコーディングを制御する場合は、SIGHUP をトラップして、デフォルト以外の方法で処理することができます (完全に無視するなど)。これについては、signal (3)およびsigaction (2)man ページを参照してください。いずれにせよ、StackOverflow に関する既存の質問がいくつかあり、助けが必要です。

于 2009-12-23T01:19:42.500 に答える