Mac OS X:从 Cocoa 应用程序中终止/退出整个进程树的最快方法

发布于 2024-07-24 13:56:54 字数 407 浏览 1 评论 0原文

我知道对此有很多问题和答案,但我正在寻找一种高效且强大的解决方案。 我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-07-31 13:56:54

您可以使用killpg来终止进程及其组中的所有内容:

#include <signal.h>
#include <unistd.h>

/* ... */

killpg(getpgid(pid), SIGTERM);

当然,应该进行适当的错误检查,但您应该了解要点。 有关详细信息,请参阅手册页 kill(2)killpg(2)

You can just use killpg to terminate the process and everything in its group:

#include <signal.h>
#include <unistd.h>

/* ... */

killpg(getpgid(pid), SIGTERM);

Proper error checking should be done, of course, but you should get the gist. See the man pages kill(2) and killpg(2) for more info.

风渺 2024-07-31 13:56:54

上次我研究这个问题时(那是几年前的事了,但我认为没有太大变化),我发现的最佳解决方案就是调用系统终止命令。

system( "ps axwww | grep -i CoreServices/Dock.app/Contents/MacOS/Dock | grep -v grep | awk '{print $1}' | xargs kill -3" );

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.

system( "ps axwww | grep -i CoreServices/Dock.app/Contents/MacOS/Dock | grep -v grep | awk '{print $1}' | xargs kill -3" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文