c++ - 为什么在mac中禁止使用fork without exec?

标签 c++ c macos fork exec

我的问题很简单。 在 Linux 上,使用不带 exec 的 fork 非常流行

但是,我发现在 MacOS 上这是不可能的(参见 fork 手册)

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/fork.2.html

There are limits to what you can do in the child process. To be totally safe you should restrict your-self yourself 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.

我觉得这很奇怪?是什么原因?是否可以解决它?

最佳答案

在 OS X 中使用 fork 是可以的,在与在 Linux 中使用 fork 相同的限制下。 Linux has similar caveatsvia the Wayback Machine .

如果您构建的应用程序是单线程的并且依赖于核心 UNIX API 和设计理念,您应该没问题。如果您要链接到其他库,您应该非常熟悉它们的行为。想象一下链接到一个启动后台线程的库——在 fork 之后你可能处于未定义的状态,因为只有调用 fork 的线程被克隆。

OS X 提供了一些很棒的功能来利用多核,例如 Grand Central Dispatch这可能值得考虑。

我建议您阅读 this article作者 Mike Ash 关于 OS X 下的 fork 安全。

关于c++ - 为什么在mac中禁止使用fork without exec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854708/

相关文章:

c - strtok() C 字符串到数组

c++ - 在方法上调用方法 C++

c++ - libQt5Core.so 需要的 libicuuc.so.48,未找到(尝试使用 -rpath 或 -rpath-link)

c - Linux 中的终止和信号问题

c - fscanf() 将什么放入 char 指针?

macos - Mac OS X Lion、滚动条和网站可用性

c++ - 四舍五入为间隔不均匀的整数

c++ - OpenGL glTranslatef 无法正常工作

python - xcode像windows一样创建命令行窗口

multithreading - 在稀缺的系统资源下测试 OSX 应用程序