[erlang-questions] Why do two processes appear after a spawn?

Ulf Wiger (TN/EAB) <>
Wed Nov 7 09:16:54 CET 2007


Dominic Williams wrote:
> Hello,
> 
> Can someone explain to me what is going on here?
> 
> Erlang (BEAM) emulator version 5.5.4 [source] [async-threads:0]  
> [hipe] [kernel-poll:false]
> 
> Eshell V5.5.4  (abort with ^G)
> 1> F = fun() -> P=processes(), {length(P), lists:member(self(),P)} end.
> #Fun<erl_eval.20.112921583>
> 2> G = fun() -> P=F(), timer:sleep(500), Q=F(), io:fwrite("~p~n", 
> [{P,Q}]) end.
> #Fun<erl_eval.20.112921583>
> 3> F().
> {24,true}
> 4> spawn(G).
> <0.35.0>
> {{25,true},{26,true}}
> 
> So the newly spawned process is immediately listed by processes(),  
> but a bit later another one appears. What is this 26th process?

The shell evaluator spawns a process that parses the
command line. It returns the parsed expression using
exit(io:parse_erl_exprs(Prompt)).

So, on when hitting return after "spawn(G).", the current
parser process exits. Then, your requested spawn operation
is executed, which leads to the first call to processes().
While the new process sleeps, the shell has time to start
a new parser process to handle the next command, and it
appears in the second call to processes().

BR,
Ulf W



More information about the erlang-questions mailing list