[comp.unix.questions] Can process do an asynchronous wait?

tms@cup.portal.com (Alexis TMS Tatarsky) (05/31/90)

If a process forks and execs several child processes, is there any
way for the parent to detect when the child processes have exited 
or died without having to wait(2) on them.  I wrote a program where
the parent caught SIGCLD but I don't know which child it is which
died. 

pfalstad@phoenix.Princeton.EDU (Paul John Falstad) (05/31/90)

In article <30343@cup.portal.com> tms@cup.portal.com (Alexis TMS Tatarsky) writes:
>If a process forks and execs several child processes, is there any
>way for the parent to detect when the child processes have exited 
>or died without having to wait(2) on them.  I wrote a program where
>the parent caught SIGCLD but I don't know which child it is which
>died. 

If your system has it, use wait3(2) with the WNOHANG flag.

-- 
Paul Falstad  PLINK:Hypnos GEnie:P.FALSTAD net:pfalstad@phoenix.princeton.edu
Disclaimer: My opinions, which belong to me and which I own, are mine.
-Anne Elk (not AN elk!)  The sun never set on the British empire because
the British empire was in the East and the sun sets in the West.

cpcahil@virtech.uucp (Conor P. Cahill) (05/31/90)

In article <30343@cup.portal.com> tms@cup.portal.com (Alexis TMS Tatarsky) writes:
>If a process forks and execs several child processes, is there any
>way for the parent to detect when the child processes have exited 
>or died without having to wait(2) on them.  I wrote a program where
>the parent caught SIGCLD but I don't know which child it is which
>died. 

The signal handler for the SIGCLD should do a wait() which will provide
the process id and exit status of the child. The fact that you got 
a SIGCLD tells you that the wait will not block, so you don't have
to worry about getting stuck in there.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

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

In article <30343@cup.portal.com> tms@cup.portal.com (Alexis TMS
Tatarsky) writes:

| If a process forks and execs several child processes, is there any
| way for the parent to detect when the child processes have exited 
| or died without having to wait(2) on them.  I wrote a program where
| the parent caught SIGCLD but I don't know which child it is which
| died. 

If you are using select for other things, another approach than
setting up a signal handler or waitpid with WNOHANG might be to open a
pipe in the parent when doing a fork on each child, and pass the write
end to the child (closing the read end in the child), and keeping the
read end open in the parent (closing the write end in the parent).
When the read file descriptor is available for I/O (assuming the child
does write to the pipe or close the pipe file descriptor), it means
that there are no more writers on the pipe, and that the child died.

Of course the above assume that you have BSD system calls.  On System
V, you can either set up a signal handler (note System V, the
semantics of the child death signal are different from BSD), or do the
pipe trick and turn on async mode on the pipe (I don't remember the
ioctl or fcntrl for this in the System V world off hand).

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