All pastes #358312 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-02-16 05:12 UTC
rendered paste body
static int calc_cpu_total()
{
        int total, t;
        int rc;
        int ps;
        char line[BUFFER_LEN];
        int cpu, nice, system, idle;

        ps = open("/proc/stat", O_RDONLY);
        rc = read(ps, line, sizeof(line));
        close(ps);
        if (rc < 0)
                return 0;
        sscanf(line, "%*s %d %d %d %d", &cpu, &nice, &system, &idle);
        total = cpu + nice + system + idle;

        t = total - previous_total;
        previous_total = total;

        if (t < 0)
                t = 0;

        return t;
}