[comp.unix.questions] How to tell if a process exists

doug@ridley.coyote.trw.com (Doug Rudoff) (08/03/90)

In a C program, how do you tell if a certain process exists? A
kill(0,pid) works only if you own the process (or you're root). It
seems like such a simple thing to request that it should be part of
the standard C libraries for unix, but I can't seem to find the right
thing to use.

The reason I need this is that I have a user interface that depends on
another prcoess to exist to run properly. The other process writes its
process id to a file and then I want the user interface to check to
see if that process id is still there. Is there a better way to do
this?

I'm running Ultrix 3.1D on a DECstation 3100.
--
=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Doug Rudoff            Mobile Data International   Bothell, WA
(206) 487-5937         rudoff@mdi.com, uunet!mdisea!rudoff
=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

merlyn@iwarp.intel.com (Randal Schwartz) (08/03/90)

In article <26B867F8.38BB@wilbur.coyote.trw.com>, doug@ridley (Doug Rudoff) writes:
| 
| In a C program, how do you tell if a certain process exists? A
| kill(0,pid) works only if you own the process (or you're root). It
| seems like such a simple thing to request that it should be part of
| the standard C libraries for unix, but I can't seem to find the right
| thing to use.

On a normal UNIX (although you mentioned Ultrix later, so all bets may
be off), you will get success or EPERM if the process exists, and
ESRCH if the process does not exist.  With this sort of algorithm, it
doesn't matter who you are.  Just remember to check errno!

Just another UNIX hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

guy@auspex.auspex.com (Guy Harris) (08/05/90)

>In a C program, how do you tell if a certain process exists? A
>kill(0,pid) works only if you own the process (or you're root).

Wrong.  "kill(0, pid)" can be made to work just as well if you don't
have permission to send the signal to the process as it does if you do
have permission to send the signal to the process; you just have to be a
little more careful about checking the value of "errno" if it fails. 
(Remember, "kill(0, pid)" does everything that "kill(<a real signal>,
pid)" does, except that it doesn't actually send the signal.  If the
process doesn't exist, it returns -1 and sets "errno" to ESRCH; if the
process exists, but you don't have permission to send to it, it returns
-1 and sets "errno" to EPERM.)

>The reason I need this is that I have a user interface that depends on
>another prcoess to exist to run properly. The other process writes its
>process id to a file and then I want the user interface to check to
>see if that process id is still there. Is there a better way to do
>this?

There is, however, a reason why I said "just as well" above.

What "kill(0, pid)" does is check whether *a* process with process ID
"pid" exists; there is some chance that *your* process with that process
ID terminated, and the process IDs wrapped around such that some other
unrelated process now has that process ID.  The chance may be small, but
it's not zero. 

In addition, in some versions of UNIX, "kill", when told to send a
signal to a "zombie" process (i.e., one that has exited but not been
reaped by its parent), will fail and set "errno" to ESRCH.  In other
versions, it will succeed, and this may be what POSIX requires; if so,
other systems will do so as well in the future.  Ultrix, at present, is
*probably* one of the systems where it fails, but beware....

darryl@lemuria.MV.COM (Darryl Wagoner) (08/07/90)

In article <26B867F8.38BB@wilbur.coyote.trw.com> rudoff@mdi.com writes:
>
>In a C program, how do you tell if a certain process exists? A
>kill(0,pid) works only if you own the process (or you're root). It
>seems like such a simple thing to request that it should be part of
>the standard C libraries for unix, but I can't seem to find the right
>thing to use.

No, you can use kill(0,pid) even if you don't own the process.  If the
process is running and own by someone else,  then you get a EPERM error,
if the process doesn't exist then you get ESRCH error, and if the
process is killable by you then kill returns a zero.  The error codes
may be different on Ultrix but you get the idea.


-- 
Darryl Wagoner		darryl@lemuria.MV.COM or uunet!virgin!lemuria!darryl
12 Oak Hill Road
Brookline, NH 03033
Office: 603.672.0736   		Home: 603.673.0578

boyd@necisa.ho.necisa.oz (Boyd Roberts) (08/09/90)

In article <3817@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
>>In a C program, how do you tell if a certain process exists? A
>>kill(0,pid) works only if you own the process (or you're root).
>
>Wrong.  "kill(0, pid)" ...

Wrong.  RTFM --  kill(pid, 0)


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

samlb@pioneer.arc.nasa.gov (Sam Bassett RCS) (08/10/90)

In article <1823@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
>Wrong.  RTFM --  kill(pid, 0)

	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
manual sez: " kill [-sig] processid... ", and I've been doing it that way
for a looooooooong time.

	What kind of UNIX _are_ you running??


Sam'l Bassett, Sterling Software @ NASA Ames Research Center, 
Moffett Field CA 94035 Work: (415) 604-4792;  Home: (415) 969-2644
samlb@well.sf.ca.us                     samlb@ames.arc.nasa.gov 
<Disclaimer> := 'Sterling doesn't _have_ opinions -- much less NASA!'

guy@auspex.auspex.com (Guy Harris) (08/11/90)

>	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
>manual sez: " kill [-sig] processid... ", and I've been doing it that way
>for a looooooooong time.

He was referring to the "kill()" library routine, not the "kill"
command....

vlb@magic.apple.com (Vicki Brown) (08/11/90)

In article <7733@amelia.nas.nasa.gov> samlb@pioneer.arc.nasa.gov.UUCP (Sam Bassett RCS) writes:
>In article <1823@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
>>Wrong.  RTFM --  kill(pid, 0)
>
>	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
>manual sez: " kill [-sig] processid... ", and I've been doing it that way
>for a looooooooong time.
>
>	What kind of UNIX _are_ you running??

  --->   C   <---  		( man 2 kill 8^)
   Vicki Brown   A/UX Development Group		Apple Computer, Inc.
   Internet: vlb@apple.com			MS 58A, 10440 Bubb Rd.	
   UUCP: {sun,amdahl,decwrl}!apple!vlb		Cupertino, CA  95014 USA
              Ooit'n Normaal Mens Ontmoet?  En..., Beviel't?
          (Did you ever meet a normal person?  Did you enjoy it?)

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/11/90)

In article <7733@amelia.nas.nasa.gov> samlb@pioneer.arc.nasa.gov.UUCP (Sam Bassett RCS) writes:
-In article <1823@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
->Wrong.  RTFM --  kill(pid, 0)
-	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
-manual sez: " kill [-sig] processid... ", and I've been doing it that way
-for a looooooooong time.

Doing what, incorrectly reading your manual?

terryl@osf.osf.org (Terry Laskodi) (08/11/90)

In article <7733@amelia.nas.nasa.gov> samlb@pioneer.arc.nasa.gov.UUCP (Sam Bassett RCS) writes:
>In article <1823@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
>>Wrong.  RTFM --  kill(pid, 0)
>
>	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
>manual sez: " kill [-sig] processid... ", and I've been doing it that way
>for a looooooooong time.
>
>	What kind of UNIX _are_ you running??


     Well, you're both right. Boyd was describing kill(2); Sam was describing
kill(1).....

samlb@pioneer.arc.nasa.gov (Sam Bassett RCS) (08/11/90)

	Ooops -- yer right -- my mistake.  Sorrreee.....


Sam'l Bassett, Sterling Software @ NASA Ames Research Center, 
Moffett Field CA 94035 Work: (415) 604-4792;  Home: (415) 969-2644
samlb@well.sf.ca.us                     samlb@ames.arc.nasa.gov 
<Disclaimer> := 'Sterling doesn't _have_ opinions -- much less NASA!'

nts0302@dsacg3.dsac.dla.mil (Bob Fisher) (08/13/90)

From article <13534@smoke.BRL.MIL>, by gwyn@smoke.BRL.MIL (Doug Gwyn):
} In article <7733@amelia.nas.nasa.gov> samlb@pioneer.arc.nasa.gov.UUCP (Sam Bassett RCS) writes:
} -In article <1823@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
} ->Wrong.  RTFM --  kill(pid, 0)
} -	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
} -manual sez: " kill [-sig] processid... ", and I've been doing it that way
} -for a looooooooong time.
} 
} Doing what, incorrectly reading your manual?

They're both right.  They're just reading different sections of the manual.
-- 
Bob Fisher
US Defense Logistics Agency Systems Automation Center
DSAC-TSX, Box 1605, Columbus, OH 43216-5002     614-238-9071 (AV 850-9071)
bfisher@dsac.dla.mil		osu-cis!dsacg1!bfisher

jeff@onion.pdx.com (Jeff Beadles) (08/16/90)

samlb@pioneer.arc.nasa.gov.UUCP (Sam Bassett RCS) writes:
>boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
>>Wrong.  RTFM --  kill(pid, 0)

>	Hmmm -- must be running Reverse Polish Unix down under, 'cause my
>manual sez: " kill [-sig] processid... ", and I've been doing it that way
>for a looooooooong time.

>	What kind of UNIX _are_ you running??

Well, most any modern variant.  The kill(pid,0) version is for the 'C'
programming language.  See the kill(2) man page.

kill -SIG pid is a simple C program that's called by the shell.


	-Jeff
-- 
Jeff Beadles    jeff@onion.pdx.com