gpt4 book ai didi

cocoa - 在 Cocoa 应用程序中 fork

转载 作者:行者123 更新时间:2023-12-03 16:11:09 26 4
gpt4 key购买 nike

我的问题不是 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/

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