cocoa - 在 Cocoa 应用程序中 fork

标签 cocoa macos fork

我的问题不是 fork() 的最佳场景。然而,这是我能得到的最好的功能。

我正在 Mac OSX 上开发 Firefox 插件。为了使其健壮,我需要创建一个新进程来运行我的插件。问题是,当我 fork 一个新进程时,就像这样:

if (fork() == 0) exit(other_main());

但是,由于状态未清理,我无法正确初始化我的新进程(调用 NSApplicationLoad 等)。有任何想法吗?顺便说一句,我当然不想创建一个新的二进制文件并执行它。

最佳答案

一般来说,在 Mac OS X 上,您需要在 fork() 之后执行 exec()

来自fork(2)手册页:

There are limits to what you can do in the child process. To be totally safe you should restrict your-self to only executing async-signal safe operations until such time as one of the exec functions is called. All APIs, including global data symbols, in any framework or library should be assumed to be unsafe after a fork() unless explicitly documented to be safe or async-signal safe. If you need to use these frameworks in the child process, you must exec. In this situation it is reasonable to exec yourself.

TN2083还有对此主题的评论:

Many Mac OS X frameworks do not work reliably if you call fork but do not call exec. The only exception is the System framework and, even there, the POSIX standard places severe constraints on what you can do between a fork and an exec.

IMPORTANT: In fact, in Mac OS X 10.5 and later, Core Foundation will detect this situation and print the warning message shown in Listing 13.

Listing 13: Core Foundation complaining about fork-without-exec

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec(). Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

关于cocoa - 在 Cocoa 应用程序中 fork ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2707572/

相关文章:

c - 使用 fork() 的 Linux 进程树

ruby - 启动一个进程并在 ruby​​ 脚本退出后保持其运行

ios - 方法【load】是否需要调用【super load】

c - 在 Mac 上编程 USB 设备

c++ - 在 OSX 上构建和使用 LLVM 3.8 的通行证

objective-c - 绘制变暗的 NSImage 的最简单方法(如禁用 View )

c - 如何让 child 等待 parent 返回(而不是退出)然后自杀?

css - Cocoa webview UserAgent "webkit-legacy"问题

objective-c - 如何使用NSScrollview?

ios - 无法删除我在 ios 中的文件