• Uncategorized

About c++ : Get-process-ID-by-name

Question Detail

I’d like to get a process ID given its name under Linux.

Is there a simple way to do this ?

I haven’t found anything on C++ that could be easily usable !

Question Answer

If going for ‘easily usable’,

char buf[512];
FILE *cmd_pipe = popen("pidof -s process_name", "r");

fgets(buf, 512, cmd_pipe);
pid_t pid = strtoul(buf, NULL, 10);

pclose( cmd_pipe );  

is the way to go.

Yeah, it’s ugly, I know. It’s much better to go and read pidof source code.

You can use the information in /proc.

Here is an example.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.