[comp.unix.wizards] How can a parent find out if one of its children is still alive

DeadHead@cup.portal.com (Bruce M Ong) (06/01/90)

I am sure there is a very easy way to do this, but I just havent
figured this one out:
	
	How can a parent find out if one of its children is still alive
or not without

	1) going through fifos, 
	2) going through messages
	3) going through shared memory
	4) going through sockets
	
	I thought the parent could use the getpgrp() call to find out
the child's group id and in turn find out if the child is still alive
or not. But that didnt seem to work.

	I dont want the parent to wait for the child to die, either...

	Please e-mail, thanks!

	deadhead@cup.portal.com

toma@ozdaltx.UUCP (Tom Armistead) (06/02/90)

In article <30408@cup.portal.com>, DeadHead@cup.portal.com (Bruce M Ong) writes:
> I am sure there is a very easy way to do this, but I just havent
> figured this one out:
> 	
> 	How can a parent find out if one of its children is still alive
> or not without

.....

If you have the process id of the child you can use kill(2) to find out if
it is still there.

i.e.:   if( kill( child_pid, 0 ) == -1 )
            puts( "The child is dead" );

Signal 0 is called the NULL signal and is used to validate a process id, no 
signal is actually sent to the destination process.

Will this do it for you?

Tom
-- 
-------------------------------
{uunet,smu}!sulaco!ozdaltx!toma         (Tom Armistead @ Garland, Texas)
{uunet,smu}!sulaco!ozdaltx!swsrv1!toma 

jrl@cbnews.att.com (john.lupien) (06/06/90)

In article <6334@ozdaltx.UUCP> toma@ozdaltx.UUCP (Tom Armistead) writes:
>In article <30408@cup.portal.com>, DeadHead@cup.portal.com (Bruce M Ong) writes:
>> I am sure there is a very easy way to do this, but I just havent
>> figured this one out:
>> 	How can a parent find out if one of its children is still alive
>> or not without
>If you have the process id of the child you can use kill(2) to find out if
>it is still there.
>i.e.:   if( kill( child_pid, 0 ) == -1 )
>            puts( "The child is dead" );
>Signal 0 is called the NULL signal and is used to validate a process id, no 
>signal is actually sent to the destination process.

This is nice in that, with a small 
change, you can use it to find
out if processes belonging to
other uid's are alive, provided
you know their pid. If kill(child_pid, 0)
returns other than -1, the process exists
and you have permission to kill (007?)
If it returns -1 and an errno of ESRCH,
the pid does not exist, otherwise you
typically get errno of EPERM.


-- 
	-John Lupien
	mvuxr!jrl
	jrl@mvuxr.att.com

matt@group-w.uchicago.edu (Matt Crawford) (06/06/90)

) DeadHead@cup.portal.com (Bruce M Ong) writes:
) > 	How can a parent find out if one of its children is still alive
) > or not without

Tom Armistead, wizard-wannabe, answers:
) i.e.:   if( kill( child_pid, 0 ) == -1 )
)             puts( "The child is dead" );

overlooking the possibility that the child exited many minutes ago and
the pid is now being used by some unrelated process.
________________________________________________________
Matt Crawford	     		matt@oddjob.uchicago.edu

smb@ulysses.att.com (Steven Bellovin) (06/07/90)

In article <1046@gargoyle.uchicago.edu>, matt@group-w.uchicago.edu (Matt Crawford) writes:
} ) DeadHead@cup.portal.com (Bruce M Ong) writes:
} ) > 	How can a parent find out if one of its children is still alive
} ) > or not without
} 
} Tom Armistead, wizard-wannabe, answers:
} ) i.e.:   if( kill( child_pid, 0 ) == -1 )
} )             puts( "The child is dead" );
} 
} overlooking the possibility that the child exited many minutes ago and
} the pid is now being used by some unrelated process.

You can look at errno to determine if the kill() failed because of
a uid mismatch.  That will yield a lot of info if root isn't doing
the checking.

meissner@osf.org (Michael Meissner) (06/07/90)

In article <13071@ulysses.att.com> smb@ulysses.att.com (Steven
Bellovin) writes:

| Path: paperboy!think!snorkelwacker!usc!ucsd!ucbvax!ulysses!ulysses.att.com!smb
| From: smb@ulysses.att.com (Steven Bellovin)
| Newsgroups: comp.unix.wizards
| Date: 6 Jun 90 19:22:28 GMT
| References: <30408@cup.portal.com> <6334@ozdaltx.UUCP> <1046@gargoyle.uchicago.edu>
| Sender: netnews@ulysses.att.com
| Distribution: na
| Lines: 15
| 
| In article <1046@gargoyle.uchicago.edu>, matt@group-w.uchicago.edu (Matt Crawford) writes:
| } ) DeadHead@cup.portal.com (Bruce M Ong) writes:
| } ) > 	How can a parent find out if one of its children is still alive
| } ) > or not without
| } 
| } Tom Armistead, wizard-wannabe, answers:
| } ) i.e.:   if( kill( child_pid, 0 ) == -1 )
| } )             puts( "The child is dead" );
| } 
| } overlooking the possibility that the child exited many minutes ago and
| } the pid is now being used by some unrelated process.
| 
| You can look at errno to determine if the kill() failed because of
| a uid mismatch.  That will yield a lot of info if root isn't doing
| the checking.

Unless of course, the newly created pid is running one of your cron
jobs, a different job created by your job control shell, on a
different window owned by you, or somebody running nethack which is
setuid to your account.  Kill only checks for the effective UID being
the same.

The above methods are ways there can be a process with the same PID as
a dearly departed child process, and have the same EUID as the parent
proces, but not be the process the parent wants to deal with.  I'm
sure I've missed a few ways to create such processes.

--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so

tcl@cs.brown.edu (Tom Lawrence) (06/07/90)

In article <MEISSNER.90Jun6191114@curley.osf.org> meissner@osf.org (Michael Meissner) writes:

>Unless of course, the newly created pid is running one of your cron
>jobs, a different job created by your job control shell, on a
>different window owned by you, or somebody running nethack which is
>setuid to your account.  Kill only checks for the effective UID being
>the same.
>
>The above methods are ways there can be a process with the same PID as
>a dearly departed child process, and have the same EUID as the parent
>proces, but not be the process the parent wants to deal with.  I'm
>sure I've missed a few ways to create such processes.

I'd like to point out that pid numbers aren't recycled for a while. It
may take quite a long time on a machine which is relatively unused.

I think the best way to reliably check the status of a child process
is to catch the SIGCHLD signal, which is delivered when the status of
a child process changes. (this signal is normally ignored). The
handler routine can either explicitely do whatever you want to do when
the child dies, or set some global flag which the main thread will
later check.

* Tom Lawrence			A is for apathy				*
* tcl@cs.brown.edu	 	B is for boredom			*
*				C is for... oh the hell with it		*

friedl@mtndew.UUCP (Stephen J. Friedl) (06/07/90)

In article <42180@brunix.UUCP>, tcl@cs.brown.edu (Tom Lawrence) writes:
> I'd like to point out that pid numbers aren't recycled for a while. It
> may take quite a long time on a machine which is relatively unused.

Be careful with this one -- the 3B15 running Sys V Rel 3 recycles PIDs
very quickly (certainly not in a circular manner) and it can surprise
you -- it surprised me.  Other machines might recycle soon too.

     Steve

-- 
Stephen J. Friedl, KA8CMY / Software Consultant / Tustin, CA / 3B2-kind-of-guy
+1 714 544 6561  / friedl@mtndew.Tustin.CA.US  / {uunet,attmail}!mtndew!friedl

"I will defend to your death my right to my opinion" - me