Here is the scope of the program:
Time information.
In this section you will print out how long the system has been up and how busy it has been. Once you have some baseline numbers printed, you will run a short program that places a load on the system. You will then take a second set of numbers and calculate the load that your program placed on the system.
Duration of uptime # get these information from /proc/uptime
Duration of idletime
Calculating load average.
Write a function that does some work (to put some load on the system) and make a note of the uptime and idletime before and after the call of the function. The following can be used as sample code for the work function. This program simple runs a math calculation a large number of times, just trying to keep the CPU busy. You can include it as a function in your overall program. Note that because you are using a math function (pow) you will need to explicitly include the math library when you compile your program, i.e., “gcc –o test test.c –lm”. (the –lm option for program compiling in C or C++.)
Here is the code I have so far:
CODE
#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
using namespace std;
int main()
{
cout << "\nHere is the beginTotaltime and beginIdletime:\n" << endl;
system("cat /proc/uptime");
void work()
{
double y;
double x = 3.0;
double e = 2.0;
int i,j;
for(i=0; i<5; i++)
{
for(j=0; j<400000; j++)
{
y=pow(x,e);
}
printf("Loop %d of work cycle\n", i);
//pause for one second between loops so that the work cycle takes a
//little time.
sleep(1); //in C or C++ you will need to include the unistd.h library
//for this function
}
}
cout << "\nHere is the endTotaltime and endIdletime:\n" << endl;
system("cat /proc/uptime");
return 0;
}
Here is the skeleton of the project:
(1) read file “/proc/uptime” to obtain beginTotaltime and beginIdletime
(2) call work( ) to put some work into the system
(7) read file “/proc/uptime” to obtain endTotaltime and endIdletime
(8) Calculate the percentage of the time that CPU was busy during this program:
programTotalTime = endTotalTime - beginTotalTime;
programIdleTime = endIdleTime - beginIdleTime;
programWorkTime = programTotalTime - programIdleTime;
percentage = (programWorkTime / programTotalTime)* 100;
I have incorporated (1), (2), and (7), but not yet (8). I am getting an error message that states: error: a function-definition is not allowed here before '{' token.
Not quite sure why I am getting this error. Tried doing a google search, but came up with nothing. Anyone have any ideas why this is?