I am using the getpid and get the pid of current process. Now I am try to get the pid of other process using process name. How to get the other process pid?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
printf("My pid:%d\n", getpid());
return 0;
}
Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.
Since PID is an unique identifier for a process, there's no way to have two distinct process with the same PID. Unless the processes are running in a separate PID namespaces (and thus can have the same PID).
The getpid() function returns the process ID of the calling process. The getpgrp() function returns the process group ID of the calling process. The getppid() function returns the parent process ID of the calling process.
1: The most common way, used by daemons. Store the pid number in a file/files. Then other processes can easily find them.
2: Portable way, spawn a child process to executes ps
with a pipe. Then you can parse the text output and find your target process.
3: Non-portable way, parse the /proc/ filesystem
Often, 1 is combined with 2 or 3, in order to verify that the pid is correct.
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