[C] Process pid by name
I use linux OS.
I've already written a function that allow me to get the process name by pid. (searching in /proc). Now I'd like to perform the inverse task.I mean get the process pid by its name.
I could write a function that search in every folder in /proc for the process name, but i think that should be another solution. Something like include a kernel structure that keep information on the active process.
Can you suggest me another way? or some references where i can study?
I don't know the specifics of this, ie, the kernel does not have to have the name at all, but it might. I guess it does, since it puts those command lines in /proc.
Originally Posted by Dedalus
The big issue is that if the normal "user space" tools are not good enough, to get some sort of customized output from the kernel would probably require you to write a separate kernel module, which that will be quite a task.
This is just an educated guess, tho, maybe somebody is aware of something else.
Anyway, even if you got the info from the kernel, I don't think that will be some kind of big advantage over just processing the data that the kernel already maintains in /proc. You will still have to search thru a list of names, all that will be saved is creating the list. Which shouldn't take more than one or two functions, maybe 30-60 lines; you put all the names in a struct array of some sort:
and whatever else you might want to know. Generally there is only a few hundred processes anyway. You could use popen() with the ps command to do this too.
Check out what is in the /proc/[PID]/maps file for your PID in question, than compare that to running: ps -ef | grep [PID]. Please do not use the [PID] string, simply replace this with the exact integer pid you are seeking.
A book I have been reading "Self Service Linux" addresses this in good detail and how to get all the PID information including its complete memory signature on the system.