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

dmitry kolesnikov dmkolesnikov@REDACTED
Fri Sep 7 05:30:44 CEST 2012


Hello,

At general level this is
f(),
spawn(fun g/0),
g()

The major concern here is supervision of forked processes, forking of
gen_server and making OTP compliment application.

Best Regards,
Dmitry >-|-|-*>


On 7.9.2012, at 4.31, Xiao Jia <stfairy@REDACTED> wrote:

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 <bengt.kleberg@REDACTED>
>
> 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
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120907/3eb83062/attachment.htm>


More information about the erlang-questions mailing list