LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   System uptime (https://www.linuxquestions.org/questions/programming-9/system-uptime-773837/)

sfskseg32 12-06-2009 03:18 PM

System uptime
 
Hello,

I have a hw assignment to obtain the system totaltime and idletime, perform a calculation, obtain totaltime and uptime again, and calculate the percentage of time that the cpu was busy during that time.

I know how to obtain the uptime and it is a file of the totaltime and idletime, but I don't know how to store those seperately. So far my prog looks like this:
Code:

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

void work()
{
double y;
double x=12340;
double z=98760;
for(long j=0;j<100000;j++)
{
for(long j=0;j<100000;j++)
{
y=x*z;
}
}
}

int main()
{
cout << "\nTotaltime and Idletime:\n" << endl;
system("cat /proc/uptime");
work();
cout << "\nTotaltime and Idletime:\n" << endl;
system("cat /proc/uptime");
return 0;
//HERE IS WHERE I WILL PERFORM CALCS
}

I need to store the 4 times and then perform the calculations. How do I store the times?

thanks
j

paulsm4 12-06-2009 05:22 PM

Your real question is "how to read /proc/uptime".

One possible answer: substitute "popen()" for "system (cat SOMEFILE)". Then you can "fread()" or "fgets()" from /proc/uptime.

An even better possibility: just "fopen()" /proc/update. Try it!

IMHO ..PSM


All times are GMT -5. The time now is 09:49 PM.