1

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 Apr 10 '16 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. – user3629249 Apr 10 '16 at 18:23
  • 1
    @user3629249 That's completely wrong. PID values are reused. – Gilles Apr 10 '16 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. – Gilles Apr 11 '16 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. – Gilles Apr 12 '16 at 7:26
2

/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

edit:

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 Apr 10 '16 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 – user3629249 Apr 10 '16 at 18:27
  • How would wait return before the child process has exited? – G-Man Apr 15 '16 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.