[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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??
0
hauto
Asked:
hauto
  • 4
1 Solution
 
cjjcliffordCommented:
kill() - if the signal sent is 0 this will just let you know if the process is running or not...
0
 
cjjcliffordCommented:
e.g.

int rc;

rc = kill( pid, 0 );
if( rc != 0 ) {
    if( errno == ESRCH ) {
        // process not running
    }
    else {
       // failed to check the process status
    }
}
0
 
cjjcliffordCommented:
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
van_dyCommented:
>>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.
0
 
cjjcliffordCommented:
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 :-)

0
 
smiththomasgCommented:
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.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now