Search code examples
cforkwaitpid

What happens when two processes wait for the same child?


From what I've read the default behavior for wait/waitpid is to wait for a state change in a process. What I can't find is the expected behavior of two processes waitpid using the same pid_t argument.

Do both return and continue execution, or is it a race condition where only one notices the state change?


Solution

  • Only the parent can wait() for a process, and a process can of course have only one parent.

    The parent process might, however, have multiple threads. In the case of multiple threads waiting for the same child, POSIX specifies that only one of them will see the state change. To allow multiple threads to see the state change, you must use waitid() with the WNOWAIT flag.

    POSIX: status information