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 !
You can get the process ID of a process by calling getpid . The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With