ruby - 为什么 posix-spawn 强制在 linux 中使用 vfork?

标签 ruby linux

我查看了 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 to fork(2).

信号处理的细节是模糊的,并且在系统之间有所不同。 BSD 手册页指出:

To avoid a possible deadlock situation, processes that are children in the middle of a vfork() are never sent SIGTTOU or SIGTTIN signals; rather, output or ioctls 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/

相关文章:

ruby - 为什么二进制文件在压缩时会损坏?

html - 包装 link_to、image_tag 和 div_tag 以生成此代码时出错

java - Java 进程的 'pathname' 的空 '/proc/smap' 条目是什么?

linux - 如何在 ucontext* ,linux 中释放堆栈?

php - 屏幕肯定在运行时找不到屏幕 session

linux - 使用 sudo su - team 并仅在此 sudo 访问下执行其余命令的 shell 脚本

ruby-on-rails - 如何使用 Mongoid 声明基于嵌入式模型属性的范围

ruby-on-rails - ruby 压缩包 : Unable to find path of file stored in Active Storage

swift - 使用 Cocoapods 时 Generamba 设置失败

linux - 运行 SSH session 并从客户端记录到文件 stdout,添加时间戳