如何等待父进程退出子进程?

时间:2018-02-09 10:01:20

标签: python linux fork

我想分叉一个进程并等待父进程退出,然后再对子进行操作。

1 个答案:

答案 0 :(得分:6)

天真的方式就是这样一个繁忙的循环:

# BAD WAY!
pid = os.fork()
if pid == 0:
    while True:
        if os.path.isdir("/proc/%s" % pid):
            break
        time.sleep(0.1)
    # Parent is dead there.

但这很容易受到PID重用问题的影响。如果在父项退出并获得其PID之后立即创建了另一个进程,则该子进程将永远不会退出。

另一种方法是在特定文件上使用flock()。但它不起作用,因为孩子与父母共享相同的锁。

这样做的一个万无一失的方法是使用一个特殊技巧:在父级中创建一个管道,在孩子中,你只需等到你得到一个EOF。

# Good way
read_fd, write_fd = os.pipe()

pid = os.fork()
if pid > 0:
    # Close the read pipe, so that only the child is the reader.
    os.close(read_fd)

    # It is important to voluntarily leak write_fd there,
    # so that the kernel will close it for the parent process
    # when it will exit, triggering our trick.

elif pid == 0:
    # Daemon ourselves first.
    os.setsid()
    for fd in {0, 1, 2}:
        os.close(fd)

    # Close the write pipe so that the parent is the only writer.
    # This will make sure we then get an EOF when the only writer exits.
    os.close(write_fd)

    # Now, we're waiting on the read pipe for an EOF.
    # PS: the assert is not necessary in production...
    assert os.read(read_fd, 1) == ""
    os.close(read_fd)

    # At this point, the parent is dead.