Professional Web Applications Themes

How Do I Determine if a Specific Process Exists? - UNIX Programming

What's the most straightforward way to implement a function bool process_exists(::pid_t pid); in C/C++? On Solaris 8, it appears sufficient to check for the existence of a directory named "/proc/pid", where the pid would be rendered as "%1lu". However, there is at least one flavor of GNU/Linux which confines the contents of /proc to only the processes that are owned by the same real (effective?) UID. That is, in such a system, user "foo" can use the above technique only if the process in question is also being run by "foo". That being the case, 1. Is there a portable ...

  1. #1

    Default How Do I Determine if a Specific Process Exists?

    What's the most straightforward way to implement a function

    bool process_exists(::pid_t pid);

    in C/C++?

    On Solaris 8, it appears sufficient to check for the existence of a
    directory named "/proc/pid", where the pid would be rendered as
    "%1lu".

    However, there is at least one flavor of GNU/Linux which confines the
    contents of /proc to only the processes that are owned by the same
    real (effective?) UID. That is, in such a system, user "foo" can use
    the above technique only if the process in question is also being run
    by "foo".

    That being the case,

    1. Is there a portable way to determine if a particular process,
    identified by its ::pid_t process ID, not necessarily running under
    the auspices of the same real user (effective?) ID, exists on the
    local host?

    2. Is there a better way to determine if a particular process,
    identified by its ::pid_t process ID and assumed to be running under
    the auspices of the same real (effective?) user ID, exists on the
    local host?


    Thanks.
    John Guest

  2. #2

    Default Re: How Do I Determine if a Specific Process Exists?

    org (John Michael Davison) writes:
     

    This is very much OS dependent (as you have found out yourself). I guess
    you can always run the ps(1) program with appropriate arguments and p
    its output. Not every UNIX has /proc FS.

    Bye, Dragan

    --
    Dragan Cvetkovic,

    To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

    !!! Sender/From address is bogus. Use reply-to one !!!
    Dragan Guest

  3. #3

    Default Re: How Do I Determine if a Specific Process Exists?

    Dragan Cvetkovic <net> writes:
     
    >
    > This is very much OS dependent (as you have found out yourself). I guess
    > you can always run the ps(1) program with appropriate arguments and p
    > its output. Not every UNIX has /proc FS.
    >[/ref]

    Or send signal 0 using kill(2) function. But you might need certain
    privilegies to send signals to unrelated processes.

    Bye, Dragan


    --
    Dragan Cvetkovic,

    To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

    !!! Sender/From address is bogus. Use reply-to one !!!
    Dragan Guest

  4. #4

    Default Re: How Do I Determine if a Specific Process Exists?

    Dragan Cvetkovic wrote: [/ref][/ref]
     

    kill(2) is supposed to return with errno ESRCH if the PID doesn't exist
    and EPERM if there is no permission. So if kill() returns -1 and errno
    is ESRCH, then that process doesn't exist. (But zombies will also exist).

    Robert
    Robert Guest

  5. #5

    Default Re: How Do I Determine if a Specific Process Exists?



    Dragan Cvetkovic wrote:
     
    >>
    >>This is very much OS dependent (as you have found out yourself). I guess
    >>you can always run the ps(1) program with appropriate arguments and p
    >>its output. Not every UNIX has /proc FS.
    >>[/ref]
    >
    >
    > Or send signal 0 using kill(2) function. But you might need certain
    > privilegies to send signals to unrelated processes.[/ref]

    retval = kill(pid,0);

    performs error checking thus it will return an error
    if the process does not exist, or if you do
    not have permissions to send a signal to
    the process.

    However, you should check the errno.
    errno == ESRCH means that the process does not exist.
    errno == EPERM *IMPLIES* that the process
    exists but you do not have permissions
    to send it a signal.

    Under the assumption that it first searches for
    the existence of a process, THEN checks your
    permissions, that ought to do it, and should
    be portable.
     

    --

    "It is impossible to make anything foolproof because fools are so
    ingenious" - A. Bloch

    Nick Guest

  6. #6

    Default Re: How Do I Determine if a Specific Process Exists?

    org (John Michael Davison) writes:
     

    It's not only system specific, but practically speaking impossible to
    determine if a given pid is associated with the program you think it
    is, if pids get recycled.

    If you have control over the two programs, the best way to test for
    existence is for the process to create and lock a file with a
    predetermined name. The process wanting to test for existence can try
    to lock that file. If it can, then the program you're interested in
    isn't running. If it can't lock the file, then the program is still
    running because it still holds the lock on the file.

    This is a lot more portable than depending on /proc entries.

    Joe
    --
    "I didn't really say everything I said."
    - Yogi Berra
    joe@invalid.address Guest

  7. #7

    Default Re: How Do I Determine if a Specific Process Exists?

    in comp.unix.questions i read: [/ref]
     [/ref][/ref]
     [/ref]
     

    there is a different errno value for each:

    [EPERM]
    The process does not have permission to send the signal to any
    receiving process.

    [ESRCH]
    No process or process group can be found corresponding to that
    specified by pid.

    --
    a signature
    those Guest

  8. #8

    Default Re: How Do I Determine if a Specific Process Exists?

    On Thu, 19 Feb 2004 18:12:16 GMT, address <address> wrote: 
    >
    > It's not only system specific, but practically speaking impossible to
    > determine if a given pid is associated with the program you think it
    > is, if pids get recycled.
    >
    > If you have control over the two programs, the best way to test for
    > existence is for the process to create and lock a file with a
    > predetermined name. The process wanting to test for existence can try
    > to lock that file. If it can, then the program you're interested in
    > isn't running. If it can't lock the file, then the program is still
    > running because it still holds the lock on the file.
    >
    > This is a lot more portable than depending on /proc entries.
    >
    > Joe
    > --
    > "I didn't really say everything I said."
    > - Yogi Berra[/ref]


    This person is a serious troll, and you should be VERY careful about
    taking any technical advice from him.

    He is also quite young and frequently pretends to technical knowledge
    he just doesn't have. He's happy to experiment with YOUR box.

    Here's a recent thread where he made an un-provoked and serious accusation
    against me, and then failed miserably to substantiate it:

    Here, he was coming to the defense of another troll.

    (the notorious "Mike" who harassed Michael Heim nearly to death on
    alt.os.linux.misc)


    ================================================== =======================

    From yyy Wed Feb 18 21:32:27 2004
    Path: newsspool2.news.pas.earthlink.net!stamper.news.pas .earthlink.net!newsread1
    ..news.pas.earthlink.net.POSTED!65807237!not-for-mail
    Newsgroups: comp.unix.shell
    From: Alan Connor <yyy>
    Subject: Re: script quesation
    References: <google.com> <m3smh7n780.fsfin
    valid.address> <mDVYb.9586$news.pas.earthlink.net> <m3n07fn4d
    address>
    Reply-To: zzz
    User-Agent: slrn/0.9.7.3 (Linux)
    Lines: 77
    Message-ID: <uCWYb.9667$news.pas.earthlink.net>
    Date: Thu, 19 Feb 2004 04:05:46 GMT
    NNTP-Posting-Host: 63.187.193.83
    X-Complaints-To: net
    X-Trace: newsread1.news.pas.earthlink.net 1077163546 63.187.193.83 (Wed, 18 Feb
    2004 20:05:46 PST)
    NNTP-Posting-Date: Wed, 18 Feb 2004 20:05:46 PST
    Organization: EarthLink Inc. -- http://www.EarthLink.net
    Xref: news.earthlink.net comp.unix.shell:148391
    X-Received-Date: Wed, 18 Feb 2004 20:05:54 PST (newsspool2.news.pas.earthlink.ne
    t)



    On Thu, 19 Feb 2004 03:18:53 GMT, address <address> wrot
    e: 
    >
    > You have suspected me of being three or four people that I wasn't.[/ref]

    That is an out-and-out lie.

    Post the articles where I falsely accused you of impersonating someone,
    with the full headers.

    (anyone who would post something like this without the articles to back
    his accusations up, is surely capable of trolling. If I accused you of
    this, then you DID it. and the headers will reveal the truth)

    You will receive this request, on any newsgroup you are on, from now until
    you apologize to me. Along with a copy of this response.

    People need to know who they are dealing with.


    This post in its entirety:

    ----------------------------------------


    Path: newsspool2.news.pas.earthlink.net!stamper.news.pas .earthlink.net!elnk-nf2-
    pas!newsfeed.earthlink.net!wn53feed!worldnet.att.n et!attbi_s03.POSTED!not-for-ma
    il
    Newsgroups: comp.unix.shell
    Subject: Re: script quesation
    References: <google.com> <m3smh7n780.fsfin
    valid.address> <mDVYb.9586$news.pas.earthlink.net>
    From: address
    Message-ID: <address>
    Lines: 14
    User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2
    MIME-Version: 1.0
    Content-Type: text/plain; cht=us-ascii
    NNTP-Posting-Host: 24.1.102.108
    X-Complaints-To: net
    X-Trace: attbi_s03 1077160733 24.1.102.108 (Thu, 19 Feb 2004 03:18:53 GMT)
    NNTP-Posting-Date: Thu, 19 Feb 2004 03:18:53 GMT
    Organization: Comcast Online
    Date: Thu, 19 Feb 2004 03:18:53 GMT
    Xref: news.earthlink.net comp.unix.shell:148389
    X-Received-Date: Wed, 18 Feb 2004 19:18:54 PST (newsspool2.news.pas.earthlink.ne
    t)



    Alan Connor <yyy> writes:
     

    You have suspected me of being three or four people that I wasn't. I
    guess I'd rather just try to deal with questions, even if they're
    posted in a braindead doood style, until I have some reason to think
    it's something other than what it is.

    Joe
    --
    "I didn't really say everything I said."
    - Yogi Berra

    ------------------------------------------------





    And here's his response:

    ---------------------------------------------------------------------


    Newsgroups: comp.unix.shell
    Subject: Re: script quesation
    References: <google.com> <m3smh7n780.fsfin
    valid.address> <mDVYb.9586$news.pas.earthlink.net> <m3n07fn4d
    address> <uCWYb.9667$news.pas.earthlink.net>
    From: address
    User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2
    Message-ID: <address>
    Lines: 32
    MIME-Version: 1.0
    Content-Type: text/plain; cht=us-ascii
    NNTP-Posting-Host: 24.1.102.108
    X-Complaints-To: net
    X-Trace: attbi_s51 1077201487 24.1.102.108 (Thu, 19 Feb 2004 14:38:07 GMT)
    NNTP-Posting-Date: Thu, 19 Feb 2004 14:38:07 GMT
    Organization: Comcast Online
    Date: Thu, 19 Feb 2004 14:38:07 GMT
    Xref: news.earthlink.net comp.unix.shell:148406
    X-Received-Date: Thu, 19 Feb 2004 06:38:07 PST (newsspool2.news.pas.earthlink.ne
    t)



    Alan Connor <yyy> writes:
     
    > >
    > > You have suspected me of being three or four people that I wasn't.[/ref]
    >
    > That is an out-and-out lie.
    >
    > Post the articles where I falsely accused you of impersonating
    > someone, with the full headers.[/ref]

    Here's one where you claimed "Joe" isn't my real name:

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=IViKb.38676%
    24Pg1.26920%40newsread1.news.pas.earthlink.net&rnu m=1&prev=/groups%3Fas_epq%3Djo
    e%26safe%3Dimages%26ie%3DUTF-8%26oe%3DUTF-8%26as_ugroup%3Dcomp.unix.shell%26as_u
    authors%3Dalan%2520connor%26lr%3D%26num%3D100%26hl %3Den

    Here's one where you claimed I was someone named Zazzybob:

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=iJJKb.23631%24l
    o3.23445%40newsread2.news.pas.earthlink.net

    I recall a couple other such things but I don't have the time or the
    interest to track them down.

    Joe
    --
    "I didn't really say everything I said."
    - Yogi Berra

    ------------------------------------------

    ================================================== =====================

    AC

    Alan Guest

  9. #9

    Default Re: How Do I Determine if a Specific Process Exists?


    Alan Connor <yyy> writes:
     
    > >
    > > It's not only system specific, but practically speaking impossible to
    > > determine if a given pid is associated with the program you think it
    > > is, if pids get recycled.
    > >
    > > If you have control over the two programs, the best way to test for
    > > existence is for the process to create and lock a file with a
    > > predetermined name. The process wanting to test for existence can try
    > > to lock that file. If it can, then the program you're interested in
    > > isn't running. If it can't lock the file, then the program is still
    > > running because it still holds the lock on the file.
    > >
    > > This is a lot more portable than depending on /proc entries.
    > >
    > > Joe
    > > --
    > > "I didn't really say everything I said."
    > > - Yogi Berra[/ref]
    >
    >
    > This person is a serious troll, and you should be VERY careful about
    > taking any technical advice from him.
    >
    > He is also quite young and frequently pretends to technical knowledge
    > he just doesn't have. He's happy to experiment with YOUR box.[/ref]

    If you see any flaws in the advice above, I'd be curious to know
    what they are. It seems like very practical advice to me.

    A variation is to have the process create a /var/run/foo.pid file
    containing its pid, so that you can halt or hup it by doing, for
    example, kill -HUP /var/run/whatever.pid.

    -SEan
    Sean Guest

  10. #10

    Default Re: How Do I Determine if a Specific Process Exists?

    org (John Michael Davison) writes:
     
     



    if (kill(pid, 0) == -1 && errno == ESRCH)
    /* Process doe snot exist */
    else
    /* process exists */

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    Casper Guest

  11. #11

    Default Re: How Do I Determine if a Specific Process Exists?

    in comp.unix.questions i read: [/ref]
     [/ref][/ref]
     
    >
    >If you see any flaws in the advice above, I'd be curious to know
    >what they are. It seems like very practical advice to me.[/ref]

    the advice is sound, disregard alan's response, you'll note it contains no
    technical basis.
     

    this suffers from stale files, e.g., should the application crash
    unexpectedly, which puts you back to not knowing if a particular
    pid is the application you expect.

    --
    a signature
    those Guest

  12. #12

    Default Re: How Do I Determine if a Specific Process Exists?


    those who know me have no need of my name <net> writes:
     [/ref]
    > [/ref]

    > >
    > >If you see any flaws in the advice above, I'd be curious to know
    > >what they are. It seems like very practical advice to me.[/ref]
    >
    > the advice is sound, disregard alan's response, you'll note it contains no
    > technical basis.

    >
    > this suffers from stale files, e.g., should the application crash
    > unexpectedly, which puts you back to not knowing if a particular
    > pid is the application you expect.[/ref]

    Yes, which is why i found joe's advice useful -
    if you are able to lock the file, then file is stale.
    Is there a way to test the lock state from the shell?
    E.g.

    #! /bin/sh
    #
    pidf=/var/run/myserver.pid

    if ( lockf? $pidf )
    then kill -HUP `cat $pidf`
    unlockf $pidf
    else echo Server is down
    fi

    -SEan



    Sean Guest

  13. #13

    Default Re: How Do I Determine if a Specific Process Exists?

    Sean Burke <org> writes:
     
     
    > >
    > > this suffers from stale files, e.g., should the application crash
    > > unexpectedly, which puts you back to not knowing if a particular
    > > pid is the application you expect.[/ref]
    >
    > Yes, which is why i found joe's advice useful -
    > if you are able to lock the file, then file is stale.
    > Is there a way to test the lock state from the shell?[/ref]

    You could write a small C (or perl/whatever) program that tests the
    lock. I don't know of a shell builtin that would do this but would
    like to find out if there is one.

    Note that the suggestion I gave isn't foolproof either. If someone
    unlinks the file before the test is done you could get a false
    reading. Also, lock files on NFS partitions can also lead to
    problems.

    Joe
    --
    "I didn't really say everything I said."
    - Yogi Berra
    joe@invalid.address Guest

Similar Threads

  1. PerformanceCounter for a specific process
    By Avi in forum ASP.NET Web Services
    Replies: 2
    Last Post: February 2nd, 08:15 AM
  2. Monitoring specific process cpu usage
    By Serge in forum Windows Server
    Replies: 6
    Last Post: September 3rd, 02:00 AM
  3. Determine if sub exists in module
    By Perl.Org in forum PERL Beginners
    Replies: 5
    Last Post: July 8th, 03:10 PM
  4. Best way to determine if file exists
    By Jason Wolfe in forum Macromedia Director Basics
    Replies: 1
    Last Post: February 16th, 03:04 PM
  5. how to check if a process still exists in a C app
    By Ian Collins in forum UNIX Programming
    Replies: 3
    Last Post: July 16th, 01:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139