加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:03:10 所属栏目:百科 来源:网络整理
导读:我正在使用getpid并获取当前进程的pid.现在我尝试使用进程名称获取其他进程的pid.如何获得其他进程pid? #include stdio.h#include sys/types.h#include unistd.hint main(void){ printf("My pid:%dn",getpid()); return 0;} 解决方法 您可以使用命令程序pi
我正在使用getpid并获取当前进程的pid.现在我尝试使用进程名称获取其他进程的pid.如何获得其他进程pid?

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    printf("My pid:%dn",getpid());

    return 0;
}

解决方法

您可以使用命令程序pidof的popen()来获取任何程序的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读