系统下的内存和CPU占有率怎么用程序获得?

想做个小程序监视系统的CPU和内存占有率,怎么用程序实现?

1、cpu占有率可以参考ClockId的帮助,里面有下面的例子:
id = ClockId(1, 1);
for( ;; ) {
ClockTime(id, NULL, &start);
sleep(1);
ClockTime(id, NULL, &stop);
printf(“load = %f%%\n”, (1000000000.0 - (stop-start)) / 10000000.0);
}

2、取得空闲内存的大小,只需要查/proc的属性就可以了。下面代码可以参考一下:
int CProcInfo::s_hSysProc=-1;

if (s_hSysProc==-1)
{
s_hSysProc=open("/proc", O_RDONLY);
}
struct stat st;
if (fstat(s_hSysProc, &st) == -1)
printf( “couldn’t get stat info for %s: %s\n”, “/proc”, strerror(errno));
return st.st_size;

3、如果要取得更多的系统信息,比如内存大小,进程信息等,可以参考pdebug及pidin的代码,比较简单的。

多谢!

老大,我想获得内存占用率,怎么获得系统的总内存大小呢?

pidin info

“pidin"的源码要搞透的话,有很多事情可以迎刃而解的。:slight_smile:

我把以前的代码公布了,你可以拿来参考一下或直接用:)

http://blog.csdn.net/Delores/archive/2008/10/23/3127978.aspx

好的,多谢!