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?
是否有任何API可用于获取进程ID?
5 个解决方案
#1
15
You are getting the return status of system
. That's not the pid. You want something like this:
您正在获得系统的返回状态。那不是pid。你想要这样的东西:
char line[LEN];
FILE *cmd = popen("pidof...", "r");
fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);
#2
2
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 pidof
using popen()
instead of system()
, but I'm sure there's a better way (the way pidof
itself uses). Perhaps it wanders through /proc
.
您可以使用popen()而不是system()来使用pidof的输出,但我确信有更好的方法(pidof本身使用的方式)。也许它徘徊/ proc。
#3
2
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);
}
#4
1
What is returned by the system
function is the return code from the command being executed.
系统函数返回的是正在执行的命令的返回码。
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的内容。
#5
1
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:
我知道这不是一个新线程,但由于我最近才遇到同样的问题,我会问你。你有没有看到其中一个:
- Get process id by name in C
- 在C中按名称获取进程ID
- C function to find a process ID and kill it
- C函数找到进程ID并将其杀死
- man sysctl(3)
- man sysctl(3)
You can use sysctl
to 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为您提供所需的信息,而无需通过系统(“bla,bla”)调用。一开始似乎要复杂得多,但根据您的需求可能值得。
#1
15
You are getting the return status of system
. That's not the pid. You want something like this:
您正在获得系统的返回状态。那不是pid。你想要这样的东西:
char line[LEN];
FILE *cmd = popen("pidof...", "r");
fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);
#2
2
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 pidof
using popen()
instead of system()
, but I'm sure there's a better way (the way pidof
itself uses). Perhaps it wanders through /proc
.
您可以使用popen()而不是system()来使用pidof的输出,但我确信有更好的方法(pidof本身使用的方式)。也许它徘徊/ proc。
#3
2
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);
}
#4
1
What is returned by the system
function is the return code from the command being executed.
系统函数返回的是正在执行的命令的返回码。
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的内容。
#5
1
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:
我知道这不是一个新线程,但由于我最近才遇到同样的问题,我会问你。你有没有看到其中一个:
- Get process id by name in C
- 在C中按名称获取进程ID
- C function to find a process ID and kill it
- C函数找到进程ID并将其杀死
- man sysctl(3)
- man sysctl(3)
You can use sysctl
to 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为您提供所需的信息,而无需通过系统(“bla,bla”)调用。一开始似乎要复杂得多,但根据您的需求可能值得。