获取CPU频率

#include <stdio.h>
#include <string.h>
float get_cpu_clock_speed()
{
    FILE *fp;
    char buffer[1024];
    size_t bytes_read;
    char *match;
    float clock_speed;
    fp=fopen("/proc/cpuinfo","r");
    bytes_read=fread(buffer,1,sizeof(buffer),fp);
    if(bytes_read==0 || bytes_read==sizeof(buffer))
        return 0;
    buffer[bytes_read]='\0';
    match=strstr(buffer,"cpu MHz");
    if(match == NULL)
        return 0;
    sscanf(match,"cpu MHz :%f",&clock_speed);
#if 0
    printf("buffer:%s, match:%s\n",buffer,match);#endif
    return clock_speed;
}
int main()
{
    printf("CPU clock speed:%4.0f MHz\n",get_cpu_clock_speed());
    return 0;
}

posted on 2014-05-02 11:58  lakeone  阅读(982)  评论(0编辑  收藏  举报

导航