root/forktest.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. printf
  2. forktest
  3. main

   1 // Test that fork fails gracefully.
   2 // Tiny executable so that the limit can be filling the proc table.
   3 
   4 #include "types.h"
   5 #include "stat.h"
   6 #include "user.h"
   7 
   8 #define N  1000
   9 
  10 void
  11 printf(int fd, const char *s, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
  12 {
  13   write(fd, s, strlen(s));
  14 }
  15 
  16 void
  17 forktest(void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  18 {
  19   int n, pid;
  20 
  21   printf(1, "fork test\n");
  22 
  23   for(n=0; n<N; n++){
  24     pid = fork();
  25     if(pid < 0)
  26       break;
  27     if(pid == 0)
  28       exit();
  29   }
  30 
  31   if(n == N){
  32     printf(1, "fork claimed to work N times!\n", N);
  33     exit();
  34   }
  35 
  36   for(; n > 0; n--){
  37     if(wait() < 0){
  38       printf(1, "wait stopped early\n");
  39       exit();
  40     }
  41   }
  42 
  43   if(wait() != -1){
  44     printf(1, "wait got too many\n");
  45     exit();
  46   }
  47 
  48   printf(1, "fork test OK\n");
  49 }
  50 
  51 int
  52 main(void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54   forktest();
  55   exit();
  56 }

/* [previous][next][first][last][top][bottom][index][help]  */