On Linux, another way to do this may include checking the contents of the /proc directory. Numbered directories are process identifiers, and subdirectories containing the cmdline file show the name of the command.
For example, if /proc/1234/cmdline contains the value foo , then the process foo has the identifier 1234. Thus, names can be mapped to PIDs using the standard file access functions in C. See proc(5) for more information.
Alex reynolds
source share