473,473 Members | 2,243 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 2788
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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: 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
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.