[erlang-questions] How to fork/clone a process in Erlang?
Magnus Henoch
magnus.henoch@REDACTED
Thu Sep 6 17:10:32 CEST 2012
Xiao Jia <stfairy@REDACTED> writes:
> How can I fork/clone a process in Erlang, just as the fork in Unix?
>
> I have searched a lot but just got nothing related to that. I suppose the
> usage may look like this:
>
> case fork() of
> {parent, Pid} -> in_parent_process_now();
> {child, Pid} -> in_child_process_now();
> end.
>
> Any ideas?
There is nothing called "fork" in Erlang, but you can achieve pretty
much the same effect using 'spawn'. For example, your example could be
written as:
ParentPid = self(),
ChildPid = spawn(fun() -> in_child_process_now(ParentPid) end),
in_parent_process_now(ChildPid).
That is, the child process starts executing in the function passed to
'spawn', and the parent process keeps executing after the call to
'spawn'.
Hope this helps,
Magnus
More information about the erlang-questions
mailing list