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;
}
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Linux系统中,获取CPU使用率内存使用率可以通过读取/proc/stat和/proc/meminfo文件来实现。下面是Linux平台下获取CPU使用率内存使用率的示例代码: 获取CPU使用率: ```c++ #include <fstream> #include <iostream> #include <sstream> #include <string> #include <unistd.h> using namespace std; double getCpuUsage() { static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle; ifstream fileStat("/proc/stat"); string line; getline(fileStat, line); istringstream ss(line); ss.ignore(5); // skip "cpu" word unsigned long long totalUser, totalUserLow, totalSys, totalIdle, totalIOwait, totalIRQ, totalSoftIRQ; ss >> totalUser >> totalUserLow >> totalSys >> totalIdle >> totalIOwait >> totalIRQ >> totalSoftIRQ; if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow || totalSys < lastTotalSys || totalIdle < lastTotalIdle) { // Overflow detection. Just skip this value. return -1.0; } unsigned long long total = totalUser - lastTotalUser + totalUserLow - lastTotalUserLow + totalSys - lastTotalSys; double cpuUsage = total; total += totalIdle - lastTotalIdle; cpuUsage /= total; lastTotalUser = totalUser; lastTotalUserLow = totalUserLow; lastTotalSys = totalSys; lastTotalIdle = totalIdle; return cpuUsage * 100.0; } int main() { while (true) { double cpuUsage = getCpuUsage(); cout << "CPU usage: " << cpuUsage << "%" << endl; usleep(1000000); } return 0; } ``` 获取内存使用率: ```c++ #include <fstream> #include <iostream> #include <sstream> #include <string> #include <unistd.h> using namespace std; double getMemoryUsage() { ifstream fileMem("/proc/meminfo"); string line; getline(fileMem, line); istringstream ss(line); ss.ignore(256, ' '); unsigned long long totalMem; ss >> totalMem; totalMem *= 1024; // convert to bytes getline(fileMem, line); ss.str(line); ss.clear(); ss.ignore(256, ' '); unsigned long long freeMem; ss >> freeMem; freeMem *= 1024; double memoryUsage = 100.0 * (totalMem - freeMem) / totalMem; return memoryUsage; } int main() { while (true) { double memoryUsage = getMemoryUsage(); cout << "Memory usage: " << memoryUsage << "%" << endl; usleep(1000000); } return 0; } ``` 以上示例代码只是提供了一个简单的思路,实际应用中还需要对获取到的数据进行处理和显示。另外,Linux系统提供了更为底层的API函数,如getrusage()和sysinfo()等,也可以用于获取系统资源使用情况。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值