But I want to this functionality using a POSIX function. I am not looking for a ready made code.
No POSIX function exists to list running processes. That is OS specific, not portable, rarely needed by applications and thus not part of POSIX.
But since you need this on Linux, the most POSIXy solution would be to use functions opendir()
/readdir()
/closedir()
to iterate over the content of /proc
special file system.
All numeric entries in the directory are PIDs of running processes. Check the content the man 5 proc
for details what information about the running processes can be obtained from there. Then you can use the open()
/read()
/close()
or readlink()
calls to retrieve the information about a particular process from the /proc/NNN/*
files.
On Linux, the standard tools like ps
and top
use the /proc
to gather the information about processes. It is official Linux' interface to the information about running processes.