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