Link to home
Start Free TrialLog in
Avatar of hauto
hauto

asked on

Given a pid, how can I tell if it's a running program?

I am writing c program on RH Linux 7.3, it's a client-server program using socket.
I use getid to get a process's pid, but how can I check if the process is running or not (with the pid obtained)? Any command to do that??
Avatar of cjjclifford
cjjclifford

kill() - if the signal sent is 0 this will just let you know if the process is running or not...
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
taken from man(3) kill:

     The kill() function sends the signal given by sig to pid, a process or a
     group of processes.  Sig may be one of the signals specified in
     sigaction(2) or it may be 0, in which case error checking is performed
     but no signal is actually sent.  This can be used to check the validity
     of pid.
>>I use getid to get a process's pid,

      getid() or getpid() ??

i wonder if there is anything like getid() on linux, or is there?
correct me if i am wrong.
yes, on linux getpid() returns the PID of the process making the call and getppid() gets the parent's PID.
I can't find a man entry for getid().

Thinking about this... there's no need to check if the process identified by getpid() is running - this is the process that made the call itself, so obviously running, although the kill(pid,0) method would still work, its just redundant :-)

In Linux, each process has a file:
/proc/<pid #>/status
You can open these files and read them.  
Find one of these files and cat it out.  Interesting huh ?
You can use this to "walk" through the process table.