Search code examples
cunixprocessposixkill

What kills the parent process?


What ends the parent process in this code? I see don't see him exiting anywhere... A friend of mine says that the parent isn't ended/kiled, the code simply stops at the last exit(0). If this is the case, then what wakes the parent process from sleep? Thank you in advance

adghtesd

the results of the program:

    parent: I'm the parent
    child: I'm the child
    child: I'm exiting
    parent: child process (PID=1919) exited with value 123
    parent: exiting
    test

Solution

  • From this POSIX reference on sleep:

    If sleep() returns because the requested time has elapsed, the value returned shall be 0. If sleep() returns due to delivery of a signal, the return value shall be the "unslept" amount (the requested time minus the time actually slept) in seconds.

    What happens for you is that when the parent process receives the SIGCHLD signal, it causes the sleep function to be interrupted, and the parent continues after the sleep call.

    If you want to sleep the full amount, you need to check what sleep returns, and call sleep in a loop until it returns 0.