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

Bengt Kleberg <>
Thu Sep 6 17:10:51 CEST 2012


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




More information about the erlang-questions mailing list