How to wait for exit of non-children processes

前端 未结 9 2419
生来不讨喜
生来不讨喜 2020-11-27 18:45

For child processes, the wait() and waitpid() functions can be used to suspends execution of the current process until a child has exited. But t

9条回答
  •  醉话见心
    2020-11-27 19:37

    So far I've found three ways to do this on Linux:

    • Polling: you check for the existence of the process every so often, either by using kill or by testing for the existence of /proc/$pid, as in most of the other answers
    • Use the ptrace system call to attach to the process like a debugger so you get notified when it exits, as in a3nm's answer
    • Use the netlink interface to listen for PROC_EVENT_EXIT messages - this way the kernel tells your program every time a process exits and you just wait for the right process ID. I've only seen this described in one place on the internet.

    Shameless plug: I'm working on a program (open source of course; GPLv2) that does any of the three.

提交回复
热议问题