随笔 - 224  文章 - 0  评论 - 10  阅读 - 35万

Linux C++ 获取系统CPU和网络情况

linux下C++获取系统CPU情况和网络使用情况

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAXBUFSIZE 1024
#define WAIT_SECOND 3   //暂停时间,单位为“秒”
typedef  struct occupy
{
    char name[20];
    unsigned int user;
    unsigned int nice;
    unsigned int system;
    unsigned int idle;
} CPU_OCCUPY;


float g_cpu_used;
int cpu_num;                //定义一个全局的int类型cup_num
void cal_occupy(CPU_OCCUPY*, CPU_OCCUPY*);
void get_occupy(CPU_OCCUPY*);
float get_io_occupy();
void get_disk_occupy(char** reused);
void getCurrentDownloadRates(long int* save_rate);

int main() {
    CPU_OCCUPY ocpu, ncpu;
    

    //获取cpu使用率
    get_occupy(&ocpu);
    //网络延迟
    long int start_download_rates;  //保存开始时的流量计数
    long int end_download_rates;    //保存结果时的流量计数
    getCurrentDownloadRates(&start_download_rates);//获取当前流量,并保存在start_download_rates里

    sleep(WAIT_SECOND); //休眠多少秒,这个值根据宏定义中的WAIT_SECOND的值来确定

    get_occupy(&ncpu);
    cal_occupy(&ocpu, &ncpu);
    printf("cpu used:%4.2f \n", g_cpu_used);
    getCurrentDownloadRates(&end_download_rates);//获取当前流量,并保存在end_download_rates里
    printf("download is : %4.2f byte/s \n", ((float)(end_download_rates - start_download_rates)) / WAIT_SECOND);
    return 0;
}
void  cal_occupy(CPU_OCCUPY* o, CPU_OCCUPY* n) {
    double od, nd;
    double id, sd;
    double scale;
    od = (double)(o->user + o->nice + o->system + o->idle);//第一次(用户+优先级+系统+空闲)的时间再赋给od
    nd = (double)(n->user + n->nice + n->system + n->idle);//第二次(用户+优先级+系统+空闲)的时间再赋给od
    scale = 100.0 / (float)(nd - od);       //100除强制转换(nd-od)之差为float类型再赋给scale这个变量
    id = (double)(n->user - o->user);    //用户第一次和第二次的时间之差再赋给id
    sd = (double)(n->system - o->system);//系统第一次和第二次的时间之差再赋给sd
    g_cpu_used = ((sd + id) * 100.0) / (nd - od); //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
}
void  get_occupy(CPU_OCCUPY* o) {
    FILE* fd;
    int n;
    char buff[MAXBUFSIZE];
    fd = fopen("/proc/stat", "r"); //这里只读取stat文件的第一行及cpu总信息,如需获取每核cpu的使用情况,请分析stat文件的接下来几行。
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %u %u %u %u", o->name, &o->user, &o->nice, &o->system, &o->idle);
    fclose(fd);
}

void getCurrentDownloadRates(long int* save_rate)
{
    char intface[] = "em1:";  //这是网络接口名,根据主机配置
    //char intface[] = "wlan0:";
    FILE* net_dev_file;
    char buffer[1024];
    size_t bytes_read;
    char* match;
    if ((net_dev_file = fopen("/proc/net/dev", "r")) == NULL)
    {
        printf("open file /proc/net/dev/ error!\n");
        exit(EXIT_FAILURE);
    }

    int i = 0;
    while (i++ < 20) {
        if (fgets(buffer, sizeof(buffer), net_dev_file) != NULL) {
            if (strstr(buffer, intface) != NULL) {
                //printf("%d   %s\n",i,buffer);
                sscanf(buffer, "%s %ld", buffer, save_rate);
                break;
            }
        }
    }
    if (i == 20) *save_rate = 0.01;
    fclose(net_dev_file); //关闭文件
    return;
}
复制代码

原文代码摘录自https://www.cnblogs.com/ip99/p/12127031.html

posted on   弘道者  阅读(798)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
历史上的今天:
2022-01-05 bash脚本检查系统CPU型号数量
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示