a)把第一次的所有cpu使用情况求和,得到s1;
b)把第二次的所有cpu使用情况求和,得到s2;
c)s2 - s1得到这个时间间隔内的所有时间片,即totalCpuTime = j2 - j1 ;
3、计算空闲时间idle
idle对应第四列的数据,用第二次的idle - 第一次的idle即可
idle=第二次的idle - 第一次的idle
4、计算cpu使用率
pcpu =100* (total-idle)/total
5、同理可以用同样的方法求出其他进程和线程所占cpu资源
源码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define __DEBUG__ 1
#define CK_TIME 1
int main(int argc ,char *argv[])
{
FILE *fp;
char buf[128];
char cpu[5];
long int user,nice,sys,idle,iowait,irq,softirq;
long int all1,all2,idle1,idle2;
float usage;
while(1)
{
fp = fopen("/proc/stat","r");
if(fp == NULL)
{
perror("fopen:");
exit (0);
}
fgets(buf,sizeof(buf),fp);
#if __DEBUG__
printf("buf=%s",buf);
#endif
sscanf(buf,"%s%d%d%d%d%d%d%d",cpu,&user,&nice,&sys,&idle,&iowait,&irq,&softirq);
/*
#if __DEBUG__
printf("%s,%d,%d,%d,%d,%d,%d,%d\n",cpu,user,nice,sys,idle,iowait,irq,softirq);
#endif
*/
all1 = user+nice+sys+idle+iowait+irq+softirq;
idle1 = idle;
rewind(fp);
/*第二次取数据*/
sleep(CK_TIME);
memset(buf,0,sizeof(buf));
cpu[0] = '\0';
user=nice=sys=idle=iowait=irq=softirq=0;
fgets(buf,sizeof(buf),fp);
#if __DEBUG__
printf("buf=%s",buf);
#endif
sscanf(buf,"%s%d%d%d%d%d%d%d",cpu,&user,&nice,&sys,&idle,&iowait,&irq,&softirq);
/*
#if __DEBUG__
printf("%s,%d,%d,%d,%d,%d,%d,%d\n",cpu,user,nice,sys,idle,iowait,irq,softirq);
#endif
*/
all2 = user+nice+sys+idle+iowait+irq+softirq;
idle2 = idle;
usage = (float)(all2-all1-(idle2-idle1)) / (all2-all1)*100 ;
printf("all=%d\n",all2-all1);
printf("ilde=%d\n",all2-all1-(idle2-idle1));
printf("cpu use = %.2f\%\n",usage);
printf("=======================\n");
fclose(fp);
}
return 1;
}