Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Failing Erlang monitor
Erlang R15B (erts-5.9) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9 (abort with ^G)
1> c(doubler).
{ok,doubler}
2> c(monitor).
{ok,monitor}
3>
3> monitor:start().
new
4> doubler:double(doubler, 2).
4
5> doubler:double(doubler, "").
<0.44.0> exited: {badarith,[{doubler,loop,0,[{file,"doubler.erl"},{line,7}]}]}.
=ERROR REPORT==== 29-Apr-2012::20:22:52 ===
Error in process <0.44.0> with exit value: {badarith,[{doubler,loop,0,[{file,"doubler.erl"},{line,7}]}]}
...and there it stops, and I have to kill the console. The monitor seems to be restarting it OK, but I'm obviously doing something wrong.
-module(doubler).
-export([loop/0, double/2]).
loop() ->
receive
{Pid, N} ->
Pid ! (N * 2),
loop()
end.
double(To, N) ->
To ! {self(), N},
receive
Result -> Result
end.
-module(monitor).
-export([loop/0, start/0]).
loop() ->
process_flag(trap_exit, true),
receive
new ->
register(doubler, spawn_link(fun doubler:loop/0)),
loop();
{'EXIT', From, Reason} ->
io:format("~p exited: ~p.", [From, Reason]),
monitor ! new,
loop()
end.
start() ->
register(monitor, spawn(fun monitor:loop/0)),
monitor ! new.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment