Get process id by name

I would like to get the process identifier with his name under Linux.

Is there an easy way to do this?

I did not find anything in C ++ that could be easily used!

+4
source share
2 answers

You can use the information in /proc .

Here is an example.

+3
source

If itโ€™s easy to use,

 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 ); 

- way.

Yes, this is ugly, I know. It is much better to go and read the pidof source code.

+4
source

All Articles