linux查看cpu使用率proc,Linux中如何获取cpu使用率

我想到的方法是读取/proc/stat,但不同版本的Linux,它的输出是不同的。

第二种方法是使用top命令来查看,但如何获取其输出呢??我要做成一个c函数的方式供其他的程序调用,返回一个cpu使用率的值。

请各位大虾帮帮忙!!谢了。

|

对/proc吧. 没有更好的方法了.

在网上搜搜,有一个库封装了读/proc系列值的函数.好像是ReadProc

|

top也是调的/proc里面的,你根据不同版本做不同的解析更好.

|

Extracting Information from /proc

Most of the entries in /proc provide information formatted to be readable by humans, but the formats are simple enough to be easily parsed. For example, /proc/cpuinfo contains information about the system CPU (or CPUs, for a multiprocessor machine). The output is a table of values, one per line, with a description of the value and a colon preceding each value.

For example, the output might look like this:

% cat /proc/cpuinfo

processor       : 0

vendor_id       : GenuineIntel

cpu family      : 6

model           : 5

model name      : Pentium II (Deschutes)

stepping        : 2

cpu MHz         : 400.913520

cache size      : 512 KB

fdiv_bug        : no

hlt_bug         : no

sep_bug         : no

f00f_bug        : no

coma_bug        : no

fpu             : yes

fpu_exception   : yes

cpuid level     : 2

wp              : yes

flags           : fpu vme de pse tsc msr pae mce cx8 apic sep

mtrr pge mca cmov pat pse36 mmx fxsr

bogomips        : 399.77

We'll describe the interpretation of some of these fields in Section 7.3.1, "CPU Information."

A simple way to extract a value from this output is to read the file into a buffer and parse it in memory using sscanf. Listing 7.1 shows an example of this. The program includes the function get_cpu_clock_speed that reads from /proc/cpuinfo into memory and extracts the first CPU's clock speed.

Listing 7.1 (clock-speed.c) Extract CPU Clock Speed from /proc/cpuinfo

#include

#include

/* Returns the clock speed of the system's CPU in MHz, as reported by

/proc/cpuinfo. On a multiprocessor machine, returns the speed of

the first CPU. On error returns zero.  */

float get_cpu_clock_speed ()

{

FILE* fp;

char buffer[1024];

size_t bytes_read;

char* match;

float clock_speed;

/* Read the entire contents of /proc/cpuinfo into the buffer.  */

fp = fopen ("/proc/cpuinfo", "r");

bytes_read = fread (buffer, 1, sizeof (buffer), fp);

fclose (fp);

/* Bail if read failed or if buffer isn't big enough.  */

if (bytes_read == 0 || bytes_read == sizeof (buffer))

return 0;

/* NUL-terminate the text.  */

buffer[bytes_read] == '';

/* Locate the line that starts with "cpu MHz".  */

match = strstr (buffer, "cpu MHz");

if (match == NULL)

return 0;

/* Parse the line to extract the clock speed.  */

sscanf (match, "cpu MHz  :  %f", &clock_speed);

return clock_speed;

}

int main ()

{

printf ("CPU clock speed: %4.0f MHzn", get_cpu_clock_speed ());

return 0;

}

Be aware, however, that the names, semantics, and output formats of entries in the /proc file system might change in new Linux kernel revisions. If you use them in a program, you should make sure that the program's behavior degrades gracefully if the /proc entry is missing or is formatted unexpectedly.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值