root/user/testbin/forktest/forktest.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dofork
  2. check
  3. dowait
  4. test
  5. main

   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * forktest - test fork().
  32  *
  33  * This should work correctly when fork is implemented.
  34  *
  35  * It should also continue to work after subsequent assignments, most
  36  * notably after implementing the virtual memory system.
  37  */
  38 
  39 #include <unistd.h>
  40 #include <string.h>
  41 #include <stdlib.h>
  42 #include <stdio.h>
  43 #include <err.h>
  44 
  45 /*
  46  * This is used by all processes, to try to help make sure all
  47  * processes have a distinct address space.
  48  */
  49 static volatile int mypid;
  50 
  51 /*
  52  * Helper function for fork that prints a warning on error.
  53  */
  54 static
  55 int
  56 dofork(void)
  57 {
  58         int pid;
  59         pid = fork();
  60         if (pid < 0) {
  61                 warn("fork");
  62         }
  63         return pid;
  64 }
  65 
  66 /*
  67  * Check to make sure each process has its own address space. Write
  68  * the pid into the data segment and read it back repeatedly, making
  69  * sure it's correct every time.
  70  */
  71 static
  72 void
  73 check(void)
  74 {
  75         int i;
  76 
  77         mypid = getpid();
  78         
  79         /* Make sure each fork has its own address space. */
  80         for (i=0; i<800; i++) {
  81                 volatile int seenpid;
  82                 seenpid = mypid;
  83                 if (seenpid != getpid()) {
  84                         errx(1, "pid mismatch (%d, should be %d) "
  85                              "- your vm is broken!", 
  86                              seenpid, getpid());
  87                 }
  88         }
  89 }
  90 
  91 /*
  92  * Wait for a child process.
  93  *
  94  * This assumes dowait is called the same number of times as dofork
  95  * and passed its results in reverse order. Any forks that fail send
  96  * us -1 and are ignored. The first 0 we see indicates the fork that
  97  * generated the current process; that means it's time to exit. Only
  98  * the parent of all the processes returns from the chain of dowaits.
  99  */
 100 static
 101 void
 102 dowait(int nowait, int pid)
 103 {
 104         int x;
 105 
 106         if (pid<0) {
 107                 /* fork in question failed; just return */
 108                 return;
 109         }
 110         if (pid==0) {
 111                 /* in the fork in question we were the child; exit */
 112                 exit(0);
 113         }
 114 
 115         if (!nowait) {
 116                 if (waitpid(pid, &x, 0)<0) {
 117                         warn("waitpid");
 118                 }
 119                 else if (WIFSIGNALED(x)) {
 120                         warnx("pid %d: signal %d", pid, WTERMSIG(x));
 121                 }
 122                 else if (WEXITSTATUS(x) != 0) {
 123                         warnx("pid %d: exit %d", pid, WEXITSTATUS(x));
 124                 }
 125         }
 126 }
 127 
 128 /*
 129  * Actually run the test.
 130  */
 131 static
 132 void
 133 test(int nowait)
 134 {
 135         int pid0, pid1, pid2, pid3;
 136 
 137         /*
 138          * Caution: This generates processes geometrically.
 139          *
 140          * It is unrolled to encourage gcc to registerize the pids,
 141          * to prevent wait/exit problems if fork corrupts memory.
 142          */
 143 
 144         pid0 = dofork();
 145         putchar('0');
 146         check();
 147         pid1 = dofork();
 148         putchar('1');
 149         check();
 150         pid2 = dofork();
 151         putchar('2');
 152         check();
 153         pid3 = dofork();
 154         putchar('3');
 155         check();
 156 
 157         /*
 158          * These must be called in reverse order to avoid waiting
 159          * improperly.
 160          */
 161         dowait(nowait, pid3);
 162         dowait(nowait, pid2);
 163         dowait(nowait, pid1);
 164         dowait(nowait, pid0);
 165 
 166         putchar('\n');
 167 }
 168 
 169 int
 170 main(int argc, char *argv[])
 171 {
 172         int nowait=0;
 173 
 174         if (argc==2 && !strcmp(argv[1], "-w")) {
 175                 nowait=1;
 176         }
 177         else if (argc!=1 && argc!=0) {
 178                 warnx("usage: forktest [-w]");
 179                 return 1;
 180         }
 181         warnx("Starting.");
 182 
 183         test(nowait);
 184 
 185         warnx("Complete.");
 186         return 0;
 187 }

/* [<][>][^][v][top][bottom][index][help] */