Results 1 to 10 of 12
Hy, could you tell me what would be a small solution (coding related) to get the CPU usage? I need it in percentage and free/total ratio
One solution would be ...
- 02-13-2005 #1Just Joined!
- Join Date
- Feb 2005
- Posts
- 9
C program to get CPU usage (in FREE/TOTAL ratio)
Hy, could you tell me what would be a small solution (coding related) to get the CPU usage? I need it in percentage and free/total ratio
One solution would be to filter top but I require a code sample.
Thank you
- 02-13-2005 #2Linux Newbie
- Join Date
- Feb 2005
- Posts
- 129
Try torsmo.
- 02-13-2005 #3Just Joined!
- Join Date
- Feb 2005
- Posts
- 9
thanks
I was looking for a smaller implementation than querying /proc/stat ... still that is a good ideea
- 02-15-2005 #4Just Joined!
- Join Date
- Feb 2005
- Posts
- 1
RE:CPU usage
also you can use glibtop librarys which your wanted things like that
/*
this program is looking for CPU,Memory,Procs also u can look glibtop header there was a lot of usefull function have fun..
systeminfo.c
*/
#include <stdio.h>
#include <glibtop.h>
#include <glibtop/cpu.h>
#include <glibtop/mem.h>
#include <glibtop/proclist.h>
int main(){
glibtop_init();
glibtop_cpu cpu;
glibtop_mem memory;
glibtop_proclist proclist;
glibtop_get_cpu (&cpu);
glibtop_get_mem(&memory);
printf("CPU TYPE INFORMATIONS \n\n"
"Cpu Total : %ld \n"
"Cpu User : %ld \n"
"Cpu Nice : %ld \n"
"Cpu Sys : %ld \n"
"Cpu Idle : %ld \n"
"Cpu Frequences : %ld \n",
(unsigned long)cpu.total,
(unsigned long)cpu.user,
(unsigned long)cpu.nice,
(unsigned long)cpu.sys,
(unsigned long)cpu.idle,
(unsigned long)cpu.frequency);
printf("\nMEMORY USING\n\n"
"Memory Total : %ld MB\n"
"Memory Used : %ld MB\n"
"Memory Free : %ld MB\n"
"Memory Buffered : %ld MB\n"
"Memory Cached : %ld MB\n"
"Memory user : %ld MB\n"
"Memory Locked : %ld MB\n",
(unsigned long)memory.total/(1024*1024),
(unsigned long)memory.used/(1024*1024),
(unsigned long)memory.free/(1024*1024),
(unsigned long)memory.shared/(1024*1024),
(unsigned long)memory.buffer/(1024*1024),
(unsigned long)memory.cached/(1024*1024),
(unsigned long)memory.user/(1024*1024),
(unsigned long)memory.locked/(1024*1024));
int which,arg;
glibtop_get_proclist(&proclist,which,arg);
printf("%ld\n%ld\n%ld\n",
(unsigned long)proclist.number,
(unsigned long)proclist.total,
(unsigned long)proclist.size);
return 0;
}
makefile is
CC=gcc
CFLAGS=-Wall -g
CLIBS=-lgtop-2.0 -lgtop_sysdeps-2.0 -lgtop_common-2.0
cpuinfo:cpu.c
$(CC) $(CFLAGS) systeminfo.c -o systeminfo $(CLIBS)
clean:
rm -f systeminfo
- 03-07-2005 #5Just Joined!
- Join Date
- Mar 2005
- Posts
- 2
Problems under Linux (Fedora Core 3)
when i want compile the libgtop program as mentioned under linux (fedora core 3) i get compiler errors:
i run the following command: gcc -Wall -g -I/usr/include/libgtop-2.0 -I/usr/include/glib-2.0 -o systeminfo systeminfo.c -lgtop-2.0 -lglib -L/usr/lib
also i can't find libgtop-sysdeps-2.0 and libgtop_common-2.0 on my system...
i get tons of errors... anyone got some hints for me?
i can't post the first errors because pipe command "> log.txt" doesn't work on the above command and it is too much for my terminal... anyone knows why?
thx a lot
- 03-07-2005 #6Linux Enthusiast
- Join Date
- Jan 2005
- Posts
- 575
The redirection doesn't work because the error messages go to stderr and not stdin.Try >& instead of >
- 03-07-2005 #7Just Joined!
- Join Date
- Mar 2005
- Posts
- 2
thx for the hint... helped a lot...
now i got:
g++ -I/usr/include/libgtop-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/glib-2.0 -O0 -g3 -Wall -c -fmessage-length=0 -oSystemInfo.o ../SystemInfo.cpp
Finished building: ../SystemInfo.cpp
Building target: SystemInfo
g++ -L/usr/lib -o SystemInfo SystemInfo.o -lgtop-2.0
SystemInfo.o(.text+0x12
: In function `main':
../SystemInfo.cpp:9: undefined reference to `glibtop_init_r(_glibtop**, unsigned long, unsigned int)'
collect2: ld returned 1 exit status
make: *** [SystemInfo] Error 1
so i think i am missing some lib to link... as i mentioned i can't find libgtop-sysdeps-2.0 and libgtop_common-2.0 on my system...
it works if i do not call the glibtop_init() function. what does it do anyway?
the compiler output is:
g++ -I/usr/include/libgtop-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/glib-2.0 -O0 -g3 -Wall -c -fmessage-length=0 -oSystemInfo.o ../SystemInfo.cpp
Finished building: ../SystemInfo.cpp
Building target: SystemInfo
g++ -L/usr/lib -o SystemInfo SystemInfo.o -lgtop-2.0
Finished building: SystemInfo
thx for your help
- 08-14-2006 #8Just Joined!
- Join Date
- Aug 2006
- Posts
- 3
Calculating CPU usage
Hi All,
I've a similar requirement to get cpu usage at any given time. I've few comments and questions related to this.
1) I tried using vmstat command through a C code, but it doesn’t return correct data if not run with a delay.
Here’s the code snippet for that:
FILE * fp;
char buf[1024];
int n = 0;
char * cmd = "vmstat | gawk '/0/ {tot=tot+1; id=id+$15} END {print 100 - id/tot}'";
fp = popen(cmd,"r");
memset(&buf,0,sizeof(buf));
n = fread(buf,sizeof(buf),1,fp);
buf[n] = '\0';
printf("%s\n", buf);
printf("Characters read: %d\n\n", n);
fclose(fp);
Does any one knows how to run a command with delay, for example vmstat -5 from C code?
2) I saw this posting today and I tired to compile “systeminfo.c”, but keep getting errors in glibtop libraries. I tired to download them from http://linux.s390.org/download/rpm2h....5-1.s390.html
3) I used rpm command to install it, but can never find it in /usr/include or /usr/local/include. Is there some other location rpm would be installed in?
4) Lastly the last posting is showing use of gtk++ and systeminfo.cpp? I’m a little confused now, should I use gcc or gtk++ to compile this program.
Any help will be greatly appreciated.
Thanks,
Romy
- 09-04-2006 #9Just Joined!
- Join Date
- Aug 2006
- Posts
- 48
Need link for glibtop
hi i just need the link for glibtop library , actually i tried to search on google and even i have downloaded the rpm but it ask for other dependencies of files, if u have any idea of glibtop library link for fedora 5 then please help me.
Originally Posted by linuxtalks
- 11-24-2006 #10Just Joined!
- Join Date
- Nov 2006
- Posts
- 1
dont know hw to compile
hi i am new to this world of linux ,
i was trying to compile the above code but it was showing undefined reference to "glibtop_global_server".
can any one tell me what is the right way to compile it.
thanks in advance...
Rgd
Dhruva



