Back to index

courier  0.68.2
confwait.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include      "config.h"
00008 #endif
00009 
00010 #include      <sys/types.h>
00011 
00012 #if    HAVE_SYS_WAIT_H
00013 #include      <sys/wait.h>
00014 #endif
00015 #include      <stdio.h>
00016 #include      <signal.h>
00017 
00018 #if    HAVE_UNISTD_H
00019 #include      <unistd.h>
00020 #endif
00021 #include      <string.h>
00022 #include      <time.h>
00023 
00024 #define       INCLUDED_FROM_CONFIGURE
00025 #include      "waitlib.c"
00026 
00027 #define       NUMPROCS      10
00028 
00029 static int numterminated=0;
00030 
00031 
00032 static void cntreaped(pid_t p, int n)
00033 {
00034        if ( ++numterminated == NUMPROCS ) _exit(0);
00035 }
00036 
00037 static RETSIGTYPE childsig(int n)
00038 {
00039        n=n;
00040 
00041        wait_reap(cntreaped, childsig);
00042 
00043 #if    RETSIGTYPE != void
00044        return (0);
00045 #endif
00046 }
00047 
00048 int main()
00049 {
00050 int    pipefd[2];
00051 int    pipefd2[2];
00052 pid_t  p;
00053 int    i;
00054 time_t t;
00055 char   c;
00056 
00057        if (pipe(pipefd) || pipe(pipefd2))
00058        {
00059               perror("pipe");
00060               exit(1);
00061        }
00062 
00063        signal(SIGCHLD, childsig);
00064 
00065        for (i=0; i<NUMPROCS; i++)
00066        {
00067               while ((p=fork()) == -1)
00068               {
00069                      perror("fork");
00070                      sleep(5);
00071               }
00072 
00073               if (p == 0)
00074               {
00075                      close(pipefd[1]);
00076                      close(pipefd2[0]);
00077                      close(pipefd2[1]);
00078                      read(pipefd[0], &c, 1);
00079                      _exit(0);
00080               }
00081        }
00082        close(pipefd2[1]);
00083        close(pipefd[0]);
00084        if (read(pipefd2[0], &c, 1) != 0)
00085        {
00086               perror("read");
00087        }
00088 
00089        wait_block();
00090 
00091        close(pipefd2[0]);
00092        close(pipefd[1]);
00093        sleep(3);
00094        time(&t);
00095        wait_clear(&childsig);
00096 
00097        do
00098        {
00099               sleep(3);
00100        } while ( time(0) < t+3);
00101        if (numterminated == NUMPROCS)
00102               exit(0);
00103        exit(1);
00104        return (0);
00105 }