home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / byteunix.lzh / byte.1 / limit.c < prev    next >
C/C++ Source or Header  |  1990-05-11  |  3KB  |  165 lines

  1. /*******************************************************************************
  2.  *  The BYTE UNIX Benchmarks - Release 2
  3.  *          Module: limit.c   SID: 2.4 4/17/90 16:45:34
  4.  *          
  5.  *******************************************************************************
  6.  * Bug reports, patches, comments, suggestions should be sent to:
  7.  *
  8.  *    Ben Smith or Rick Grehan at BYTE Magazine
  9.  *    bensmith@bixpb.UUCP    rick_g@bixpb.UUCP
  10.  *
  11.  *******************************************************************************
  12.  *  Modification Log:
  13.  *  $Header: limit.c,v 3.4 87/06/22 14:25:11 kjmcdonell Beta $
  14.  *
  15.  ******************************************************************************/
  16. char SCCSid[] = "@(#) @(#)limit.c:2.4 -- 4/17/90 16:45:34";
  17. /*
  18.  *  Force a UNIX system to the per process and per user limits
  19.  *
  20.  */
  21.  
  22. #define    CLICK    1024
  23. #define    MAXCHN    100
  24.  
  25. #include <signal.h>
  26. #include <setjmp.h>
  27.  
  28. int    parent;        /* parent's pid */
  29. int    child;        /* child's pid */
  30. int    pid[MAXCHN];
  31. int    ncall;
  32. int    level;
  33. jmp_buf    env;
  34.  
  35. main(argc, argv)
  36. int    argc;
  37. char    *argv[];
  38. {
  39.     char    *top;
  40.     int    pad;
  41.     int    end;
  42.     int    i;
  43.     int    status;
  44.     float    f;
  45.     int    flag();
  46.     int    wakeup();
  47.     long    last;
  48.  
  49.     /* open files (file descriptors) */
  50.     for (i = 3; open(".", 0) > 0; i++) ;
  51.     printf("Maximum open files per process: %d\n", i);
  52.     while (--i > 2)
  53.         close(i);
  54.  
  55.     /* process address space */
  56.     top = (char *)sbrk(0);
  57. #if debug
  58.     printf("inital top of program: 0x%x\n", top);
  59. #endif
  60.     pad = (((int)top+CLICK-1)/CLICK)*CLICK - (int)top;
  61.     sbrk(pad);
  62.     for (i = 0; (char *)sbrk(CLICK) != (char *)-1; i++) ;
  63. #if debug
  64.     printf("final top of program: 0x%x\n", sbrk(0));
  65. #endif
  66.     brk(top);
  67. #if debug
  68.     printf("top of program restored to: 0x%x\n", sbrk(0));
  69. #endif
  70.     end = (((int)top+pad)/CLICK) + i;
  71.     f = ((float)end * CLICK) / 1024;
  72.     printf("Process address space limit: ");
  73.     if (f < 1024)
  74.         printf("%.2f Kbytes\n", f);
  75.     else {
  76.         f /= 1024;
  77.         printf("%.2f Mbytes\n", f);
  78.     }
  79.  
  80.     /* process creations */
  81.     printf("Maximum number of child processes:");
  82.     i = 0;
  83.     while (1) {
  84. #if debug
  85.         printf("about to fork\n");
  86. #endif
  87.         if ((pid[i] = fork()) == -1) {
  88. #if debug
  89.             perror("fork failed");
  90. #endif
  91.             break;
  92.         } else if (pid[i] != 0) {
  93. #if debug
  94.             printf("child %d: pid=%d\n", i+1, pid[i]);
  95. #endif
  96.             i++;
  97.             if (i >= MAXCHN) {
  98.                 printf(" more than");
  99.                 break;
  100.             }
  101.         } else {
  102. #if debug
  103.             printf("child %d pausing\n", getpid());
  104. #endif
  105.             pause();
  106. #if debug
  107.             printf("child %d exiting\n", getpid());
  108. #endif
  109.             exit(1);
  110.         }
  111.     }
  112.     printf(" %d\n", i);
  113.     while (--i >= 0) {
  114.         kill(pid[i], SIGKILL);
  115.         wait(0);
  116.     }
  117.  
  118.     ncall = level = 0;
  119.     parent = getpid();
  120.     signal(SIGTERM, flag);
  121.     if ((child = fork()) == 0) {
  122.         signal(SIGALRM, wakeup);
  123.         recurse();
  124.         exit(4);
  125.     }
  126.     while ((i = wait(&status)) == -1) {
  127.     }
  128.     printf("Estimated maximum stack size: %d Kbytes\n", level);
  129.     exit(0);
  130. }
  131.  
  132. recurse()
  133. {
  134.     int    temp[1024 / sizeof(int)];
  135. #if debug
  136.     printf("recursion @ level %d\n", ncall);
  137. #endif
  138.     temp[1024 / sizeof(int) - 1] = 1;
  139.     ncall++;
  140.     kill(parent, SIGTERM);
  141.     while (ncall > level) {
  142.         alarm(2);
  143.         pause();
  144.     }
  145.     if (ncall < 8000)
  146.         /* less than 8M bytes of temp storage! */
  147.         recurse();
  148.     else
  149.         /* give up! */
  150.         exit(0);
  151. }
  152.  
  153. flag()
  154. {
  155.     signal(SIGTERM, flag);
  156.     level++;
  157.     if (child != 0)
  158.         kill(child, SIGTERM);
  159. }
  160.  
  161. wakeup()
  162. {
  163.     signal(SIGALRM, wakeup);
  164. }
  165.