linux C编程 获取 CPU温度 使用率,内存使用率 开机时间信息

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/sysinfo.h>

//#include <thread>

typedef  struct CPU_PACKED
{
    char name[20];
    unsigned int user;
    unsigned int nice;
    unsigned int system;
    unsigned int idle;
}CPU_OCCUPY;
//-----------------------mem--------------------
typedef  struct  MEM_PACKED
{
    char name[20];
    unsigned long total;
    char name2[20];
}MEM_OCCUPY;

typedef struct MEM_PACK
{
    double total;
    double used_rate;
}MEM_PACK;
//------------------------------sysinfo-------------
/**
struct sysinfo{
    long uptime;

    unsigned long totalram;

};
*/

int cal_cpuoccupy(CPU_OCCUPY *o, CPU_OCCUPY *n)
{
    unsigned long od, nd;
    unsigned long id, sd;
    int cpu_use = 0;

    od = (unsigned long) (o->user+ o->nice + o->system + o->idle);//第一次(用户+优先级+系统+空闲)的时间
    nd = (unsigned long) (n->user + n->nice + n->system + n->idle); //第二次

    id = (unsigned long) (n->user - o->user);
    sd = (unsigned long) (n->system - o->system);
    if((nd - od) != 0){
        cpu_use = (int)((sd+id)*100)/(nd-od);
    }else {
        cpu_use = 0;
    }
    return cpu_use;
}

void get_cpuoccupy(CPU_OCCUPY *cpust)
{
    FILE *fd;
    int n;
    char buff[256];
    CPU_OCCUPY *cpu_occupy;
    cpu_occupy = cpust;

    fd = fopen("/proc/stat","r");
    fgets(buff,sizeof(buff),fd);
    sscanf(buff,"%s %u %u %u %u",cpu_occupy->name,&cpu_occupy->user,&cpu_occupy->nice, &cpu_occupy->system, &cpu_occupy->idle);
    fclose(fd);
}


MEM_PACK get_memocupy()
{
    FILE *fd;
    int n;
    double mem_total,mem_used_rate;
    char buff[256];
    MEM_OCCUPY *m = (MEM_OCCUPY*)malloc(sizeof(MEM_OCCUPY));
    MEM_PACK p;

    fd = fopen("/proc/meminfo", "r");

    fgets(buff,sizeof (buff), fd);
    sscanf(buff, "%s %lu %s\n", m->name, &m->total, m->name2);
    mem_total = m->total;
    fgets(buff,sizeof (buff),fd);
    sscanf(buff, "%s %lu %s\n", m->name, &m->total,m->name2);
    mem_used_rate = (1 - m->total/mem_total)*100;
    mem_total = mem_total/(1024*1024);
    p.total = mem_total;
    p.used_rate = mem_used_rate;
    fclose(fd);
    free(m);
    return p;
}
int get_cpu_temp()
{
    FILE *fd;
    int temp;
    char buff[256];

    fd = fopen("/sys/class/thermal/thermal_zone0/temp","r");
    fgets(buff,sizeof(buff),fd);
    sscanf(buff, "%d", &temp);

    fclose(fd);

    return temp/1000;
}
int get_last_reboot(char *cmd,char *output, int size)
{
    FILE *fp = NULL;
    fp = popen(cmd,"r");

    if(fp){
        fgets(output,size,fp);
        if(fgets(output,size,fp) != NULL){
            if(output[strlen(output) - 1] == '\n')
                output[strlen(output) - 1] = '\0';
        }
        pclose(fp);
    }
    return 0;
}

int main()
{
    CPU_OCCUPY cpu_stat1;
    CPU_OCCUPY cpu_stat2;
    int cpu;

    int temp = get_cpu_temp();
    printf("cpu temperature is %d\n",temp);

   struct MEM_PACK mem = get_memocupy();
   printf("mem occupy total %f use_rate %f\n",mem.total,mem.used_rate);
    char lastreboot[64];
    while(1){
        get_last_reboot("last reboot",lastreboot, 64);
        printf("%s \n",lastreboot);

        get_cpuoccupy((CPU_OCCUPY*)&cpu_stat1);
        usleep(10000);
        get_cpuoccupy((CPU_OCCUPY*)&cpu_stat2);

        cpu = cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1,(CPU_OCCUPY*)&cpu_stat2);

        printf("cpu usage: %d \n",cpu);

        usleep(1000000);
    }

    return 0;
}
Linux系统中,通过C语言获取CPU的占用率和温度信息通常需要利用特定的库和API,因为这些硬件监控数据通常是内核空间管理的。以下是两个常见的方法: 1. **使用sysfs接口**: - Linux的sysfs目录提供了一个接口,允许用户空间程序访问内核的许多资源,包括CPU状态。你可以使用`<linux/sysfs.h>`头文件中的函数来读取CPU使用百分比,例如`read_file()`和`kobject_readlink()`。 示例代码(仅作参考,实际应用需要处理错误和异常): ```c #include <linux/fs.h> #include <linux/module.h> #include <linux/kobject.h> #include <linux/sysfs.h> int cpu_usage(char *buf, char *page) { struct file *file; long len = 0; char *usage_str; file = filp_open("/proc/stat", O_RDONLY, 0); if (IS_ERR(file)) { return PTR_ERR(file); } usage_str = kmalloc(16, GFP_KERNEL); if (!usage_str) { return -ENOMEM; } len = read_file(file, page); if (len < 0) { kfree(usage_str); return len; } /* 解析 /proc/stat 获取CPU使用率 */ // ... kfree(usage_str); filp_close(file, NULL); return len; } 2. **使用特定工具的API(如lm_sensors)**: 如果你想获取CPU温度,可以考虑使用像`lm-sensors`这样的工具提供的API。首先,你需要安装`libslurm-dev`或类似包来获取相关库。然后,通过`sensors`命令的驱动模块读取温度数据。 示例: ```c #include <stdio.h> #include <stdlib.h> #include "sensors_api.h" // 假设这个头文件包含传感器API void get_cpu_temperature(void) { float temperature; sensors_get_temperature(&temperature); // 根据实际API调用 printf("CPU Temperature: %.2f°C\n", temperature); } ``` 请注意,以上代码片段仅供参考,并不是完整的实用程序,实际使用时需要根据具体的Linux发行版、API文档以及权限限制进行适当的修改和处理。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值