我正在使用 getpid 并获取当前进程的 pid。现在我尝试使用进程名称获取其他进程的 pid。如何获取对方进程pid?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
printf("My pid:%d\n", getpid());
return 0;
}
最佳答案
您可以使用popen()
和命令程序pidof
来获取任何程序的pid
。
像这样:
char line[total_length];
FILE * command = popen("pidof ...","r");
fgets(line,total_length,command);
pid_t pid = strtoul(line,NULL,10);
pclose(command);
编辑:
关于c - 如何在c中获取另一个进程的pid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721644/