AtomVM
AtomVM copied to clipboard
again pingpong, send and whereis
executing
-module(pp).
-export([start/0, ping/1, pong/0]).
ping(0) ->
pong ! finished,
io:format("ping finished~n");
ping(N) ->
pong ! {ping, self()}, % this does not work
% whereis(pong) ! {ping, self()}, % this works
io:format("ping sent~n"),
receive
pong ->
io:format("Ping received pong~n")
end,
ping(N - 1).
pong() ->
receive
finished ->
io:format("Pong finished~n");
{ping, Ping_PID} ->
io:format("Pong received ping~n"),
Ping_PID ! pong,
pong()
end.
start() ->
register(pong, spawn(pp, pong, [])),
spawn(pp, ping, [3]),
receive X -> X end.
results in
Starting application...
ping sent
-- no more output--
replacing
pong ! {ping, self()}, % this does not work
with
whereis(pong) ! {ping, self()}, % this works
results in the expected output:
Starting application...
ping sent
Pong received ping
Ping received pong
ping sent
Pong received ping
Ping received pong
ping sent
Pong received ping
Ping received pong
ping finished
btw:
replacing io:format("..~n") by erlang:display("..)
results in
Starting application...
"ping sent""Pong received ping"
"Ping received pong"
"ping sent""Pong received ping"
"Ping received pong"
"ping sent""Pong received ping"
"Ping received pong"
"ping finished"
rgds, g.