Надо вычислить нагрузку в процентах на cpu, юзаю для этого procfs но чет не пашет у меня(постоянно 0 возвращает).
Что не так с формулой?
int cpuUsage() {
int load = 0;
unsigned long total, c, n, s, i = 0;
unsigned long c1, n1, s1, i1 = 0;
FILE *f = fopen("/proc/stat", "r");
if (fscanf(f, "cpu %lu %lu %lu %lu", &c, &n, &s, &i) < 4) {
fclose(f);
return -1;
}
fclose(f);
usleep(300);
f = fopen("/proc/stat", "r");
if (fscanf(f, "cpu %lu %lu %lu %lu", &c1, &n1, &s1, &i1) < 4) {
fclose(f);
return -1;
}
fclose(f);
c = (c + c1)/2;
n = (n + n1)/2;
s = (s + s1)/2;
i = (i + i1)/2;
total = c + n + s + i;
load = 100 * ((c + n + s)/total);
return load;
}