Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,585 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,974 people online right now. Registration is fast and FREE... Join Now!




C++ in Linux

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

C++ in Linux, Program to display time information

chaoticabyss99
30 Sep, 2008 - 11:41 PM
Post #1

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
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?

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ In Linux
30 Sep, 2008 - 11:50 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(chaoticabyss99 @ 1 Oct, 2008 - 03:41 AM) *

Not quite sure why I am getting this error.


Because you can't declare functions inside of main.

CODE

int main()  // <--------- Start of int main
{

cout << "\nHere is the beginTotaltime and beginIdletime:\n" << endl;
system("cat /proc/uptime");

void work()  // <--------- Declare function work.... needs be be before or after int main
{


Move the work function to before or after int main.
User is online!Profile CardPM
+Quote Post

chaoticabyss99
RE: C++ In Linux
1 Oct, 2008 - 12:17 AM
Post #3

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
QUOTE(no2pencil @ 1 Oct, 2008 - 12:50 AM) *

QUOTE(chaoticabyss99 @ 1 Oct, 2008 - 03:41 AM) *

Not quite sure why I am getting this error.


Because you can't declare functions inside of main.

CODE

int main()  // <--------- Start of int main
{

cout << "\nHere is the beginTotaltime and beginIdletime:\n" << endl;
system("cat /proc/uptime");

void work()  // <--------- Declare function work.... needs be be before or after int main
{


Move the work function to before or after int main.



OK, I put it before the int main(). Now I'm getting the error: linuxtime.cpp: In function 'int main()':
linuxtime.cpp:29: error: 'cout' was not declared in this scope

How is it not declared when I used: using namespace std; ??
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ In Linux
1 Oct, 2008 - 12:23 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You'll have to post your code, because it compiles & runs for me with the alteration that I suggested :

cpp

#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
using namespace std;

void work(void);

int main(void) {

cout << "\nHere is the beginTotaltime and beginIdletime:\n" << endl;
system("cat /proc/uptime");

cout << "\nHere is the endTotaltime and endIdletime:\n" << endl;
system("cat /proc/uptime");

return 0;
}

void work(void) {
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
}
}


Gives the following for output

QUOTE

code/c >$./linux_cpp

Here is the beginTotaltime and beginIdletime:

544681.73 536894.79

Here is the endTotaltime and endIdletime:

544681.81 536894.82

User is online!Profile CardPM
+Quote Post

chaoticabyss99
RE: C++ In Linux
1 Oct, 2008 - 12:39 AM
Post #5

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
QUOTE(no2pencil @ 1 Oct, 2008 - 01:23 AM) *

You'll have to post your code, because it compiles & runs for me with the alteration that I suggested :

cpp

#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
using namespace std;

void work(void);

int main(void) {

cout << "\nHere is the beginTotaltime and beginIdletime:\n" << endl;
system("cat /proc/uptime");

cout << "\nHere is the endTotaltime and endIdletime:\n" << endl;
system("cat /proc/uptime");

return 0;
}

void work(void) {
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
}
}


Gives the following for output

QUOTE

code/c >$./linux_cpp

Here is the beginTotaltime and beginIdletime:

544681.73 536894.79

Here is the endTotaltime and endIdletime:

544681.81 536894.82



Thanks No2!! You've been very helpful. Now I just have to figure out the last part of the problem. Time for bed now though.

User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: C++ In Linux
3 Oct, 2008 - 12:58 PM
Post #6

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
I have tried adding in the last piece of the program where it totals and shows the percentage the CPU was busy. Here is the code
CODE
#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
using namespace std;

double beginTotalTime;
double beginIdleTime;
double endTotalTime;
double endIdleTime;
double programTotalTime;
double programIdleTime;
double programWorkTime;
double percentage;

void work(void);

int main(void)
{
double endIdleTime;

cout << "\nHere is the beginTotaltime and beginIdletime:\n" << endl;
system("cat /proc/uptime");

cout << "\nHere is the endTotaltime and endIdletime:\n" << endl;
system("cat /proc/uptime");

programTotalTime = endTotalTime - beginIdleTime;
programIdleTime = endIdleTime - beginIdleTime;
programWorkTime = programTotalTime - programIdleTime;
percentage = (programWorkTime/programTotalTime) * 100;

cout << "The program's Total Time is:     " << programTotalTime << endl;
cout << "The program's Idle Time is:      " << programIdleTime << endl;
cout << "The program's Work Time is:      " << programWorkTime << endl;
cout << "The percentage the CPU was busy: " << percentage << "%" << endl;

return 0;

}

void work(void)
{
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
}
}


Why is it that my beginTotalTime and endTotalTime are only 1 second apart? I suspected that the Work function would make this difference greater than 1 second.

Here is my output
CODE
~$ ./linuxtime

Here is the beginTotaltime and beginIdletime:

12204838.72 12196587.35

Here is the endTotaltime and endIdletime:

12204838.73 12196587.35
The program's Total Time is:     0
The program's Idle Time is:      4.86043e-270
The program's Work Time is:      -4.86043e-270
The percentage the CPU was busy: -inf%


Why is the programIdleTime and programWorkTime in funky numbers?? And what's with the '-inf%'?? I expected an actual number there and not '-inf'.

Any suggestions would be awesome!!

This post has been edited by chaoticabyss99: 3 Oct, 2008 - 01:00 PM
User is offlineProfile CardPM
+Quote Post

GWatt
RE: C++ In Linux
3 Oct, 2008 - 01:22 PM
Post #7

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,179



Thanked: 18 times
Dream Kudos: 450
My Contributions
I would guess that you're getting funny output because you never assign a value to your variables. You just use them.
User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: C++ In Linux
3 Oct, 2008 - 01:27 PM
Post #8

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
QUOTE(GWatt @ 3 Oct, 2008 - 02:22 PM) *

I would guess that you're getting funny output because you never assign a value to your variables. You just use them.


I assigned them 0, and still getting the same funny output. crazy.gif
User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: C++ In Linux
3 Oct, 2008 - 01:41 PM
Post #9

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
Also, the Work function isn't right somehow. I must have it in the wrong place or something, I don't know. Because it is not showing up in my output and I think this is probably why there's only a 1 second difference in my beginTotalTime and endTotalTime.
User is offlineProfile CardPM
+Quote Post

GWatt
RE: C++ In Linux
3 Oct, 2008 - 03:06 PM
Post #10

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,179



Thanked: 18 times
Dream Kudos: 450
My Contributions
You have to call the work() function somewhere in there.
Also, you should set the value of your variables based on output from the time() function. Include time.h for it to work.

ALso, in your work() function, you don't need that inner for loop. At least on my machine, it adds nothing to the wait time, even when I increased the condition fromj < 40000 to j < 80000

However, when I added file io to take place every single iteration, then it significantly slowed the cycle down.
User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: C++ In Linux
5 Oct, 2008 - 07:49 AM
Post #11

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
Thanks GWatt! I have finally got the work() function to show up and the cycle time has been slowed down. However, I'm still getting funny output. Here's what the output looks like:
CODE
The program's Total Time is:     -4.85776e-270
The program's Idle Time is:      -0.878033
The program's Work Time is:      0.878033
The percentage the CPU was busy: -1.80748e+271%


Any idea on how to get this output to look normal?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: C++ In Linux
5 Oct, 2008 - 08:09 AM
Post #12

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,179



Thanked: 18 times
Dream Kudos: 450
My Contributions
Can you post your new code? I don't know what you've changed since the last time it was posted.
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/3/08 12:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month