本文介绍了如何在 Linux 中用 C 获取进程的 PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 kill API 来终止一个进程.为此,我需要进程的进程 ID.我尝试使用:
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:
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");
是否有任何可用于获取进程 ID 的 API?
Is there any API that could be used to get the process id?
推荐答案
您正在获取 system
的返回状态.那不是pid.你想要这样的东西:
You are getting the return status of system
. That's not the pid. You want something like this:
char line[LEN];
FILE *cmd = popen("pidof...", "r");
fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);
这篇关于如何在 Linux 中用 C 获取进程的 PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!