tsk
tsk

Reputation: 1

What are the ways to check a process with specific pid exists and active?

What are the ways to check a process with specific pid exists and active (not zombie)? I am using Ubuntu Linux with gcc.

Upvotes: 0

Views: 81

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120229

You should understand that by the time you decide to something with this process, it may be long gone (google TOCTOU).

Anyway, the POSIX way is to call kill (0, pid) and check the errno. ESRCH would mean "no such process" and EPERM is "the process exists but you cannot signal it".

A Linux-specific way would be reading the /proc directory. Entries there correspond to PIDs (there are also some entries which are not numeric, you can ignore them). Not all Linux systems use /proc though.

If you are using SELINUX there might be processes you are not allowed to even know about, but since you can't do much about it, don't even worry.

Upvotes: 3

Related Questions