gpt4 book ai didi

cocoa - 在 cocoa 应用程序中进行进程外工作吗?

转载 作者:行者123 更新时间:2023-12-03 17:00:00 24 4
gpt4 key购买 nike

我有一个 cocoa 应用程序,需要在第二个进程中完成一些工作(因为它可能会由于有缺陷的库而崩溃)。我想让我的项目尽可能简单,所以理想情况下我会使用与父进程相同的二进制文件,并仅使用命令行参数控制子进程。如果父进程可以获取新进程的标准输入和标准输出的句柄,以便它们可以进行通信(尽管我用 pipeline() 创建的东西也可以工作),那就太好了。以前有人解决过这个问题吗?你学到了什么?我有 Win32/Linux 背景,所以我不确定 Cocoa/OS X 是否有我应该使用的特殊功能。

最佳答案

I'd like to keep my project as simple as possible, so ideally I would use the same binary as the parent process and just control the child with command line parameters.

forkexec 在 Mac OS X 上的工作方式与在 Linux 和其他 POSIX 环境上的工作方式相同,但有一个问题:在 Cocoa 应用程序中,您不能只是 fork 而不是 exec,因为 Core Foundation 不允许您在新进程中使用任何基于 CF 或 Cocoa 的 API。如果您在 Cocoa 应用程序中fork,则必须立即exec

您可以通过在 argv< 的 [0] 中使用您自己的 argv[0]执行相同的二进制文件 您传递给 exec

有一个 Cocoa 版本的 fork+exec:创建一个 NSTask 并将其启动路径设置为您自己的 [[[NSProcessInfo processInfo] arguments] objectAtIndex :0]。由于上述原因,无法使用 NSTask 进行 forkexec

It would also be nice if the parent could get handles to the new process's stdin & stdout so that they can communicate (although something I create with pipe() would work too).

pipe 也可以按照您的预期工作。

如果您使用 NSTask,则 pipe 的 Cocoa 版本为 [NSPipe pipeline]

关于cocoa - 在 cocoa 应用程序中进行进程外工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3835247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com