如何在 Linux 中用 C 获取进程的 PID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8166415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 22:47:15  来源:igfitidea点击:

How to get the PID of a process in Linux in C

clinuxprocess

提问by ravi J

I need to kill a process using the kill API. For that I need the process id of the process. I tried to get it using:

我需要使用 kill API 来终止一个进程。为此,我需要进程的进程 ID。我试图使用它:

ret = system("pidof -s raj-srv");

but it is not returning the correct value. I dont want to kill the process using this:

但它没有返回正确的值。我不想用这个来终止进程:

ret = system("pkill raj");

Is there any API that could be used to get the process id?

是否有任何可用于获取进程 ID 的 API?

回答by Some programmer dude

What is returned by the systemfunction is the return code from the command being executed.

system函数返回的是正在执行的命令的返回码。

What you can do is something like this:

你可以做的是这样的:

system("pidof -s raj-srv > /tmp/pid-of-raj-srv");

And then read the contents of the file /tmp/pid-of-raj-srv.

然后读取文件的内容/tmp/pid-of-raj-srv

回答by cnicutar

You are getting the return status of system. That's not the pid. You want something like this:

您正在获得 的退货状态system。那不是pid。你想要这样的东西:

char line[LEN];
FILE *cmd = popen("pidof...", "r");

fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);

pclose(cmd);

回答by Marcelo Cantos

The system()call doesn't return the output of pidof, it returns pidof's return code, which is zero if it succeeds.

system()调用不返回 的输出pidof,而是返回pidof的返回码,如果成功则返回值为零。

You could consume the output of pidofusing popen()instead of system(), but I'm sure there's a better way (the way pidofitself uses). Perhaps it wanders through /proc.

您可以使用pidofusingpopen()而不是的输出system(),但我确信有更好的方法(pidof本身使用的方式)。也许它徘徊通过/proc

回答by j4x

I know it is not a fresh thread, but as I have only recently faced the same question, I will ask you. Did you see one of this:

我知道这不是一个新鲜话题,但因为我最近才遇到同样的问题,所以我会问你。你有没有看到其中之一:

You can use sysctlto give you the needed information without having to pass through a system( "bla, bla" )call. It seems to be far more complicated at first, but may be worth depending on your needs.

您可以使用它sysctl来为您提供所需的信息,而无需通过system( "bla, bla" )电话。起初它似乎要复杂得多,但可能值得根据您的需要。

回答by Alok Prasad

There could be multiple instances of processes running in that case , pidof returns strings of pid seperated by space .

在这种情况下可能有多个进程正在运行,pidof 返回由空格分隔的 pid 字符串。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

main()
{
        char pidline[1024];
        char *pid;
        int i =0
        int pidno[64];
        FILE *fp = popen("pidof bash","r");
        fgets(pidline,1024,fp);

        printf("%s",pidline);
        pid = strtok (pidline," ");
        while(pid != NULL)
                {

                        pidno[i] = atoi(pid);
                        printf("%d\n",pidno[i]);
                        pid = strtok (NULL , " ");
                        i++;
                }

        pclose(fp);
}