如何获得一个进程的PID在Linux下用C [英] How to get the PID of a process in Linux in C

查看:925
本文介绍了如何获得一个进程的PID在Linux下用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用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?

推荐答案

你得到的返回状态系统。这不是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);

这篇关于如何获得一个进程的PID在Linux下用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆