getloadavg.c 272 B

1234567891011121314
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <sys/sysinfo.h>
  4. int getloadavg(double *a, int n)
  5. {
  6. struct sysinfo si;
  7. if (n <= 0) return n ? -1 : 0;
  8. sysinfo(&si);
  9. if (n > 3) n = 3;
  10. for (int i=0; i<n; i++)
  11. a[i] = 1.0/(1<<SI_LOAD_SHIFT) * si.loads[i];
  12. return n;
  13. }