C/C++获取Linux系统CPU和内存及硬盘使用情况

时间:2023-03-30 23:07:56
C/C++获取Linux系统CPU和内存及硬盘使用情况

需求分析:

不使用Top  df  free 等命令,利用C/C++获取Linux系统CPU和内存及硬盘使用情况

实现:

  1. //通过获取/proc/stat (CPU)和/proc/meminfo(内存和硬盘)文件信息
  2. //proc/meminfo文件结构
  3. //MemTotal:       515164 kB
  4. //MemFree:         15024 kB
  5. //Buffers:         13740 kB
  6. //Cached:         248824 kB
  7. //SwapCached:    960 kB
  8. //Active:         309980 kB
  9. //Inactive:       63420 kB
  10. typedef struct MEMPACKED         //定义一个mem occupy的结构体
  11. {
  12. <span style="white-space:pre">    </span>char name1[20];      //定义一个char类型的数组名name有20个元素
  13. <span style="white-space:pre">    </span>unsigned long MemTotal;
  14. <span style="white-space:pre">    </span>char name2[20];
  15. <span style="white-space:pre">    </span>unsigned long MemFree;
  16. <span style="white-space:pre">    </span>char name3[20];
  17. <span style="white-space:pre">    </span>unsigned long Buffers;
  18. <span style="white-space:pre">    </span>char name4[20];
  19. <span style="white-space:pre">    </span>unsigned long Cached;
  20. <span style="white-space:pre">    </span>char name5[20];
  21. <span style="white-space:pre">    </span>unsigned long SwapCached;
  22. }MEM_OCCUPY;
  23. //proc/stat文件结构
  24. //cpu  633666 46912 249878 176813696 782884 2859 19625 0
  25. //cpu0 633666 46912 249878 176813696 782884 2859 19625 0
  26. //intr 5812844
  27. //ctxt 265816063
  28. //btime 1455203832
  29. //processes 596625
  30. //procs_running 1
  31. //procs_blocked 0
  32. typedef struct CPUPACKED         //定义一个cpu occupy的结构体
  33. {
  34. <span style="white-space:pre">    </span>char name[20];      //定义一个char类型的数组名name有20个元素
  35. <span style="white-space:pre">    </span>unsigned int user; //定义一个无符号的int类型的user
  36. <span style="white-space:pre">    </span>unsigned int nice; //定义一个无符号的int类型的nice
  37. <span style="white-space:pre">    </span>unsigned int system;//定义一个无符号的int类型的system
  38. <span style="white-space:pre">    </span>unsigned int idle; //定义一个无符号的int类型的idle
  39. <span style="white-space:pre">    </span>unsigned int lowait;
  40. <span style="white-space:pre">    </span>unsigned int irq;
  41. <span style="white-space:pre">    </span>unsigned int softirq;
  42. }CPU_OCCUPY;
  43. void get_memoccupy(MEM_OCCUPY *mem) //对无类型get函数含有一个形参结构体类弄的指针O
  44. {
  45. <span style="white-space:pre">    </span>FILE *fd;
  46. <span style="white-space:pre">    </span>char buff[256];
  47. <span style="white-space:pre">    </span>MEM_OCCUPY *m;
  48. <span style="white-space:pre">    </span>m = mem;
  49. <span style="white-space:pre">    </span>fd = fopen("/proc/meminfo", "r");
  50. <span style="white-space:pre">    </span>//MemTotal: 515164 kB
  51. <span style="white-space:pre">    </span>//MemFree: 7348 kB
  52. <span style="white-space:pre">    </span>//Buffers: 7892 kB
  53. <span style="white-space:pre">    </span>//Cached: 241852  kB
  54. <span style="white-space:pre">    </span>//SwapCached: 0 kB
  55. <span style="white-space:pre">    </span>//从fd文件中读取长度为buff的字符串再存到起始地址为buff这个空间里
  56. <span style="white-space:pre">    </span>fgets(buff, sizeof(buff), fd);
  57. <span style="white-space:pre">    </span>sscanf(buff, "%s %lu ", m->name1, &m->MemTotal);
  58. <span style="white-space:pre">    </span>fgets(buff, sizeof(buff), fd);
  59. <span style="white-space:pre">    </span>sscanf(buff, "%s %lu ", m->name2, &m->MemFree);
  60. <span style="white-space:pre">    </span>fgets(buff, sizeof(buff), fd);
  61. <span style="white-space:pre">    </span>sscanf(buff, "%s %lu ", m->name3, &m->Buffers);
  62. <span style="white-space:pre">    </span>fgets(buff, sizeof(buff), fd);
  63. <span style="white-space:pre">    </span>sscanf(buff, "%s %lu ", m->name4, &m->Cached);
  64. <span style="white-space:pre">    </span>fgets(buff, sizeof(buff), fd);
  65. <span style="white-space:pre">    </span>sscanf(buff, "%s %lu", m->name5, &m->SwapCached);
  66. <span style="white-space:pre">    </span>fclose(fd);     //关闭文件fd
  67. }
  68. int get_cpuoccupy(CPU_OCCUPY *cpust) //对无类型get函数含有一个形参结构体类弄的指针O
  69. {
  70. <span style="white-space:pre">    </span>FILE *fd;
  71. <span style="white-space:pre">    </span>char buff[256];
  72. <span style="white-space:pre">    </span>CPU_OCCUPY *cpu_occupy;
  73. <span style="white-space:pre">    </span>cpu_occupy = cpust;
  74. <span style="white-space:pre">    </span>fd = fopen("/proc/stat", "r");
  75. <span style="white-space:pre">    </span>fgets(buff, sizeof(buff), fd);
  76. <span style="white-space:pre">    </span>sscanf(buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice, &cpu_occupy->system, &cpu_occupy->idle, &cpu_occupy->lowait, &cpu_occupy->irq, &cpu_occupy->softirq);
  77. <span style="white-space:pre">    </span>fclose(fd);
  78. <span style="white-space:pre">    </span>return 0;
  79. }
  80. int cal_cpuoccupy(CPU_OCCUPY *o, CPU_OCCUPY *n)
  81. {
  82. <span style="white-space:pre">    </span>unsigned long od, nd;
  83. <span style="white-space:pre">    </span>double cpu_use = 0;
  84. <span style="white-space:pre">    </span>od = (unsigned long)(o->user + o->nice + o->system + o->idle + o->lowait + o->irq + o->softirq);//第一次(用户+优先级+系统+空闲)的时间再赋给od
  85. <span style="white-space:pre">    </span>nd = (unsigned long)(n->user + n->nice + n->system + n->idle + n->lowait + n->irq + n->softirq);//第二次(用户+优先级+系统+空闲)的时间再赋给od
  86. <span style="white-space:pre">    </span>double sum = nd - od;
  87. <span style="white-space:pre">    </span>double idle = n->idle - o->idle;
  88. <span style="white-space:pre">    </span>cpu_use = idle / sum;
  89. <span style="white-space:pre">    </span>printf("cpu_use1(idle) = %f\r\n", cpu_use);
  90. <span style="white-space:pre">    </span>idle = n->user + n->system + n->nice - o->user - o->system - o->nice;
  91. <span style="white-space:pre">    </span>cpu_use = idle / sum;
  92. <span style="white-space:pre">    </span>printf("cpu_use2(user+system+nice) = %f\r\n", cpu_use);
  93. <span style="white-space:pre">    </span>return 0;
  94. }
  95. int main( int argc, char **argv )
  96. {
  97. <span style="white-space:pre">    </span>MEM_OCCUPY mem_stat;
  98. <span style="white-space:pre">    </span>CPU_OCCUPY cpu_stat1;
  99. <span style="white-space:pre">    </span>CPU_OCCUPY cpu_stat2;
  100. <span style="white-space:pre">    </span>CAfcLowerTools m_afcLowerTools;
  101. //获取内存
  102. <span style="white-space:pre">    </span>get_memoccupy((MEM_OCCUPY *)&mem_stat);
  103. <span style="white-space:pre">    </span>printf(" [MemTotal] = %lu \n [MemFree] = %lu \n [Buffers] = %lu \n [Cached] = %lu \n [SwapCached] = %lu \n", mem_stat.MemTotal, mem_stat.MemFree, mem_stat.Buffers, mem_stat.Cached, mem_stat.SwapCached);
  104. //第一次获取cpu使用情况
  105. <span style="white-space:pre">    </span>get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);
  106. <span style="white-space:pre">    </span>Sleep(1000);
  107. //第二次获取cpu使用情况
  108. <span style="white-space:pre">    </span>get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);
  109. //计算cpu使用率
  110. <span style="white-space:pre">    </span>cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2);
  111. }