linux c实现获取系统温度,cpu主频,运行时间,内存使用情况

代码如下:

/*
 * @Description: 
 * @Version: 
 * @Autor: lishi
 * @Date: 2023-02-17 09:09:27
 * @LastEditors: lishi
 * @LastEditTime: 2023-02-17 10:18:45
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/sysinfo.h>

static int getSysIntValue(char *key)
{
    FILE *fp = NULL;
    int value = 0;
    if(key == NULL)return 0;
    
    fp = fopen(key, "r");
    if (fp == NULL) {
        printf("Error: could not open %s file\n",key);
        return 1;
    }

    fscanf(fp, "%d", &value);
    fclose(fp);fp = NULL;
    return value;
}

static int getSysStringValue(char *key,char *value,int bufLen)
{
    FILE *fp = NULL;
    int i = 0,j=0;
    char *tmp = NULL;
    if(key == NULL || value == NULL || bufLen <= 0)return 0;
    tmp = (char *)malloc(bufLen);
    if(tmp == NULL)return 0;
    memset(value,0,bufLen);
    fp = fopen(key, "r");
    if (fp == NULL) {
	    free(tmp);
	    tmp = NULL;
        printf("Error: could not open %s file\n",key);
        return 1;
    }
    fgets(tmp, bufLen, fp);
    for(i = 0;i < strlen(tmp);i++){
        if(tmp[i] != '\n' && tmp[i] != '\r'){
            value[j++] = tmp[i];
        }
    }
    fclose(fp);fp=NULL;
    free(tmp);tmp = NULL;
    return 0;
}

int main() 
{
    struct sysinfo info;
    int i = 0;
    long run_secs_total = 0;
    long run_hours = 0,run_mins = 0,run_secs = 0;
    long mem_total = 0,mem_free = 0,mem_used = 0;
    double mem_total2 = 0,mem_free2 = 0,mem_used2 = 0;
    if (sysinfo(&info) != 0) {
        printf("Error: could not get system information\n");
        return 1;
    }
    run_secs_total = info.uptime;
    run_hours = run_secs_total / 3600;
    run_mins = (run_secs_total%3600) / 60;
    run_secs = run_secs_total % 60;
    printf("%-20s: %02ld:%02ld:%02ld \n\n","System run time", run_hours,run_mins,run_secs);

    mem_total = info.totalram>>20;
    mem_free = info.freeram>>20;
    mem_used = (info.totalram - info.freeram)>>20;
    mem_total2 = mem_total/1024.0;
    mem_free2 = mem_free/1024.0;
    mem_used2 = mem_used/1024.0;
    printf("%-20s: %.2f GB\n", "Total RAM",mem_total2);
    printf("%-20s: %.2f GB\n", "Used RAM",mem_used2);
    printf("%-20s: %.2f GB\n", "Free RAM",mem_free2);
    printf("%-20s: %ld%%\n\n","Memory usage", ((info.totalram - info.freeram) * 100) / info.totalram);
    
    printf("%-20s: %d%%\n\n", "GPU utilization",getSysIntValue("/sys/devices/platform/fb000000.gpu/utilisation"));
    for(i=0;i<7;i++){
        char key[128] = {0};
        char value[256] = {0};
        int temp = 0;
        double temp2 = 0;
        sprintf(key,"/sys/class/thermal/thermal_zone%d/type",i);
        getSysStringValue(key,value,sizeof(value));
        memset(key,0,sizeof(key));
        sprintf(key,"/sys/class/thermal/thermal_zone%d/temp",i);
        temp = getSysIntValue(key);
        temp2 = temp/1000.0;
        printf("%-20s: %.3f°\n",value,temp2);
    }
    printf("\n");
    for(i=0;i<8;i++){
        char key[128] = {0};
        char value[256] = {0};
        long temp = 0;
        double temp2 = 0;
        sprintf(key,"/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq",i);
        getSysStringValue(key,value,sizeof(value));
        temp = strtol(value,NULL,10);
        temp2 = temp/1000000.0;
        memset(key,0,sizeof(key));
        sprintf(key,"cpu%d_cur_freq",i);
        printf("%-20s: %.3f GHZ\n",key,temp2);
    }
    return 0;
}

运行截图如下:
运行截图

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值