Linux中如何获取cpu使用率

Penny 2006-08-21 02:55:53
我想到的方法是读取/proc/stat,但不同版本的Linux,它的输出是不同的。
第二种方法是使用top命令来查看,但如何获取其输出呢??我要做成一个c函数的方式供其他的程序调用,返回一个cpu使用率的值。
请各位大虾帮帮忙!!谢了。
...全文
1233 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Penny 2006-08-22
  • 打赏
  • 举报
回复
TO;YanDong_8212(谢科)、upcuiling
如果是根据不同的Linux,编写不同的程序,那直接分析文件是可以的,但这显然不符合我的要求,我想写一个通用的函数。
upcuiling 2006-08-21
  • 打赏
  • 举报
回复
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 <stdio.h>
#include <string.h>

/* 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] == '\0';
/* 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 MHz\n", 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.

fytzzh 2006-08-21
  • 打赏
  • 举报
回复
对/proc吧. 没有更好的方法了.

在网上搜搜,有一个库封装了读/proc系列值的函数.好像是ReadProc
YanDong_8212 2006-08-21
  • 打赏
  • 举报
回复
top也是调的/proc里面的,你根据不同版本做不同的解析更好.

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧