杀死以 popen 启动的进程
使用popen
打开一个进程的管道后,有没有办法杀死已经启动的进程? (使用 pclose
不是我想要的,因为它将等待进程完成,但我需要杀死它。)
After opening a pipe to a process with popen
, is there a way to kill the process that has been started? (Using pclose
is not what I want because that will wait for the process to finish, but I need to kill it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不要使用
popen()
,编写您自己的包装器来执行您想要的操作。fork()
相当简单,然后替换stdin
& 标准输出使用
dup2()
,然后对您的孩子调用exec()
。这样,您的父级将拥有准确的子级 PID,您可以使用
kill()
对此。谷歌搜索“popen2()实现”的一些示例代码
如何实现
popen()
正在做的事情。 只有十几行长的。 取自 dzone.com 我们可以看到
一个如下所示的示例:
注意:似乎 popen2() 就是您想要的,但我的发行版似乎没有提供此方法。
Don't use
popen()
, write your own wrapper that does what you'd like.It's fairly straightforward to
fork()
, and then replacestdin
&stdout
by using
dup2()
, and then callingexec()
on your child.That way, your parent will have the exact child PID, and you can use
kill()
on that.Google search for "popen2() implementation" for some sample code on
how to implement what
popen()
is doing. It's only a dozen or so lineslong. Taken from dzone.com we can see
an example that looks like this:
NB: Seems like popen2() is what you want, but my distribution doesn't seem to come with this method.
这是 popen2 的改进版本(感谢 Sergey L.)。 slacy发布的版本并不返回popen2中创建的进程的PID,而是返回分配给
sh
的PID。新版本将被调用
Here is an improved version of popen2 (credit is due to Sergey L.). The version posted by slacy does not return the PID of the process created in popen2, but the PID assigned to
sh
.The new version is to be called with
popen 实际上并没有启动一个线程,而是派生了一个进程。 当我查看定义时,似乎没有一种简单的方法可以获取该进程的 PID 并杀死它。 可能有一些困难的方法,比如检查进程树,但我想你最好使用 Pipe、fork 和 exec 函数来模仿 popen 的行为。 然后你可以使用从 fork() 获得的 PID 来终止子进程。
popen does not actually start a thread, but rather forks a process. As I look at the definition, it doesn't look like there is an easy way to get PID of that process and kill it. There might be difficult ways like examining process tree, but i guess you'd be better off with using pipe, fork and exec functions to mimic behaviour of popen. Then you can use PID you get from fork() to kill the child process.
实际上,如果进程正在执行 I/O(应该如此,否则为什么使用
popen
而不是system
(3)?),则pclose
应该在下次尝试读取或写入时用SIGPIPE
敲击它,并且它应该很好地倒下:-)Actually if the process is doing I/O (which it should be, otherwise why
popen
instead ofsystem
(3)?), thenpclose
should whack it with aSIGPIPE
the next time it tries to read or write, and it should fall over nicely :-)我遵循 @slacy 创建函数 popen2 的想法。
但发现问题是,当子进程死亡时,仍然读取文件描述符
outfp
的父进程不会从函数返回。这可以通过在父进程上添加关闭未使用管道来解决。
我们可以使用 bash 作为 shell 来获取新进程的正确 pid。
当子进程死亡时,函数
popen2
的调用者应该通过调用pclose2
来收集子进程的状态。 如果我们不收集该状态,子进程在终止时可能是僵尸进程。这是经过测试并按预期工作的完整代码。
I have follow idea of @slacy that create function popen2.
But found problem when child process is die, parent process that still read file descripter
outfp
not return from function.That could fix by add close unuse pipe on parent process.
We can get correct pid of new process by using bash as shell.
When child process die the caller of function
popen2
should collect status of child process by callpclose2
. if we don't collect that status child process could be zombie process when it terminate.This is the full code that tested and work as expect.
最明显的方法是 system("pkill process_name");
显然,如果您有多个正在运行的进程实例,那么这是有问题的。
The obvious way is system("pkill process_name");
Clearly this is problematic if you have more than one instance of the process running.
我在 python 中做了类似的事情,你可以杀死一个子进程以及为此分叉的任何子进程。 实际上与 slacy 的解决方案类似。
http://www.pixelbeat.org/libs/subProcess.py
I did something similar in python, where you could kill a subprocess and any subprocesses forked for that. It's similar to slacy's solution actually.
http://www.pixelbeat.org/libs/subProcess.py
我只是将(bash)脚本放入第一行:
然后我读取 pid,并用它来执行以下操作:
杀死(pid,9)
I simply put in the (bash) script, in the first line:
then I read the pid, and use it to do a :
kill(pid,9)