博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

LinuxSmartphone

好记性不如烂笔头,进步在于积累!
   LinuxSmartphone.cublog.cn
关于作者  


我的分类  




获取cpu一些信息的例子
#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);
  }
}

 发表于: 2008-06-26,修改于: 2008-06-26 10:55 已浏览100次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.1205

京ICP证041476号