home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / programm / 5847 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.0 KB  |  58 lines

  1. Newsgroups: comp.unix.programmer
  2. 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
  3. From: gaf@alpha.aosg.gsf.dec.com (Gerald Feldman)
  4. Subject: Re: Which Child?
  5. Message-ID: <1993Jan6.213104.13272@aosg.gsf.dec.com>
  6. Keywords: child fork
  7. Sender: usenet@aosg.gsf.dec.com (USENET News System)
  8. Nntp-Posting-Host: alpha.aosg.gsf.dec.com
  9. Reply-To: gaf@aosg.gsf.dec.com
  10. Organization: Digital Equipment Corporation
  11. References:  <C0Fyxx.ABn@cerc.wvu.wvnet.edu>
  12. Date: Wed, 6 Jan 1993 21:31:04 GMT
  13. Lines: 43
  14.  
  15. In article <C0Fyxx.ABn@cerc.wvu.wvnet.edu>, abmg@cerc.wvu.wvnet.edu (Aliasghar Babadi) writes:
  16. |> Hi,
  17. |>     I have a program which forks several child processes. If one
  18. |> child is terminated. Is there a way to findout which child has been  
  19. |> terminated? Thanks.
  20.  
  21. -- 
  22. The fork(2) system call returns the process-id of the child it forked.
  23. The wait(2) system call returns the process id of the child that
  24. terminated. There is a familty of wait calls that you can use depending
  25. upon your flavor of Unix. You can use a signal handler if the parent
  26. process needs to do other than wait. In the parent, you can keep a table
  27. of children you forked.
  28. A very simplified model(note that I do no error checking):
  29.  
  30. pid_t ptable[NUMBER_OF_PIDS]; /* This table could be in shared memory 
  31.                 * if multiple processes can perform the
  32.                 * fork()
  33.                 */
  34. int    numchild;
  35. :
  36. :
  37. /* fork a child */
  38. pid = fork();
  39. if (pid) { /* I am parent */
  40.     ptable[numchild++] = pid; /* Add pid to the table */
  41.     :
  42.     :
  43.     : 
  44.     pid = wait(0); /* wait for a child to terminate */
  45.     :
  46. } else { /* in child */
  47.     :
  48. }
  49.  
  50. +-------------------------------------------------------------+
  51. Jerry Feldman           AOSG
  52. Mailstop: GSF1-1/K13       DTN:264-5863
  53. Digital Equipment Corp.    EXT:(603)884-5863
  54. 5 Wentworth Drive          ip(DEC): gaf@aosg.gsf.dec.com
  55. Hudson, NH 03051           enet: alpha::gaf
  56.                Internet(HOME):gaf@palantir.newton.ma.us
  57. +-------------------------------------------------------------+
  58.