Linux – Can /proc/PID still exist when PID isn’t running

linuxprocprocess

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

Best Answer

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.