concurrency - Concurrent Erlang Code -
from old exam, there question don't understand answer to. module question looks this:
-module(p4). -export([start/0, init/0, f1/1, f2/1, f3/2]). start() -> spawn(fun() -> init() end). init() -> loop(0). loop(n) -> receive {f1, pid} -> pid ! {f1r, self(), n}, loop(n); {f2, pid} -> pid ! {f2r, self()}, loop(n+1); {f3, pid, m} -> pid ! {f3r, self()}, loop(m) end. f1(serv) -> serv ! {f1, self()}, receive {f1r, serv, n} -> n end. f2(serv) -> serv ! {f2, self()}, receive {f2r, serv} -> ok end. f3(serv, n) -> serv ! {f3, self(), n}, receive {f3r, serv} -> ok end.
the question asks consider following function part of code, , result of function be. correct answer 2. think it'd 3, since "increase-call" f2(server)
after response of self()!{f1r, server, 2}
.
test3() -> server = start(), self()!{f1r, server, 2}, f2(server), f1(server).
my questions are:
- why answer 2 , not 3, , how
self()!{f1r, server, 2}
work? - isn't
self()!{f1r, server, 2}
response receive clause inloop(n)
off1(serv)
function?
self()!{f1r, server, 2}
sends {f1r, server, 2}
itself. message wait in inbox until received.
then f2
gets executed , f1
.
it in last function, when last line receive {f1r, serv, n} -> n end
executed, process running test3
receives message waiting in inbox (the 1 sent itself) , returns 2 in message.
note @ end of program, there have been {f1r, <process number>, n}
waiting in inbox, n having value of 1.
Comments
Post a Comment