I have a process that starts a child process and when the child exits, scans /proc/<pid>/stat to gather some information about the CPU and memory the child used. However I am worried that I am living on borrowed time here. How long after wait() returns or SIGCHLD is sent/signal handler is called, will it actually hang around? It would be super convenient if they lasted until the PID needed to be reused but that's clearly not what's happening.

  • 1
    If you are using C, see also getrusage(2) and times(2).
    – meuh
    Commented Apr 10, 2016 at 8:34
  • pid values are not re-used. The count will be incremented with each new process. The only 'reasonable' method of resetting the count is to re-boot the computer. Commented Apr 10, 2016 at 18:23
  • 1
    @user3629249 That's completely wrong. PID values are reused. Commented Apr 10, 2016 at 20:39
  • 1
    /proc/<pid>/stat no longer exists by the time wait returns. You mention a short delay during which it still exists, but that doesn't jibe with my reading of the code, nor with my understanding of the architecture (the data is supposed to be reclaimed when the parent calls wait), and I cannot reproduce this on Debian jessie. What kernel version are you using? Are you absolutely sure about the timing and that you're hitting the right child? In any case, it's clearly not something you can count on. Commented Apr 11, 2016 at 0:30
  • 1
    @user3629249 Once again, no. Try it on Linux, for example: if you leave it running for long enough, eventually the PIDs reach 32767, which is the maximum value, and then they start again at the first free PID. If PIDs were never reused, that would mean that the system would lock up after a certain number of forks! How long you have before a PID is reused depends on how long the process survived: if it survived for about 32700 forks, the PID could be the next one to be reused. Commented Apr 12, 2016 at 7:26

1 Answer 1


/proc/[pid] disappears when the program exits. See this: https://superuser.com/questions/365576/lifetime-of-the-symlinks-from-the-file-descriptors-in-proc-pid-fd


The wait man page says

The wait() function shall suspend execution of the calling thread until status information for one of the terminated child processes of the calling process is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process.

So it is possible that a signal to terminate the child process is delivered. Then wait returned, but the child process has not yet exited. So you can still read /proc/pid/stat. When the child process exits, /proc/pid/stat disappears.

  • It doesn't tho', or at least, it's not as simple as that. My wait() has returned and I have received SIGCHLD but my child process's entry in /proc is still there long enough for me to take a quick peek.
    – Gaius
    Commented Apr 10, 2016 at 9:09
  • the code gets the childs PID when the call to fork() is made to create the child process. Suggest examining the /proc pid sub directory while the child is still running Commented Apr 10, 2016 at 18:27
  • How would wait return before the child process has exited? Commented Apr 15, 2016 at 0:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .