获取cpu一些信息的例子

1619阅读 1评论2008-06-26 LinuxSmartphone
分类:LINUX

#include
#include
#include

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);
  }
}
上一篇:gpephone之dbus分析(2)
下一篇:gpephone之dbus分析(3)之gpe-applaucher(一 )xml文件

文章评论