How Do I Determine if a Specific Process Exists?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > How Do I Determine if a Specific Process Exists?




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How Do I Determine if a Specific Process Exists?  
John Michael Davison


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 09:34 AM

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.





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 09:34 AM

jdavison@speakeasy.org (John Michael Davison) writes:

> What's the most straightforward way to implement a function
>
>     bool process_exists(::pid_t pid);
>
> in C/C++?
>

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 parse
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 !!!





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 09:34 AM

Dragan Cvetkovic <me@privacy.net> writes:

> jdavison@speakeasy.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 parse
> its output. Not every UNIX has /proc FS.
>

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 !!!





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 09:34 AM

Dragan Cvetkovic wrote:
> Dragan Cvetkovic <me@privacy.net> writes:
>
> 

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

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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 09:34 AM



Dragan Cvetkovic wrote:

> Dragan Cvetkovic <me@privacy.net> writes:
>=20
>=20 
s 
se 
>=20
>=20
> Or send signal 0 using kill(2) function. But you might need certain
> privilegies to send signals to unrelated processes.

retval =3D 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 =3D=3D ESRCH means that the process does not exist.
errno =3D=3D 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.

>=20
> Bye, Dragan
>=20
>=20

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






[ Post a follow-up to this message ]



    Re: How Do I Determine if a Specific Process Exists?  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 10:34 AM

jdavison@speakeasy.org (John Michael Davison) writes:

> What's the most straightforward way to implement a function
>
>     bool process_exists(::pid_t pid);
>
> in C/C++?

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





[ Post a follow-up to this message ]



    Re: How Do I Determine if a Specific Process Exists?  
those who know me have no need of my name


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 10:34 AM

in comp.unix.questions i read:
>Dragan Cvetkovic <me@privacy.net> writes: 
 
 

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

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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 02:34 PM

On Thu, 19 Feb 2004 18:12:16 GMT, joe@invalid.address <joe@invalid.address> wrote:[color
=blue]
>
>
> jdavison@speakeasy.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[/color]


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 zzzzzz@xxx.yyy Wed Feb 18 21:32:27 2004
Path: newsspool2.news.pas.earthlink.net!stamper.news.pas.earthlink.net!newsr
ead1
.news.pas.earthlink.net.POSTED!65807237!not-for-mail
Newsgroups: comp.unix.shell
From: Alan Connor <zzzzzz@xxx.yyy>
Subject: Re: script quesation
References: <f116ef63.0402181741.12240cd6@posting.google.com> <m3smh7n780.fs
f@in
valid.address> <mDVYb.9586$tL3.2596@newsread1.news.pas.earthlink.net> <m3n07
fn4d
e.fsf@invalid.address>
Reply-To: xxxx@yyy.zzz
User-Agent: slrn/0.9.7.3 (Linux)
Lines: 77
Message-ID: <uCWYb.9667$tL3.1005@newsread1.news.pas.earthlink.net>
Date: Thu, 19 Feb 2004 04:05:46 GMT
NNTP-Posting-Host: 63.187.193.83
X-Complaints-To: abuse@earthlink.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.earthlin
k.ne
t)



On Thu, 19 Feb 2004 03:18:53 GMT, joe@invalid.address <joe@invalid.address> 
wrot
e:
>
>
> Alan Connor <zzzzzz@xxx.yyy> writes:
> 
>
> You have suspected me of being three or four people that I wasn't.

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.net!attbi_s03.POSTED!not-fo
r-ma
il
Newsgroups: comp.unix.shell
Subject: Re: script quesation
References: <f116ef63.0402181741.12240cd6@posting.google.com> <m3smh7n780.fs
f@in
valid.address> <mDVYb.9586$tL3.2596@newsread1.news.pas.earthlink.net>
From: joe@invalid.address
Message-ID: <m3n07fn4de.fsf@invalid.address>
Lines: 14
User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 24.1.102.108
X-Complaints-To: abuse@comcast.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.earthlin
k.ne
t)



Alan Connor <zzzzzz@xxx.yyy> writes:

> I suspect this is the troll that has been plaguing the comp
> hierarchy for the last week or so.

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: <f116ef63.0402181741.12240cd6@posting.google.com> <m3smh7n780.fs
f@in
valid.address> <mDVYb.9586$tL3.2596@newsread1.news.pas.earthlink.net> <m3n07
fn4d
e.fsf@invalid.address> <uCWYb.9667$tL3.1005@newsread1.news.pas.earthlink.net
>
From: joe@invalid.address
User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2
Message-ID: <m3fzd7m8xd.fsf@invalid.address>
Lines: 32
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 24.1.102.108
X-Complaints-To: abuse@comcast.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.earthlin
k.ne
t)



Alan Connor <zzzzzz@xxx.yyy> writes:

> On Thu, 19 Feb 2004 03:18:53 GMT, joe@invalid.address
> <joe@invalid.address> wrote: 
>
> That is an out-and-out lie.
>
> Post the articles where I falsely accused you of impersonating
> someone, with the full headers.

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

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

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

[url]http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=iJJKb.23631%24l[/
url]
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






[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 06:34 PM


Alan Connor <zzzzzz@xxx.yyy> writes:

> On Thu, 19 Feb 2004 18:12:16 GMT, joe@invalid.address <joe@invalid.address
> wrote: 
>
>
> 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.

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





[ Post a follow-up to this message ]



    Re: How Do I Determine if a Specific Process Exists?  
Casper H.S. Dik


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-19-04 07:34 PM

jdavison@speakeasy.org (John Michael Davison) writes:

>What's the most straightforward way to implement a function

>    bool process_exists(::pid_t pid);



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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:15 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register