There are two cases to consider for each pid when it terminates: 1) The process is our child. In this case, we will reap the process and notify with the exit status. 2) The process was not our child. In this case, it will be reaped by someone else (its parent or init, if reparented) so we cannot know the exit status and we must notify with None(). NOTE: A child can only be reaped by us, the paren
| 98 | // between waitpid and the (!exists) conditional it will still exist as a |
| 99 | // zombie; it will be reaped by us on the next loop. |
| 100 | foreach (pid_t pid, promises.keys()) { |
| 101 | int status; |
| 102 | Result<pid_t> child_pid = os::waitpid(pid, &status, WNOHANG); |
| 103 | if (child_pid.isSome()) { |
| 104 | // We have reaped a child. |
| 105 | notify(pid, status); |
| 106 | } else if (!os::exists(pid)) { |
| 107 | // The process no longer exists and has been reaped by someone else. |
| 108 | notify(pid, None()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | delay(interval(), self(), &ReaperProcess::wait); // Reap forever! |
| 113 | } |