fork在Cocoa应用程序中 [英] fork within Cocoa application

查看:195
本文介绍了fork在Cocoa应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is not the best scenario for fork(). However, this is the best func I can get.

我在Mac OSX上使用Firefox插件。为了使其健壮,我需要创建一个新的过程来运行我的插件。问题是,当我分叉一个新的过程,很像这样:

I am working on a Firefox plugin on Mac OSX. To make it robust, I need to create a new process to run my plugin. The problem is, when I forked a new process, much like this:

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

然而,由于状态未清除,我无法正确初始化我的新进程(调用NSApplicationLoad等)。有任何想法吗? BTW,我当然不想创建一个新的二进制和执行它。

However, since the state is not cleaned, I cannot properly initialized my new process (call NSApplicationLoad etc.). Any ideas? BTW, I certainly don't want create a new binary and exec it.

推荐答案

一般来说,你需要<$ c Mac OS X上 fork()之后的$ c> exec()。

In general, you need to exec() after fork() on Mac OS X.

fork(2) 手册页:


子进程。为了完全安全,您应该将自己限制为仅执行异步信号安全操作,直到调用其中一个exec函数时为止。在fork()之后,任何框架或库中的所有API(包括全局数据符号)都应假定为不安全,除非明确记录为安全或异步信号安全。如果你需要在子进程中使用这些框架,你必须执行exec。在这种情况下,自我执行是合理的。

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 也对此主题发表了评论:

TN2083 also comments on this subject:


如果调用 fork 但不调用 exec ,许多Mac OS X框架不能可靠地工作。唯一的例外是系统框架,甚至在那里,POSIX标准对您在 fork exec之间可以做什么有严格的限制。 / code>

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.

重要:事实上,在Mac OS X 10.5及更高版本中,Core Foundation会检测到这种情况,警告消息如清单13所示。

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.

清单13: Core Foundation抱怨fork-without-exec

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

该进程已分叉,您不能安全地使用此CoreFoundation功能。你必须执行()。
打开 __ THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY ___ YOU_MUST_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.

这篇关于fork在Cocoa应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆