Back to index

courier  0.68.2
testwait.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2006 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "waitlib.h"
00007 #if    HAVE_UNISTD_H
00008 #include      <unistd.h>
00009 #endif
00010 #include      <stdio.h>
00011 #include      <stdlib.h>
00012 #include      <signal.h>
00013 
00014 /* Stress test waitlib.c */
00015 
00016 #define       NUMCHILDREN   100           /* Start 100 child processes */
00017 #define       INITCHILDREN  10            /* Start with these many child procs */
00018 
00019 static unsigned started, finished;
00020 
00021 static void reap_child(pid_t p, int dummy)
00022 {
00023        ++finished;
00024 }
00025 
00026 static RETSIGTYPE sighandler(int sig)
00027 {
00028        wait_reap(&reap_child, &sighandler);
00029 #if    RETSIGTYPE != void
00030        return (0);
00031 #endif
00032 }
00033 
00034 static pid_t start_child()
00035 {
00036 pid_t  p;
00037 
00038        wait_block();
00039        while ((p=fork()) == (pid_t)-1)
00040        {
00041               perror("fork");
00042               sleep(3);
00043        }
00044        ++started;
00045        if (p == 0)
00046        {
00047               wait_restore();
00048        }
00049        else
00050               wait_clear(&sighandler);
00051        return (p);
00052 }
00053 
00054 extern void foobar();
00055 
00056 int    main()
00057 {
00058 int    pipefd[2];
00059 int    pipefd2[2];
00060 char   c;
00061 
00062        if (pipe(pipefd) || pipe(pipefd2))
00063        {
00064               perror("pipe");
00065               exit(1);
00066        }
00067 
00068        signal(SIGCHLD, sighandler);
00069 
00070        started=finished=0;
00071        while (started < INITCHILDREN)
00072        {
00073               if (start_child() == 0)
00074               {
00075                      close(pipefd2[0]);
00076                      close(pipefd2[1]);
00077                      close(pipefd[1]);
00078                      if (read(pipefd[0], &c, 1) != 1)
00079                             ; /* Shut gcc up */
00080                      close(pipefd[0]);
00081                      _exit(0);
00082               }
00083        }
00084        close(pipefd2[1]);
00085        close(pipefd[0]);
00086        if (read(pipefd2[0], &c, 1) != 1)
00087               ; /* Shut gcc up */
00088        close(pipefd[1]);
00089        close(pipefd2[0]);
00090 
00091        while (started < NUMCHILDREN)
00092               if (start_child() == 0)
00093                      _exit(0);
00094 
00095        alarm(30);
00096        while (finished < started)
00097               foobar();
00098        exit(0);
00099 }