Advertisement
francogp

forktest

May 13th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  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. heavytest()
  12. {
  13.   int i, j;
  14.   for(i = 1; i < 1000000000; i++){
  15.     j++;
  16.   }
  17. }
  18.  
  19. void
  20. forktest(void)
  21. {
  22.   int n, pid;
  23.  
  24.   printf(1, "fork test\n");
  25.  
  26.   for(n=0; n<N; n++){
  27.     pid = fork();
  28.     if(pid < 0)
  29.       break;
  30.     if(pid == 0)
  31.       exit();
  32.     // For priority testing
  33.     if(n == 10){
  34.       printf(1, "\n* Heavy test on pid %d\n", pid);
  35.       set_priority(2);
  36.       procstat();
  37.       heavytest();
  38.     }
  39.     if(n == 40){
  40.       printf(1, "\n* Heavy test on pid %d\n", pid);
  41.       set_priority(0);
  42.       procstat();
  43.       heavytest();
  44.     }
  45.   }
  46.  
  47.   if(n == N){
  48.     printf(1, "fork claimed to work %d times!\n", N);
  49.     exit();
  50.   }
  51.  
  52.   for(; n > 0; n--){
  53.     if(wait() < 0){
  54.       printf(1, "wait stopped early\n");
  55.       exit();
  56.     }
  57.   }
  58.  
  59.   if(wait() != -1){
  60.     printf(1, "wait got too many\n");
  61.     exit();
  62.   }
  63.  
  64.   printf(1, "fork test OK\n");
  65. }
  66.  
  67. int
  68. main(void)
  69. {
  70.   forktest();
  71.   exit();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement