Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  process will stack overflow after received many message?

Guest
Posted: Fri Sep 16, 2011 8:39 am Reply with quote
Guest
Hi,
I am a newbie of Erlang, here I have a question for receive
message in Erlang loop.
Like below Erlang code, when process receive one message, it will
invoke loop again,
then it can service like a server, receive message forever.
BUT is possible that process will stack overflow after receive too
many message? the process invoke loop again and again.

I saw there have many code write like this in <<Programming Erlang>> book.

loop() ->
receive
hello ->
io:format("hello\n"),
loop();
Other ->
io:format("I don't know what is this message, ~p is ~n" ,[Other]),
loop()
end.
_______________________________________________
erlang-questions mailing list
erlang-questions@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions
Post received from mailinglist
Guest
Posted: Fri Sep 16, 2011 8:44 am Reply with quote
Guest
On Fri, Sep 16, 2011 at 9:38 AM, Jovi Zhang <bookjovi@gmail.com (bookjovi@gmail.com)> wrote:
Quote:
Hi,
   I am a newbie of Erlang, here I have a question for receive
message in Erlang loop.
   Like below Erlang code, when process receive one message, it will
invoke loop again,
   then it can service like a server, receive message forever.
   BUT is possible that process will stack overflow after receive too
many message? the process invoke loop again and again.

   I saw there have many code write like this in <<Programming Erlang>> book.

loop() ->
   receive
       hello ->
           io:format("hello\n"),
           loop();
       Other ->
           io:format("I don't know what is this message, ~p is ~n" ,[Other]),
           loop()
   end.


No stack overflow, due to tail call optimisation. See also http://www.erlang.org/doc/reference_manual/functions.html#id74170

Robby
 




Post received from mailinglist
Guest
Posted: Fri Sep 16, 2011 8:57 am Reply with quote
Guest
On Fri, Sep 16, 2011 at 4:44 PM, Robert Raschke <rtrlists@googlemail.com> wrote:
>
> On Fri, Sep 16, 2011 at 9:38 AM, Jovi Zhang <bookjovi@gmail.com> wrote:
>>
>> Hi,
>>    I am a newbie of Erlang, here I have a question for receive
>> message in Erlang loop.
>>    Like below Erlang code, when process receive one message, it will
>> invoke loop again,
>>    then it can service like a server, receive message forever.
>>    BUT is possible that process will stack overflow after receive too
>> many message? the process invoke loop again and again.
>>
>>    I saw there have many code write like this in <<Programming Erlang>>
>> book.
>>
>> loop() ->
>>    receive
>>        hello ->
>>            io:format("hello\n"),
>>            loop();
>>        Other ->
>>            io:format("I don't know what is this message, ~p is ~n"
>> ,[Other]),
>>            loop()
>>    end.
>>
>
> No stack overflow, due to tail call optimisation. See also
> http://www.erlang.org/doc/reference_manual/functions.html#id74170
>
> Robby
>

Thanks, but when I test it using escript, the result is like below,
"loop end" printed two times, it means it stacked!

[root@localhost Erlang]# cat jovi.erl
#!/usr/bin/env escript

loop() ->
receive
one ->
io:format("one\n"),
loop();
two ->
io:format("two\n")
end,
io:format("loop end\n").


main(_) ->
Pid = spawn(fun() -> loop() end),
Pid ! one,
Pid ! two,
receive
_Any -> void
end.


[root@localhost Erlang]# ./jovi.erl
one
two
loop end
loop end
^C
_______________________________________________
erlang-questions mailing list
erlang-questions@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Post received from mailinglist
Guest
Posted: Fri Sep 16, 2011 9:05 am Reply with quote
Guest
Your last call to io:format/2 is making the function non tail recursive.

/H
Guest
Posted: Fri Sep 16, 2011 9:06 am Reply with quote
Guest
You might find this link useful as well
Guest
Posted: Fri Sep 16, 2011 9:45 am Reply with quote
Guest
On Fri, Sep 16, 2011 at 5:06 PM, Steve Strong <steve@srstrong.com> wrote:
> You might find this link useful as
> well http://en.wikipedia.org/wiki/Tail_call - the reason that your new
> example is stacking is that having code after the recursion means that it's
> no longer tail-recursive, and will indeed consume stack space.
> --
> Steve Strong
> @srstrong
> Sent with Sparrow
>
> On Friday, 16 September 2011 at 10:57, Jovi Zhang wrote:
>
> On Fri, Sep 16, 2011 at 4:44 PM, Robert Raschke <rtrlists@googlemail.com>
> wrote:
>
> On Fri, Sep 16, 2011 at 9:38 AM, Jovi Zhang <bookjovi@gmail.com> wrote:
>
> Hi,
>    I am a newbie of Erlang, here I have a question for receive
> message in Erlang loop.
>    Like below Erlang code, when process receive one message, it will
> invoke loop again,
>    then it can service like a server, receive message forever.
>    BUT is possible that process will stack overflow after receive too
> many message? the process invoke loop again and again.
>
>    I saw there have many code write like this in <<Programming Erlang>>
> book.
>
> loop() ->
>    receive
>        hello ->
>            io:format("hello\n"),
>            loop();
>        Other ->
>            io:format("I don't know what is this message, ~p is ~n"
> ,[Other]),
>            loop()
>    end.
>
> No stack overflow, due to tail call optimisation. See also
> http://www.erlang.org/doc/reference_manual/functions.html#id74170
>
> Robby
>
> Thanks, but when I test it using escript, the result is like below,
> "loop end" printed two times, it means it stacked!
>
> [root@localhost Erlang]# cat jovi.erl
> #!/usr/bin/env escript
>
> loop() ->
> receive
> one ->
> io:format("one\n"),
> loop();
> two ->
> io:format("two\n")
> end,
> io:format("loop end\n").
>
>
> main(_) ->
> Pid = spawn(fun() -> loop() end),
> Pid ! one,
> Pid ! two,
> receive
> _Any -> void
> end.
>
>
> [root@localhost Erlang]# ./jovi.erl
> one
> two
> loop end
> loop end
> ^C
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions
>
>

Hmm, Thanks very much, I understand it now!

.jovi
_______________________________________________
erlang-questions mailing list
erlang-questions@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Post received from mailinglist
Guest
Posted: Fri Sep 16, 2011 3:31 pm Reply with quote
Guest
On Fri, Sep 16, 2011 at 11:45, Jovi Zhang <bookjovi@gmail.com> wrote:
>
> Hmm, Thanks very much, I understand it now!
>

Let me add that the perhaps most subtle way to make a function
stacking is to put an exception handler on the stack. Beware of that
case if you wonder why something is stacking when it should not be. It
only happens if the exception handler is pushed by each recursive call
though.

--
J.
_______________________________________________
erlang-questions mailing list
erlang-questions@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions
Post received from mailinglist

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum