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

Posted on 2004-11-23
Last Modified: 2010-04-15
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??
Question by:hauto
    LVL 11

    Expert Comment

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

    Accepted Solution


    int rc;

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

    Expert Comment

    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.
    LVL 5

    Expert Comment

    >>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.
    LVL 11

    Expert Comment

    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 :-)

    LVL 1

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    Directory does exist 19 138
    Beginner Probleme with my IDE Code::Blocks C++ 5 224
    Pointer in one class to member in another 6 102
    Adjust Mfcapp 29 142
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now