我查看了 posix-spawn( https://github.com/rtomayko/posix-spawn ) 的代码,它似乎在低级实现中使用了 vfork。但是,不鼓励在 linux 平台上使用 vfork。posix-spawn 可以吗模块 ?
最佳答案
不推荐使用
vfork
,因为它严重限制了新进程的功能(直到它 exec
),因此很难正确使用。在 Linux 上,fork
使用写时复制语义,因此 fork
将相当快,而无需使用 vfork
。此外,vfork
信号语义在所有平台上都不是标准的。来自 man vfork
:
It is rather unfortunate that Linux revived this specter from the past. The BSD man page states:
This system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of
vfork
() as it will, in that case, be made synonymous tofork(2)
.信号处理的细节是模糊的,并且在系统之间有所不同。 BSD 手册页指出:
To avoid a possible deadlock situation, processes that are children in the middle of a
vfork()
are never sentSIGTTOU
orSIGTTIN
signals; rather, output orioctl
s are allowed and input attempts result in an end-of-file indication.
也就是说,如果使用得当,vfork
会很有用,而且它在许多其他平台上会更快。 Linux 确实支持它,所以只要代码不做任何受 vfork
限制的事情,它在 Linux 上应该没问题。
关于ruby - 为什么 posix-spawn 强制在 linux 中使用 vfork?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994510/