[英]How can you get a Linux top like output using C
I am trying to get the CPU usage and details of all the running processes on embedded Linux while a C test is running. 我正在尝试在C测试运行时获取CPU使用率以及嵌入式Linux上所有正在运行的进程的详细信息。 Is there a
top
equivalent in C that I could use ? 我可以使用C中的
top
等效项吗?
I think you need to parse the /proc file system. 我认为您需要解析/ proc文件系统。 The kernel has a special file system which is normally mounted at /proc with a unique directory for each process (eg. /proc/30 belongs to the process with PID=30).
内核有一个特殊的文件系统,该文件系统通常安装在/ proc上,每个进程都有一个唯一的目录(例如/ proc / 30属于PID = 30的进程)。 The files in these directories are all text files and contains lots of info on the running processes.
这些目录中的文件都是文本文件,并且包含有关正在运行的进程的大量信息。
Instead of top
, you should probably use ps
with the right arguments and you can parse the output. 可以使用
ps
和正确的参数来代替top
,然后可以解析输出。
You'll have to call that with a system
call or popen
from your C code. 您必须使用
system
调用来调用它,或者从C代码中popen
。 You cannot get this information strictly inside your C code (at least I don't think you can). 您不能严格在C代码内部获得此信息(至少我认为不能)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.