在Linux上,有没有内置的C库函数可以获取机器的CPU负载? 我可以编写自己的函数来打开和解析/proc目录下的文件,但似乎应该有更好的方法。
- 不需要可移植性
- 不能需要除基本RHEL4安装之外的任何库。
在Linux上,有没有内置的C库函数可以获取机器的CPU负载? 我可以编写自己的函数来打开和解析/proc目录下的文件,但似乎应该有更好的方法。
/proc/stat
更加相关。 - m-ric获取Linux CPU负载信息的首选方法是从/proc/stat、/proc/loadavg和/proc/uptime中读取。所有常规的Linux实用工具,如top,都使用这种方法。
来自 proc (5) 手册:
/proc/loadavg
The first three fields in this file are load average figures
giving the number of jobs in the run queue (state R) or waiting
for disk I/O (state D) averaged over 1, 5, and 15 minutes. They
are the same as the load average numbers given by uptime(1) and
other programs. The fourth field consists of two numbers sepaâ
rated by a slash (/). The first of these is the number of curâ
rently executing kernel scheduling entities (processes,
threads); this will be less than or equal to the number of CPUs.
The value after the slash is the number of kernel scheduling
entities that currently exist on the system. The fifth field is
the PID of the process that was most recently created on the
system.
我的理解是,解析/proc的内容是官方提供的接口之一(那里有许多文件在呈现给用户之前应该先被解析)。