首页 > 系统相关 >Linux C++ 获取系统CPU和网络情况

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

时间:2023-01-05 17:11:13浏览次数:50  
标签:int C++ cpu occupy rates Linux download 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

标签:int,C++,cpu,occupy,rates,Linux,download,CPU
From: https://www.cnblogs.com/yuandaozhe/p/17028164.html

相关文章

  • linux中正则表达式 {n} 表示匹配前面的项n次
     001、{n};  匹配之前的项n次;   [0-9]{3}能够匹配任意的三位数,[0-9]{3}可以扩展为[0-9][0-9][0-9]。 测试:[root@pc1test]#lsa.txt[root@pc1test]#......
  • manjaro/Linux设置开机启动脚本
     /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置 /etc/rc.d/rc.local在这......
  • linux C 简易版iostat
    Linux下用C语言获取IO信息只获取iostat的CPU信息代码如下#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#include<unistd.h>#inc......
  • linux脚本免密的方法/不用输入密码
     第一种方法:使用管道(上一个命令的stdout接到下一个命令的stdin):在脚本首行添加#!/bin/bashechopassword|sudo-Sapt-getupdate第二种方法使用文本块输入重定向......
  • linux 中正则表达式?、+、*、及.*的区别
     001、?表示匹配0次或者1次atcg?t能够匹配atct或atcgt,但是不能匹配atcggt。 测试:[root@pc1test]#lsa.txt[root@pc1test]#cata.txtatct888atcggk......
  • 六款常用的linux C/C++ IDE
     一、AnjutaAnjuta是一个多语言的IDE,它最大的特色是灵活,同时打开多个文件,内嵌代码级的调试器(调用gdb),应用程序向导(Applicationwizards)可以方便的帮助你创建GNOME程序而不......
  • linux 中 [^] 正则表达式,匹配不在中括号内的任意一个字符。
     [^]:匹配不在中括号内的任意一个字符。中括号内可以是一个字符组或字符范围; 1[^01]能够匹配12和13,但是不匹配11和10;A[^0-9]匹配A以及随后除数字外的任意单......
  • Linux 添加nacos守护进程
    1、编写启动服务文件 /lib/systemd/system/nacos.service#vim/lib/systemd/system/nacos.service添加以下内容[Unit]Description=nacosAfter=network.target[Servi......
  • 阿里云k8s前端测试环境cpu和内存过低pod无法启动的问题
    镜像在本地可以正常启动,放到阿里云之后无法启动,只在容器启动的一瞬间状态是oomkilled的然后无限重启容器,oomkilled状态只维持一会姑没有截图 阿里云edas配置使用cpu和内......
  • 基于OpenCV DNN模块给黑白老照片上色(附Python/C++源码)
    导读本文给大家分享一个用OpenCVDNN模块给黑白老照片上色的实例,并给出Python和C++版本源码。 背景介绍    这个项目是基于在加利福尼亚大学,伯克利,RichardZhang,Phil......