获取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]='';
    match=strstr(buffer,"cpu MHz");
    if(match == NULL)
        return 0;
    sscanf(match,"cpu MHz :%f",&clock_speed);
#if 0
    printf("buffer:%s, match:%s
",buffer,match);#endif
    return clock_speed;
}
int main()
{
    printf("CPU clock speed:%4.0f MHz
",get_cpu_clock_speed());
    return 0;
}

原文地址:https://www.cnblogs.com/lakeone/p/3703872.html