Mac OS X:从 Cocoa 应用程序中终止/退出整个进程树的最快方法
我知道对此有很多问题和答案,但我正在寻找一种高效且强大的解决方案。 我需要从 Cocoa 应用程序中终止一个进程及其所有子进程。 我得到了进程 ID,我将要编写的代码是
kill -- -<parent PID>
从我的应用程序中执行这样的终止命令......但这对我来说似乎非常hacky和残酷。 难道就没有更好的解决办法吗? Carbon 的 KillProcess() 及其 Process Manager 朋友似乎没有多大帮助,除非我自己构建一个进程树表示。 我错过了什么吗?
我还有一些基于 PID 发送退出 Apple 事件的代码。 如果能够将其自下而上地发送到父进程定义的树中的每个进程,那就更好了。 但这只是锦上添花。 第一个问题的答案得到了“要点”。
I know there are many questions and answers about this, but I am looking for an efficient and robust solution. I need to kill a process AND all it's child processes from within a Cocoa app. I got the process ID and what I am about to code is to execute the kill command like so
kill -- -<parent PID>
from within my app ... but that seems awfully hacky and brutal to me. Isn't there a better solution? Carbon's KillProcess()and its Process Manager friends don't seem much help unless I build a process tree representation myself. Am I missing something?
I also have some code to send the Quit Apple Event based on PID. It would be even nicer to be able to send that to each process in the tree defined by a parent process, bottom up. But that's only a nice-to-have. An answer to the first question gets the "point".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用killpg来终止进程及其组中的所有内容:
当然,应该进行适当的错误检查,但您应该了解要点。 有关详细信息,请参阅手册页
kill(2)
和killpg(2)
。You can just use killpg to terminate the process and everything in its group:
Proper error checking should be done, of course, but you should get the gist. See the man pages
kill(2)
andkillpg(2)
for more info.上次我研究这个问题时(那是几年前的事了,但我认为没有太大变化),我发现的最佳解决方案就是调用系统终止命令。
The last time I looked into this (which was a few years ago, but I don't think much has changed) the best solution I found was just to call the system kill command.