473,473 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get the CPU utilzation in Linux through c code?

3 New Member
i want to get the CPU and Memory utilizatoin in Linux through C.

will be thank ful for any help.
May 17 '07 #1
6 2787
arne
315 Recognized Expert Contributor
i want to get the CPU and Memory utilizatoin in Linux through C.

will be thank ful for any help.
Have a look at /proc/stat and /proc/meminfo. These contain the info you need (and they're used by most monitoring programs).

HTH,
arne
May 17 '07 #2
sumin
3 New Member
Thanx arne ..

But can i get some c function which calculates the like the following which calculates the memory used in solaris.

#include <stdio.h>
#include <unistd.h>

int main()
{

long numPages, pageSize, freePages, totalSize, totalUsed, totalFree;

numPages = sysconf(_SC_PHYS_PAGES)/1024;
pageSize = sysconf(_SC_PAGESIZE)/1024;
freePages = sysconf( _SC_AVPHYS_PAGES)/1024;
totalSize = numPages * pageSize;
totalFree = pageSize * freePages;
totalUsed = totalSize - totalFree;

printf("total memory == %u MB\n",totalSize);
printf("total free memory == %u MB\n",totalFree);
printf("total used memory == %u MB\n",totalUsed);

return(0);

}
May 17 '07 #3
arne
315 Recognized Expert Contributor
Thanx arne ..

But can i get some c function which calculates the like the following which calculates the memory used in solaris.

#include <stdio.h>
#include <unistd.h>

int main()
{

long numPages, pageSize, freePages, totalSize, totalUsed, totalFree;

numPages = sysconf(_SC_PHYS_PAGES)/1024;
pageSize = sysconf(_SC_PAGESIZE)/1024;
freePages = sysconf( _SC_AVPHYS_PAGES)/1024;
totalSize = numPages * pageSize;
totalFree = pageSize * freePages;
totalUsed = totalSize - totalFree;

printf("total memory == %u MB\n",totalSize);
printf("total free memory == %u MB\n",totalFree);
printf("total used memory == %u MB\n",totalUsed);

return(0);

}
Yes, try 'man sysconf'.

I ran your code on my Linux box and the values returned seem reasonable.
May 17 '07 #4
sumin
3 New Member
hi arne..
I searched in man.. but i am not able to find anythig related to CPU utlization
May 18 '07 #5
arne
315 Recognized Expert Contributor
hi arne..
I searched in man.. but i am not able to find anythig related to CPU utlization
No, me neither. Sorry, if i was not clear on that: the options you used I saw in the manpages, too. And the code you posted runs fine. I don't know if there is something similar for CPU utilisation.
May 18 '07 #6
prn
254 Recognized Expert Contributor
I don't recall just what the functions are, but I can give you a hint. Look for the source code for top. I know it's available because I recall needing to modify it when Solaris 2.6 first came out and we only had top code for Solaris 2.5. :)

Seriously, though, I'm sure a more current version is also available. There's going to be a lot more than you need just to get CPU and memory utilization, but it is in there.

Best Regards,
Paul
May 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jhuola Hoptire | last post by:
Just installed J2RE 1.4.2 on a Linux server. I am very knew to the POSIX world. I couldn't dig-up much in the docs or via google about the following: 1 - Is there a standard way to make sure...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
1
by: srihari | last post by:
Hai, I am trying to install IBM DB2 8.1 on Red Hat linux 8.0. My machine is Intel XEON 64bit. The installation went well except for the creation of tools catalog. When I tried to install the tools...
5
by: cranium.2003 | last post by:
hi, Here is my code #include <iostream.h> int main() { cout <<"HI"; return 0; } and using following command to compile a C++ program g++ ex1.cpp -o ex1
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.