process - 如何在 Erlang 中 fork/clone 一个进程

标签 process erlang fork

如何在 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/

相关文章:

multithreading - 杀死从特权用户到根用户的信号

java - java中在进程中创建线程

concurrency - 如何发送消息以在 YAWS/Erlang 中接收

rest - 牛仔邮政处理程序

c - 我如何一次最多 fork 父进程的 5 个子进程?

process - 从锁定互斥锁的进程退出会导致死锁吗?

erlang - 如何在 Erlang 中替换双引号

c++ - Linux:来自内存密集型进程的 system() 无需 fork

c++ - 使用 fork() C++ 操作共享内存中的字符串

memory-management - 差异交换和分页