Skip to contentSkip to conversation

Check if a process exists?

DISCUSSION Hareesh Nagarajan 2 participants 2 messages Mar 1, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

Can a process check if a given PID exists or not? In other words can a process check if an unrelated process is alive? Is there any system call that does this?

I can think of a (few) work arounds to this:

1. open("/proc/pid_in_question") will return true if the process with pid = pid_in_question is alive.

2. lock a file called ``pid_in_question". Test the lock from the other process. If the process is dead then there is no lock on the file ``pid_in_question".

Any ideas/comments?

Thanks,

Hareesh

#1 lore raw
Quoted 6 lines · Hareesh Nagarajan

Hi,

Can a process check if a given PID exists or not? In other words can a process check if an unrelated process is alive? Is there any system call that does this?

Use kill() with a second argument being zero.

    if (kill(pid, 0) == -1)
    {
       /* Process is not there. */
    }
    else
    {
       /* Process is alive. */
    }

Holger