Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-07-2007, 11:57 AM   #1
Registered: Nov 2005
Posts: 66

Rep: Reputation: 15
cpu usage


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:
#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)?

Last edited by BackwardsDown; 04-07-2007 at 12:01 PM.
Old 04-07-2007, 02:02 PM   #2
koen plessers
Registered: Aug 2003
Location: Belgium
Distribution: Mint
Posts: 191

Rep: Reputation: 30

You might use conky to compare the values you get.


Koen Plessers
Old 04-07-2007, 02:09 PM   #3
Registered: Nov 2005
Posts: 66

Original Poster
Rep: Reputation: 15
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).
Old 04-07-2007, 03:31 PM   #4
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,703

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
Have a look at /proc/stat to get CPU usage (user, nice, system and idle), loadavg is a different metric.
Old 04-07-2007, 05:51 PM   #5
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU usage meniscus Linux - Newbie 2 12-01-2006 06:47 AM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 05:55 AM
cpu usage Aldebaran Linux - Newbie 3 10-11-2003 11:57 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration