LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 04-07-2007, 12:57 PM   #1
BackwardsDown
Member
 
Registered: Nov 2005
Posts: 66

Rep: Reputation: 15
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)?

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

Rep: Reputation: 30
Hello

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

Bye

Koen Plessers
 
Old 04-07-2007, 03:09 PM   #3
BackwardsDown
Member
 
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, 04:31 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

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

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


Reply


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 07:47 AM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 09: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 05:45 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 06:55 AM
cpu usage Aldebaran Linux - Newbie 3 10-11-2003 12:57 PM


All times are GMT -5. The time now is 10:00 PM.

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