怎么监测服务器的cup使用情况,有着方面的函数调用么?谢谢大家

sun_star221 2004-04-14 08:09:31
怎么监测服务器的cup使用情况?还有内存的使用情况
...全文
58 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanlei1978113 2004-04-14
  • 打赏
  • 举报
回复
struct CPU_USAGE
{
unsigned long cpu_user;
unsigned long cpu_sys;
unsigned long cpu_nice;
unsigned long cpu_idle;
};

int Get_CpuInfor_DynamicData_From_Proc_Stat( struct CPU_USAGE *usage )
{
FILE *fp= NULL;
char tmp[10];
//char buffer[128+1];

fp = fopen(CPU_FILE_PROC_STAT, "r");
if ( fp == NULL )
{
perror("fopen");
return (-1);
};
//printf("%s %d\n", __FILE__, __LINE__);
fscanf( fp, "%s %lu %lu %lu %lu", tmp, &(usage->cpu_user), &(usage->cpu_sys), &(usage->cpu_nice), &(usage->cpu_idle) );
//printf("%s %d\n", __FILE__, __LINE__);
fclose( fp );
//printf("%s %d\n", __FILE__, __LINE__);

return ( 1 );

}

double Get_Cpu_Use_Rate( const struct CPU_USAGE *cur, const struct CPU_USAGE *old )
{
double user, sys, nice, idle, total;
double use_rate;

user = (double)(cur->cpu_user - old->cpu_user);
sys = (double)(cur->cpu_sys - old->cpu_sys);
nice = (double)(cur->cpu_nice - old->cpu_nice);
idle = (double)(cur->cpu_idle - old->cpu_idle);

total = user + sys + nice + idle;
use_rate = ( 1 - idle / total ) * 100;

return use_rate;
}

这是我自己写的,你试着用用,欢迎多提宝贵意见。
anonimousboy 2004-04-14
  • 打赏
  • 举报
回复
cat /proc/cpuinfo
cat /proc/meminfo

可以看到cpu和内存的现在的状况

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧