How can I look at process in the system like ps does?

How can I look at process in the system like ps does?


You really don't want to do this.



The most portable way, by far, is to do popen(pscmd, "r") and
parse the output. (pscmd should be something like `"ps -ef"' on
SysV systems; on BSD systems there are many possible display options:
choose one.)



In the examples section, there are two complete versions of this; one
for SunOS 4, which requires root permission to run and uses the
`kvm_*' routines to read the information from kernel data
structures; and another for SVR4 systems (including SunOS 5), which uses
the `/proc' filesystem.



It's even easier on systems with an SVR4.2-style `/proc'; just read
a psinfo_t structure from the file `/proc/PID/psinfo' for each PID
of interest. However, this method, while probably the cleanest, is also
perhaps the least well-supported. (On FreeBSD's `/proc', you read a
semi-undocumented printable string from `/proc/PID/status'; Linux
has something similar.)






Home FAQ