编程获取linux的CPU使用率内存占用率

本文介绍了如何编程获取Linux系统的CPU和内存使用率,包括使用`statfs`和`statvfs`系统调用计算硬盘使用率,以及通过解析`/proc/stat`获取CPU利用率的shell脚本示例。
摘要由CSDN通过智能技术生成
Linux下提供top、ps命令查看当前cpu、mem使用情况,简要介绍如下:

一、使用ps查看进程的资源占用

ps -aux

查看进程信息时,第三列就是CPU占用。

[root@localhost utx86]# ps -aux | grep my_process
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ



root   14415  3.4  0.9   37436  20328  pts/12   SL+  14:18   0:05 ./my_process
root   14464  0.0   0.0   3852   572    pts/3    S+   14:20   0:00 grep my_process
每一列含义如下

USER   PID   %CPU %MEM  VSZ  RSS TTY  STAT   START  TIME   COMMAND

即my_process进程当前占用cpu 3.4%, 内存0.9%

二、top动态查看系统负荷

top -n 1

显示后退出

[root@localhost utx86]# top -n 1
top - 14:23:20 up  5:14, 14 users,  load average: 0.00, 0.04, 0.01
Tasks: 183 total,   1 running, 181 sleeping,   1 stopped,   0 zombie
Cpu(s):  1.8%us,  1.4%sy,  0.0%ni, 95.8%id,  0.7%wa,  0.1%hi,  0.2%si,  0.0%st
Mem:   2066240k total,  1507316k used,   558924k free,   190472k buffers
Swap:  2031608k total,       88k used,  2031520k free,  1087184k cached

1、获取cpu占用情况

[root@localhost utx86]# top -n 1 |grep Cpu
Cpu(s):  1.9%us,  1.3%sy,  0.0%ni, 95.9%id,  0.6%wa,  0.1%hi,  0.2%si,  0.0%st

解释:1.9%us是用户占用cpu情况

1.3%sy,是系统占用cpu情况

得到具体列的值:

[root@localhost utx86]# top -n 1 |grep Cpu | cut -d "," -f 1 | cut -d ":" -f 2
1.9%us
[root@localhost utx86]# top -n 1 |grep Cpu | cut -d "," -f 2
1.3%sy

2、获得内存占用情况

[root@localhost utx86]# top -n 1 |grep Mem
Mem:   2066240k total,  1515784k used,   550456k free,   195336k buffers

获得内存情况指定列

[root@localhost c++_zp]# top -n 1 |grep Mem | cut -d "," -f 1 | cut -d ":" -f 2
2066240k total
[root@localhost c++_zp]# top -n 1 |grep Mem | cut -d "," -f 2
1585676k used

三、编程实现

现在可以通过程序将cpu使用率、内存使用情况保存到文件中
// test.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
system("top -n 1 |grep Cpu | cut -d \",\" -f 1 | cut -d \":\" -f 2 >cpu.txt");
system("top -n 1 |grep Cpu | cut -d \",\" -f 2 >>cpu.txt");
system("top -n 1 |grep Mem | cut -d \",\" -f 1 | cut -d \":\" -f 2 >>cpu.txt");
system("top -n 1 |grep Mem | cut -d \",\" -f 2 >>cpu.txt");
return 0;
}

编译、运行:

[root@localhost study]# g++ test.cpp
[root@localhost study]# ./a.out
[root@localhost study]# cat cpu.txt
2.1%us
1.5%sy
2066240k total
1619784k used

四、硬盘使用率编程实现

1.硬盘使用率 命令df -lh

2.程序实现,调用statfs

int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
struct statfs {
long    f_type;    /* type of filesystem (see below) */
long    f_bsize;    /* optimal transfer block size */
long    f_blocks;  /* total data blocks in file system */
long    f_bfree;    /* free blocks in fs */
long    f_bavail;  /* free blocks avail to non-superuser */
long    f_files;    /* total file nodes in file system

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值