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

  1. /* getload(what)
  2.  * int what;
  3.  *
  4.  * Returns the 1, 5, or 15 minute load average.
  5.  * Call with getload(1), or getload(5), or getload(15).
  6.  * Defaults to getload(1);
  7.  * The program "getld" must be setgid to read /dev/kmem and 
  8.  * located where the execl expects it (/usr/local/lib).
  9.  */
  10. #include <signal.h>
  11. #include "loadstuff.h"
  12.  
  13. float getload(whatave)
  14. int whatave;
  15. {
  16.     int loadav;
  17.     float load;
  18.     char *charav = "1 ";
  19.     int status, pid, w;
  20.  
  21.     if(whatave == 15)
  22.     charav = "15";
  23.     if (whatave == 5)
  24.     charav = "5";
  25.  
  26. /* Now run the getld program, using fork/exec. */
  27.     if ((pid = vfork()) == 0) {
  28. /* This Line should be changed if the program "getld" is not
  29.  * going to be actually called "getld" or wont be in /usr/local/lib
  30.  */
  31.         execl("/usr/local/lib/getld","getld",charav,0);
  32.         _exit(254);
  33.     }
  34.     while ((w = wait(&status)) != pid && w != -1)
  35.         ;
  36.     if (w == -1)
  37.         status = -1;
  38.  
  39.     loadav = (status >> 8);    /* Set loadav to system exit */
  40.  
  41.     load = (float)loadav / 10;  /* Get the correct floating point value */
  42.                 /* that corresponds to the integer */
  43.                 /* exit value we received from getld */
  44.  
  45.     if(loadav == 254) load = NOKMEM;
  46.     if(loadav == 255) load = NONAMELST;
  47.     if(loadav == 250) load = OVERFLOW;
  48.  
  49.     return(load);
  50. }
  51.