c - 如何在c中获取另一个进程的pid?

标签 c linux system embedded-linux

我正在使用 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);

编辑:

请参阅:How to get the PID of a process in Linux in C

关于c - 如何在c中获取另一个进程的pid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721644/

相关文章:

c++ - 解码像 FAST 这样的数据协议(protocol)的快速方法是什么?在 FAST 中,数据以字节编码,位作为存在标志?

c - 获取列表中节点的当前值和位置

linux - 如何在 Mac OS 上使用 Cron 更新自制软件

c - 运行程序3秒

C# 即使具有管理员权限也无法访问系统文件

iphone - SystemSoundID有时随设备音量而变化

c - 使用 O_CREAT 调用 open() 的默认模式是什么以及如何在打开/创建文件时正确设置它

c - 如何在c中使用MMFdata?

C 变量初始化 valgrind 提示

linux - 递归编辑特定名称的文件