[erlang-questions] there is a blocking in the process evaluating the huge factorial

Mike Oxford <>
Thu Jun 2 20:54:43 CEST 2011


As a guess;  io:format sends the message to the group leader for output.
If your group leader were to change based on the spawn() call, you may have
your io:format message in the run queue behind the fac call.

More information may be found here...
http://www.trapexit.org/forum/viewtopic.php?p=43855

Again, just a guess. :)

G'luck!

-mox

On Thu, Jun 2, 2011 at 7:27 AM, systemio systemio <> wrote:

> Hi all,
>
> My Erlang is (Erlang R13B03 (erts-5.7.4) [smp:2:2] [rq:2]
> [async-threads:0]).
> WinXP SP1.
>
> I've found the strange behavior when i spawn the process to evaluate the
> huge
> factorial.
>
> Here is the factorial module:
> -module(test).
> -compile(export_all).
>
> fac(0) ->
> 1;
> fac(N) ->
> N * fac(N-1).
>
> Here is the Erlang Shell code:
> 1> Fac = fun() -> test:fac(100000) end.
> #Fun<erl_eval.20.67289768>
> 2> S1 = fun()-> spawn(Fac), timer:sleep(3000), io:format("Will I am", []),
> spawn(Fac) end.
> #Fun<erl_eval.20.67289768>
> 3> S2 = fun()-> spawn(Fac), io:format("Will I am", []), spawn(Fac) end.
> #Fun<erl_eval.20.67289768>
> 4> S1().
> Will I am<0.38.0>
> 5> S2().
> Will I am<0.42.0>
>
> In my PC the fun Fac evaluates about 11 sec.
>
> If i start S1() i will see the message "Will I am" after 14 sec and it is
> strange as i expect it after 3 seconds. After message appeares i type
> "i()."
> and hit enter. This command is blocked and result appeares after 11 sec
> until
> second process evaluates.
>
> if i start S2() i will see the message "Will i am" immediately but i will
> not
> be able to type anything in the shell and my typing will appear after 22
> sec.
>
> Also i've tested it with spawn(Node, ...) and the result is IAE same.
>
> Are these behaviors valid or do i do smth wrong?
>
> ~
> Alex
> _______________________________________________
> 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/20110602/c71928fa/attachment.html>


More information about the erlang-questions mailing list