Show the list of running processes in C

邮差的信 提交于 2019-12-08 14:21:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!