获取linux系统运行情况,linux下获取内存和cpu使用情况

Get the target computer is running Linux system hardware occupation, wrote a few small program directly after.

Proc file is read to obtain.

cpu usage: / proc / stat, memory usage: / proc / meminfo

Look at the program:

/ ************************************************* **************

* @ File: statusinfo.c

*

* @ Brief: obtained from a Linux system CPU and memory usage

*

* @ Version 1.0

*

************************************************** ************* /

typedef struct PACKED / / define a cpu occupy the structure

{

char name [20]; / / define an array of type char name name 20 elements

unsigned int the user; / / define the user of an unsigned int type

unsigned int nice; / / define an unsigned int type nice

unsigned int system ;/ / define an unsigned int type system

unsigned int idle; / / Define an unsigned int type idle

} CPU_OCCUPY;

typedef struct PACKED / / define a mem occupy the structure

{

char name [20]; / / define an array of type char name name 20 elements

unsigned long total;

char name2 [20];

unsigned long free;

} MEM_OCCUPY;

get_memoccupy (MEM_OCCUPY * mem) / / untyped get function contains a parameter structure class get pointer O

{

FILE * fd;

int n;

char BUFF [256];

MEM_OCCUPY * m;

m = mem;

fd = fopen ("/ proc / meminfo", "r");

fgets (buff, sizeof (buff), fd);

fgets (buff, sizeof (buff), fd);

fgets (buff, sizeof (buff), fd);

fgets (buff, sizeof (buff), fd);

sscanf (buff, "% s% u% s", m-> name, & m-> total, m-> name2);

fgets (buff, sizeof (buff), fd); / / read from fd file length buff string and then saved to the starting address of this space buff

sscanf (buff, "% s% u", m-> name2, & m-> free, m-> name2);

fclose (fd); / / close the file fd

}

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) ;/ / first (user + priority + system + idle) time and then assigned to the od

nd = (unsigned long) (n-> user + nice + n-> system + n-> idle) ;/ / second (user + priority + system + idle) and then assigned to the od

id = (unsigned long) (n-> user - o-> user); / / user the first time and the second time the difference is assigned to id

sd = (unsigned long) (n-> system - o-> system) ;/ / system first and the second time the difference is assigned to sd

if ((nd-od)! = 0)

cpu_use = (int) ((sd + id) * 10000) / (nd-od); / / ((user + system) well-behaved 100) in addition to the (first and second time) is assigned to g_cpu_used

else cpu_use = 0;

/ / Printf ("cpu:% u / n", cpu_use);

return cpu_use;

}

get_cpuoccupy (CPU_OCCUPY * cpust) / / untyped get function contains a parameter structure class get pointer O

{

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);

}

int main ()

{

CPU_OCCUPY cpu_stat1;

CPU_OCCUPY cpu_stat2;

MEM_OCCUPY mem_stat;

int cpu;

/ / Get the memory

get_memoccupy ((MEM_OCCUPY *) & mem_stat);

/ / First get cpu usage

get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1);

sleep (10);

/ / Get CPU usage

get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat2);

/ / Calculate the cpu utilization

cpu = cal_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1, (CPU_OCCUPY *) & cpu_stat2);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux的shell脚本可以利用内存CPU来执行各种操作和任务,以下是几种使用内存CPU情况: 1. 内存使用:在shell脚本中,您可以使用变量和数组来存储数据,这些数据会在内存中占用一定的空间。如果您需要处理大量数据,您可能需要分配更多的内存来存储数据。您可以使用Linux系统提供的free命令来查看系统内存使用情况。 2. CPU占用:在shell脚本中,您可以运行各种命令和程序,这些命令和程序会占用系统CPU资源。如果您需要执行一些耗费CPU资源的任务,您可能需要在编写脚本时考虑到这一点,并尽可能优化您的代码以最大化CPU使用效率。您可以使用Linux系统提供的top命令来查看系统CPU使用情况,并找出哪些进程占用了大量的CPU资源。 需要注意的是,在编写shell脚本时,应该尽可能地使用系统资源,避免浪费资源和影响系统性能。另外,您还可以使用Linux系统提供的一些工具来优化您的shell脚本,如使用awk和sed来处理文本数据,使用xargs和parallel来并行执行命令等。 ### 回答2: 要运行输出可用内存CPU占用情况,可以使用Linux的shell脚本来实现。 首先,使用内存信息的命令是`free`,它可以显示系统内存使用情况。可以使用`grep`和`awk`命令来提取可用内存的信息并进行格式化输出。 ```shell # 获取可用内存信息并格式化输出 memory_info=$(free -m | grep Mem | awk '{print $4}') echo "可用内存:${memory_info} MB" ``` 接下来,获取CPU占用情况可以使用`top`命令。`top`命令可以显示系统的实时进程和CPU使用情况,但默认情况下会一直显示,需要使用`head`和`grep`命令来筛选出CPU占用信息并进行格式化输出。 ```shell # 获取CPU占用信息并格式化输出 cpu_info=$(top -n 1 -b | head -n 12 | grep '%Cpu' | awk '{print $2}') echo "CPU占用:${cpu_info}%" ``` 最后,将上述代码整合到一个shell脚本文件中,给文件添加执行权限,并运行即可得到可用内存CPU占用情况的输出。 ```shell #!/bin/bash # 获取可用内存信息并格式化输出 memory_info=$(free -m | grep Mem | awk '{print $4}') echo "可用内存:${memory_info} MB" # 获取CPU占用信息并格式化输出 cpu_info=$(top -n 1 -b | head -n 12 | grep '%Cpu' | awk '{print $2}') echo "CPU占用:${cpu_info}%" ``` 以上的脚本可以在Linux系统运行,并输出可用内存CPU占用情况。 ### 回答3: 要求在Linux的shell脚本中运行并输出可用内存CPU占用情况,可以使用下面的方法: 1. 通过`free`命令获取系统内存使用情况使用`-h`选项以人类可读的方式显示内存使用情况,`awk`命令可以帮助我们提取所需的信息。可以使用以下命令来获取可用内存的值: ``` free -h | awk '/Mem/ {print $7}' ``` 该命令先运行`free -h`命令获取内存信息,然后使用`awk`过滤出包含"Mem"的行,并打印第7列的值,即可用内存。 2. 通过`top`命令获取系统CPU占用情况。我们可以使用`top`命令的`-n1`选项,表示只运行一次,并且使用`-b`选项以批处理模式运行,将输出结果直接输出给其他命令进行处理。利用`grep`和`awk`命令可以提取所需的CPU使用信息。以下是获取CPU占用百分比的命令示例: ``` top -n1 -b | grep "Cpu(s)" | awk '{print $2+$4}' ``` 此命令运行`top`命令获取CPU信息,并且使用`grep`过滤出包含"Cpu(s)"的行,然后使用`awk`打印第2列和第4列的和,即CPU占用。 综上,我们可以将上述两个命令结合在一起,编写一个shell脚本,如下所示: ```shell #!/bin/bash # 获取可用内存 free_mem=$(free -h | awk '/Mem/ {print $7}') # 获取CPU占用 cpu_usage=$(top -n1 -b | grep "Cpu(s)" | awk '{print $2+$4}') # 输出结果 echo "可用内存: $free_mem" echo "CPU占用: $cpu_usage%" ``` 保存脚本并赋予执行权限,然后通过运行脚本来获取可用内存CPU占用情况
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值