home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / libcs / dfork.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  2.6 KB  |  83 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. #include <c.h>
  29. /*
  30.  * This routine does a double fork so that the (grand)child won't
  31.  * hang on exit because the (grand)parent never wait(2)ed for it.
  32.  * Note: grandchild will be inherited by init (proc 1) on exit.
  33.  **********************************************************************
  34.  * HISTORY
  35.  * $Log:    dfork.c,v $
  36.  * Revision 1.2  90/12/11  17:51:32  mja
  37.  *     Add copyright/disclaimer for distribution.
  38.  * 
  39.  * 30-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  40.  *    Adapted for 4.2 BSD UNIX.  Inserted READ and WRITE macros here
  41.  *    rather than using <modes.h>, which is obsolete.
  42.  *
  43.  * 13-Mar-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
  44.  *    Made code more careful about closing pipes in cases of system
  45.  *    call failures.
  46.  *
  47.  */
  48.  
  49. #define READ 0
  50. #define WRITE 1
  51.  
  52. dfork() {
  53.     int pid;
  54.     int fildes[2];
  55.  
  56.     if (pipe(fildes) == CERROR)
  57.     return(CERROR);
  58.     if ((pid = fork()) == CERROR) {
  59.     close(fildes[READ]);
  60.     close(fildes[WRITE]);
  61.     return(CERROR);
  62.     }
  63.     if (pid) {
  64.     close(fildes[WRITE]);
  65.     while (wait(0) != pid);
  66.     if (read(fildes[READ], &pid, sizeof(int)) != sizeof(int))
  67.         pid = CERROR;
  68.     close(fildes[READ]);
  69.     return(pid);
  70.     } else {
  71.     close(fildes[READ]);
  72.     if ((pid = fork()) == CERROR)
  73.         exit(CERROR);
  74.     if (pid) {
  75.         write(fildes[WRITE], &pid, sizeof(int));
  76.         close(fildes[WRITE]);
  77.         exit(0);
  78.     }
  79.     close(fildes[WRITE]);
  80.     return(0);
  81.     }
  82. }
  83.