[erlang-questions] How to fork/clone a process in Erlang?

Xiao Jia <>
Fri Sep 7 03:31:32 CEST 2012


Well, this approach can solve much of the problem.
Do you know if there's any approach which can clone the control flow as
well as the states?

Of course in your example the control flow is cloned as you also loop in
the spawn fun.
But what I want is a more general case. Take the following C code as an
example.

f();
fork();
g();

Here the return value of fork() is ignored, so the next steps of both the
parent and the child
process are the same, which is to execute g(). Can I achieve this in Erlang?

2012/9/6 Bengt Kleberg <>
>
> Greetings,
>
> Suppose that you have a process running like this:
> loop( State ) ->
>         receive
>         normal ->
>                 New_state = new_state( State ),
>                 loop( New_state )
>         _ -> ok
>         end.
>
> You could add a forking like this:
> loop( State ) ->
>         receive
>         normal ->
>                 loop( State )
>         fork ->
>                 spawn( fun() -> loop(State) end ),
>                 loop( State )
>         _ -> ok
>         end.
>
>
> bengt
>
> On Thu, 2012-09-06 at 17:02 +0200, Xiao Jia wrote:
> > Hi,
> >
> >
> > 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?
> >
> >
> >
> >
> > Thanks,
> >
> >
> > Xiao Jia
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120907/b681135c/attachment.html>


More information about the erlang-questions mailing list