LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cpu usage (https://www.linuxquestions.org/questions/programming-9/cpu-usage-544061/)

BackwardsDown 04-07-2007 11:57 AM

cpu usage
 
Hi,

It has been asked a few times before on this forum, but none of the answers gave me a statisfying solution.

I want to make a function that returns the cpu usage in %. I can use getloadavg() which returns the load averages. But I dont want that because the loadavg can be bigger than 2 or 3 even with a single cpu.

And this:
Quote:

#my_array=($(top -n 1))
#echo ${my_array[27]}
Gives me a different output on each pc.

What is a reliable way to get the cpu usage in linux using C++ (or just the command-line)?

koen plessers 04-07-2007 02:02 PM

Hello

You might use conky http://conky.sourceforge.net/ to compare the values you get.

Bye

Koen Plessers

BackwardsDown 04-07-2007 02:09 PM

Compare to what?

I am only able to get the load average, and when I try to extract the % with top every computer retuns something different (system, user, waiting, etc).

jlliagre 04-07-2007 03:31 PM

Have a look at /proc/stat to get CPU usage (user, nice, system and idle), loadavg is a different metric.

makyo 04-07-2007 05:51 PM

Hi.
Quote:

P Percentage of the CPU that this job got. This is just
user + system times divided by the total running time. It
also prints a percentage sign.
-- excerpt from man time
cheers, makyo


All times are GMT -5. The time now is 12:02 AM.