12

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

2
  • 2
    Are you wondering if checking for /proc/N is a safe/reliable way to see if PID N is running? Commented Sep 8, 2017 at 15:21
  • I was wondering if theoretically there could be residue in /proc after process is gone. Commented Sep 12, 2017 at 9:11

1 Answer 1

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.

4
  • 4
    What about processes that finished execution but wait() hadn't been called on them? Commented Sep 8, 2017 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! Commented Sep 8, 2017 at 9:36
  • Basically, if ps shows the process, its /proc directory should exist.
    – Barmar
    Commented Sep 8, 2017 at 22:18
  • @Barmar “must” in fact: ps accesses /proc to find the information it displays. Commented Sep 9, 2017 at 8:27

You must log in to answer this question.

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