• C

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??
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kill() - if the signal sent is 0 this will just let you know if the process is running or not...

int rc;

rc = kill( pid, 0 );
if( rc != 0 ) {
    if( errno == ESRCH ) {
        // process not running
    else {
       // failed to check the process status

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day 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.
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

>>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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.