按名称查找进程的PID没有用popen()或系统() [英] Find PID of a Process by Name without Using popen() or system()

查看:454
本文介绍了按名称查找进程的PID没有用popen()或系统()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过程的名字,我已经到杀()信号发送到该进程,但我需要它的PID调用杀()。我只想用:

I've a process name and I've to send a kill() signal to that process but I need its PID to call kill(). I would only like to use:

popen("pidof process_name");

作为最后一件事。是否有任何其他的方法来找出进程的PID?我能想到的一个方法是发送一个插座要求这一进程,并要求其PID。

as the last thing. Is there any other way to find out the process' PID? One way I could think of is to send a socket request to that process and ask to its PID.

另一种方法是一个简单的code我正在写一个有点太复杂了:做什么的的pidof命令的源$ C ​​$ C 其实就是做(它使用一个函数调用 find_pid_by_name()但在做了很多事情)。

The other way is a little too complicated for a simple code I'm writing: to do what pidof command's source code is actually doing (it uses a function call find_pid_by_name() but that's doing a lot of things).

如果没有简单的解决方案是可能的,我已经做到这一点:

If no simple solution is possible, I've to do this:

system("pkill <process_name>");

和检查它的返回code。但pkill的可用于确保在所有Linux机器?

and check its return code. But will pkill be available for sure on all Linux machines?

推荐答案

您提到您正在使用Linux操作系统。这不是最干净的解决方案,但你可以通过在/ proc每个条目并检查CMDLINE进程名反对你在找什么。

You mentioned you were using linux. It isn't the cleanest solution, but you can go through every entry in /proc and check the process name in cmdline against what you are looking for.

这篇关于按名称查找进程的PID没有用popen()或系统()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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