-module(pingpong). -export([start/1, ppR/1, ppS/2]). % A program that starts two processes and then sends a message back and % forth between them N times. Note that ppR and ppS also have to be % exported because they are used in the spawn calls. start(N) -> B = spawn(pingpong, ppR, [N]), A = spawn(pingpong, ppS, [N, B]), io:format("Started A:~w, B:~w~n", [A, B]). % the sender process ppS(0, _) -> true; ppS(N, B) -> pp_send({B, N}), pp_receive(), ppS(N-1, B). % the receiver process ppR(0) -> true; ppR(N) -> Msg = pp_receive(), pp_send(Msg), ppR(N - 1). % send a message pp_send({Pid, N}) -> Pid ! {self(), N}. % receive a message and return it pp_receive() -> receive {From, Msg} -> io:format("~w Got message ~w from ~w~n", [self(), Msg, From]) end, {From, Msg}.