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 Answer
/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 receivedSIGCHLD
but my child process's entry in/proc
is still there long enough for me to take a quick peek.– GaiusApr 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 Apr 10, 2016 at 18:27 -
How would
wait
return before the child process has exited? Apr 15, 2016 at 0:56
getrusage(2)
andtimes(2)
.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./proc/<pid>/stat
no longer exists by the timewait
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 callswait
), 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.