问题
Can you suggest me a system call that retrieves the current running processes? (I have to write a C function like top)
I tried to read the proc/
folder but is not good in my case.
回答1:
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.
Read proc(5) man page.
You could use libprocps
which is reading /proc/
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)
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...
回答2:
ps
ps aux
Where:
-A: select all processes
a: select all processes on a terminal, including those of other users
x: select processes without controlling ttys
Personally I like to use:
ps -ef
来源:https://stackoverflow.com/questions/16482575/show-the-list-of-running-processes-in-c