11

is it possible that a folder like /proc/4587 exists even though there isn't a process that has PID 4587?

  • 1
    Are you wondering if checking for /proc/N is a safe/reliable way to see if PID N is running? – Andy Lester Sep 8 '17 at 15:21
  • I was wondering if theoretically there could be residue in /proc after process is gone. – David Dai Sep 12 '17 at 9:11
25

If /proc is only the proc mount (and no one is playing tricks with overlays), no, a pid-based folder only exists as long as the corresponding process exists in some state (including as a zombie). In fact, just before returning a directory entry for a process id, the kernel re-validates the process’ existence — so at the instant a directory entry is returned, the corresponding process is still there. Accessing a directory also starts by looking up the corresponding process. (If the line numbers change, look for proc_pident_instantiate and proc_pident_lookup.)

You can run into issues caused by listing /proc and using the results later (even a few microseconds later): a process can be running when you list /proc, and stop before you act on the results.

  • 3
    What about processes that finished execution but wait() hadn't been called on them? – el.pescado Sep 8 '17 at 9:30
  • 2
    Good point @el.pescado, a quick check indicates that zombie processes still have their directory. I’ll update my answer, thanks! – Stephen Kitt Sep 8 '17 at 9:36
  • Basically, if ps shows the process, its /proc directory should exist. – Barmar Sep 8 '17 at 22:18
  • @Barmar “must” in fact: ps accesses /proc to find the information it displays. – Stephen Kitt Sep 9 '17 at 8:27

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.