显示用C正在运行的进程列表 [英] Show the list of running processes in C
问题描述
您可以给我建议一个系统调用检索当前正在运行的进程? (我有写的 C 像顶部功能)
Can you suggest me a system call that retrieves the current running processes? (I have to write a C function like top)
我想读 PROC /
文件夹,但并不好我的情况。
I tried to read the proc/
folder but is not good in my case.
推荐答案
的仅的方式做到这一点在Linux上访问的/ proc /
伪文件系统。请记住,的/ proc /
文件在磁盘上没有真正的文件,因此I / O(即读的/ proc /
文件)是相当快的。
The only way to do that on Linux is to access the /proc/
pseudo-file system. Remember that /proc/
files are not "real" files on disk, so I/O (i.e. reading /proc/
files) is quite fast.
PROC(5)手册页。
您可以使用 libprocps
这是读的/ proc /
的 PS
和顶
(和 HTOP
等...)都使用命令的/ proc /
;如果你想从一个程序(这可能是一个坏主意)内使用它们使用的的popen(3)(以获得它们的输出)不系统(3)
The ps
and top
(and htop
etc...) commands are all using /proc/
; if you want to use them from inside a program (which may be a bad idea) use popen(3) (to get their output) not system(3)
所以,让你可以使用 READDIR
正在运行的进程上的/ proc /
,然后阅读的/ proc / * / STAT
文件,想起那些状态为研究
等...等...
So to get the running processes you could use readdir
on /proc/
and then read the /proc/*/stat
files, remembering those whose status is R
etc... etc...
这篇关于显示用C正在运行的进程列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!