You are not logged in.

#1 2008-12-04 02:52:52

Honken
Member
Registered: 2007-03-02
Posts: 79

Reading /proc/stat gives bogus data?

Hi everyone, I just started to write a program to check CPU usage via /proc/stat but it seems to return completely bogus data. Here's my (crap) code:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    unsigned long user, nice, system, idle;
    ifstream procstat ("/proc/stat");
    if (!procstat.is_open()){
        return 1;
    }
    for(int i(0); i!=2; ++i)
        procstat >> user >> nice >> system >> idle;
    procstat.close();
    cout << user << "\n" <<  nice << "\n" << system << "\n" << idle << "\n";
    return 0;
}

It returns some values, but they're COMPLETELY different from when I do cat /proc/stat. Any ideas of why?

Thanks!

Edit: Doh, stupid error (as I thought, that loop didn't work), but does anyone know of a clean way of removing the first column in /proc/stat (which happen to be 'cpu')?

Edit2: Fixed, ugh I'm slow today heh.

Last edited by Honken (2008-12-04 04:50:15)

Offline

Board footer

Powered by FluxBB