计算linux和openwrt的cpu使用率
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
float get_cpu_util(void);
int main()
{
float p = get_cpu_util();
printf("p-->%.1f",p);
return 0;
}
float get_cpu_util(void)
{
FILE *fp;
{
float p = get_cpu_util();
printf("p-->%.1f",p);
return 0;
}
float get_cpu_util(void)
{
FILE *fp;
char buffer[128],name[15];
long user1, nice1, system1, idle1,user2, nice2, system2, idle2,sum1,sum2;
float util;
if( (fp=fopen("/proc/stat","r") )==NULL)
{
printf("Can not open file!");
exit(1);
}
fgets (buffer, sizeof(buffer),fp);
sscanf (buffer, "%s %ld %ld %ld %ld", name, &user1,&nice1,&system1, &idle1);
printf ("%s %ld %ld %ld %ld\n ", name, user1, nice1, system1, idle1);
sum1=user1+ nice1+system1+idle1;
fclose(fp);
long user1, nice1, system1, idle1,user2, nice2, system2, idle2,sum1,sum2;
float util;
if( (fp=fopen("/proc/stat","r") )==NULL)
{
printf("Can not open file!");
exit(1);
}
fgets (buffer, sizeof(buffer),fp);
sscanf (buffer, "%s %ld %ld %ld %ld", name, &user1,&nice1,&system1, &idle1);
printf ("%s %ld %ld %ld %ld\n ", name, user1, nice1, system1, idle1);
sum1=user1+ nice1+system1+idle1;
fclose(fp);
sleep(1);
if( (fp=fopen("/proc/stat","r") )==NULL)
{
printf("Can not open file!");
exit(1);
}
fgets (buffer, sizeof(buffer),fp);
fopen和open区别 sscanf (buffer, "%s %ld %ld %ld %ld", name, &user2,&nice2,&system2, &idle2);
printf ("%s %ld %ld %ld %ld\n ", name, user2, nice2, system2, idle2);
sum2=user2+ nice2+system2+idle2;
util=(float)100.0*(user2-user1+system2-system1)/(sum2-sum1);
printf("Cpu Utilization:%.1f %%\n",util);
fclose(fp);
return util;
}
return util;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论