我不确定 Haskell 中的 forkIO
/forkOS
和 forkProcess
之间有什么区别。据我了解,forkIO
/forkOS
更像是线程(类似于 C 中的 pthread_create
),而 forkProcess
启动一个单独的进程(类似于fork
)。
最佳答案
forkIO
创建一个轻量级未绑定(bind)绿色线程。绿色线程的开销非常小; GHC 运行时能够在一小部分操作系统工作线程上高效地复用数百万个绿色线程。绿色线程在其生命周期内可能存在于多个操作系统线程上。
forkOS
创建一个绑定(bind)线程:一个绿色线程,保证 FFI 调用发生在单个固定操作系统线程上。绑定(bind)线程通常在与使用线程本地数据的 C 库交互时使用,并期望所有 API 调用都来自同一线程。来自 the paper指定 GHC 的绑定(bind)线程:
The idea is that each bound Haskell thread has a dedicated associated OS thread. It is guaranteed that any FFI calls made by a bound Haskell thread are made by its associated OS thread, although pure-Haskell execution can, of course, be carried out by any OS thread. A group of foreign calls can thus be guaranteed to be carried out by the same OS thread if they are all performed in a single bound Haskell thread.
[...]
[F]or each OS thread, there is at most a single bound Haskell thread.
请注意,上面的引用并不排除与绑定(bind)线程关联的操作系统线程可以充当未绑定(bind) Haskell 线程的工作线程的可能性。它也不保证绑定(bind)线程的非 FFI 代码将在任何特定线程上执行。
forkProcess
创建一个新进程,就像 UNIX 上的 fork
一样。
关于haskell - forkIO/forkOS 和 forkProcess 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41485126/