热门关键字:  ubuntu  分区  函数  linux系统进程  Fedora

当前位置 :| 主页>Linux教程>编程开发>C++>

获取cpu一些信息的例子

来源: 作者: 时间:2008-06-27 Tag: 点击:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* cpu(void)
{
unsigned int total;

float user;
float nice;
float system;
float idle;

char cpu[21];
char text[201];

FILE *fp;

fp = fopen("/proc/stat", "r");
while (fgets(text, 200, fp))
{
if (strstr(text, "cpu"))
{
sscanf(text, "%s %f %f %f %f", cpu, &user, &nice, &system, &idle);
}
}
fclose(fp);

total = (user + nice + system + idle);
user = (user / total) * 100;
nice = (nice / total) * 100;
system = (system / total) * 100;
idle = (idle / total) * 100;

snprintf(cpu, 21, "%4.2f %4.2f %3.2f %4.2f", user, nice, system, idle);
printf("cpu detail is:\n%s\n",cpu);
return(cpu);
}
int main(int argc,char **argv)
{
//char cpu_detail[200];
while(1)
{
cpu();
sleep(2);
}
}

上一篇:没有了
下一篇:没有了
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表