如何在 Erlang 中 fork/clone 进程,就像 Unix 中的 fork
一样?
我搜索了很多,但一无所获。
也许用法是这样的:
case fork() of
{parent, Pid} ->
in_parent_process_now();
{child, Pid} ->
in_child_process_now();
{error, Msg} ->
report_fork_error(Msg)
end.
有什么想法吗?
编辑:
为了更好的说明我的观点,以下面的C代码为例:
f();
fork();
g();
这里忽略了fork()
的返回值,所以父进程和子进程接下来的步骤是一样的,就是执行g()
.
我可以在 Erlang 中实现吗?
最佳答案
(这个问题是 also answered in the erlang-questions mailing list 。)
Erlang 没有“fork”操作。但是它有一个 spawn
操作:
parent_process() ->
will_be_executed_by_parent_process(),
spawn(fun() -> will_be_executed_by_child_process() end),
will_also_be_executed_by_parent_process().
... 其中函数名称显示它们将在什么上下文中执行。请注意,传递给子进程的任何数据都将复制到新进程的堆中。
关于process - 如何在 Erlang 中 fork/clone 一个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12302583/