home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!haven.umd.edu!decuac!pa.dec.com!e2big.mko.dec.com!peavax.mlo.dec.com!nntpd.lkg.dec.com!news!alpha.aosg.gsf.dec.com!gaf
- From: gaf@alpha.aosg.gsf.dec.com (Gerald Feldman)
- Subject: Re: Which Child?
- Message-ID: <1993Jan6.213104.13272@aosg.gsf.dec.com>
- Keywords: child fork
- Sender: usenet@aosg.gsf.dec.com (USENET News System)
- Nntp-Posting-Host: alpha.aosg.gsf.dec.com
- Reply-To: gaf@aosg.gsf.dec.com
- Organization: Digital Equipment Corporation
- References: <C0Fyxx.ABn@cerc.wvu.wvnet.edu>
- Date: Wed, 6 Jan 1993 21:31:04 GMT
- Lines: 43
-
- In article <C0Fyxx.ABn@cerc.wvu.wvnet.edu>, abmg@cerc.wvu.wvnet.edu (Aliasghar Babadi) writes:
- |> Hi,
- |> I have a program which forks several child processes. If one
- |> child is terminated. Is there a way to findout which child has been
- |> terminated? Thanks.
-
- --
- The fork(2) system call returns the process-id of the child it forked.
- The wait(2) system call returns the process id of the child that
- terminated. There is a familty of wait calls that you can use depending
- upon your flavor of Unix. You can use a signal handler if the parent
- process needs to do other than wait. In the parent, you can keep a table
- of children you forked.
- A very simplified model(note that I do no error checking):
-
- pid_t ptable[NUMBER_OF_PIDS]; /* This table could be in shared memory
- * if multiple processes can perform the
- * fork()
- */
- int numchild;
- :
- :
- /* fork a child */
- pid = fork();
- if (pid) { /* I am parent */
- ptable[numchild++] = pid; /* Add pid to the table */
- :
- :
- :
- pid = wait(0); /* wait for a child to terminate */
- :
- } else { /* in child */
- :
- }
-
- +-------------------------------------------------------------+
- Jerry Feldman AOSG
- Mailstop: GSF1-1/K13 DTN:264-5863
- Digital Equipment Corp. EXT:(603)884-5863
- 5 Wentworth Drive ip(DEC): gaf@aosg.gsf.dec.com
- Hudson, NH 03051 enet: alpha::gaf
- Internet(HOME):gaf@palantir.newton.ma.us
- +-------------------------------------------------------------+
-