Srikanth Srikanth - 11 months ago 68
C Question

Find PID of a Process by Name without Using popen() or system()

I've a process name and I've to send a

signal to that process but I need its PID to call
. I would only like to use:

popen("pidof process_name");

as the last thing. Is there any other way to find out the process' PID? One way I could think of is to send a socket request to that process and ask to its PID.

The other way is a little too complicated for a simple code I'm writing: to do what pidof command's source code is actually doing (it uses a function call
but that's doing a lot of things).

If no simple solution is possible, I've to do this:

system("pkill <process_name>");

and check its return code. But will pkill be available for sure on all Linux machines?

Answer Source

You mentioned you were using linux. It isn't the cleanest solution, but you can go through every entry in /proc and check the process name in cmdline against what you are looking for.