home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / loadav / getld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  1.5 KB  |  61 lines

  1. /* getld.c
  2.  * This program must be setgid to memory (or whoever is allowed
  3.  * to read /dev/kmem).  It exits with with the load average, or
  4.  * a negative number to indicate an error.
  5.  * It serves primarily to be called by the library routine
  6.  * getload();
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <nlist.h>
  11. #include "loadstuff.h"
  12.  
  13. struct nlist avenrun[] =
  14. {
  15.     {    "_avenrun"    },
  16.     {        0        }
  17. };
  18.  
  19. main(argc,argv)
  20. int argc;
  21. char **argv;
  22. {
  23.  
  24.     register int kmem, whatave, returnave;
  25.     double avg[3];
  26.  
  27.     if((kmem = open("/dev/kmem", 0)) < 0)
  28.     exit(NOKMEM);
  29.  
  30.     nlist(NAMELIST, avenrun);    /* Find where our kernel keeps it */
  31.     if(avenrun[0].n_type == 0)
  32.     exit(NONAMELST);
  33.  
  34.     whatave = atoi(argv[1]);
  35.     --whatave;
  36.     if(whatave == 4) whatave = 1;
  37.     if(whatave == 14) whatave = 2;
  38.     
  39.     if(whatave <0 || whatave > 2)
  40.     whatave = 0;
  41.  
  42.     lseek(kmem, (long) avenrun[0].n_value, 0);
  43.     read(kmem, avg, sizeof (avg));
  44.     
  45.     returnave = avg[whatave] * 100;
  46. /* Now we have the load average to 2 floating point accuracy * 100
  47.  * However, we want to return it with 1 point accuracy that is correctly
  48.  * rounded up or down.  So we divide it by 10 and see what the
  49.  * remainder is and do the rounding (for the integer).
  50.  */
  51.     if(returnave % 10 >= 5)    /* Round up */
  52.     returnave = returnave / 10 + 1;
  53.     else returnave = returnave / 10;
  54.  
  55.     if(returnave >= 250)    /* We cannot return with a number > 255 */
  56.     returnave = 250;    /* and 255 = -1, 254 = -2   */
  57.  
  58.     exit(returnave);        /* Exit with an integer load average */
  59.  
  60. }
  61.