在linux中,是否有一个内置的C库函数来获取机器的CPU负载?我可以编写自己的函数,在/proc中打开和解析文件,但似乎应该有更好的方法。
发布于 2009-07-29 13:35:17
如果您真的想要一个c接口,请使用getloadavg(),它也可以在不使用/proc的unixes中工作。
它有一个包含所有细节的手册页。
发布于 2009-07-29 07:54:08
在linux上获取有关CPU负载信息的首选方法是从/proc/stat、/proc/loadavg和/proc/uptime读取。所有普通的linux实用程序(如top )都使用此方法。
发布于 2009-07-29 11:52:46
来自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.https://stackoverflow.com/questions/1198573
复制相似问题