Skip to content

Instantly share code, notes, and snippets.

@stiiifff
Created October 21, 2012 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stiiifff/3928673 to your computer and use it in GitHub Desktop.
Save stiiifff/3928673 to your computer and use it in GitHub Desktop.
Erlang process ring (Erlang programming, Chapter 4, Ex 4.2)
-module (ring).
-export ([start/3, first_proc/3, first_loop/4, proc_start/3, proc_loop/2]).
start(MsgCount, ProcCount, Msg) ->
spawn(ring, first_proc, [MsgCount, ProcCount, Msg]).
first_proc(MsgCount, ProcCount, Msg) ->
io:format("First process ~w created, setting up the rest of the ring ...~n", [self()]),
NextPid = spawn(ring, proc_start, [self(), self(), ProcCount-1]),
LastPid = receive
{Pid, ready} -> Pid
end,
io:format("Received ready message from last process ~w, about to send messages around the ring ...~n", [LastPid]),
first_loop(NextPid, LastPid, MsgCount, Msg).
first_loop(NextPid, LastPid, MsgCount, Msg) ->
case MsgCount of
MsgCount when MsgCount > 0 ->
NextPid ! {self(), Msg},
receive
{LastPid, Msg} ->
first_loop(NextPid, LastPid, MsgCount-1, Msg)
end;
0 ->
NextPid ! {self(), stop},
receive
{LastPid, stop} -> ok
end
end.
proc_start(FirstPid, PrevPid, ProcCount) when ProcCount > 0 ->
NextPid = spawn(ring, proc_start, [FirstPid, self(), ProcCount-1]),
io:format("Created process ~w, ~w processes to go.~n", [NextPid, ProcCount]),
proc_loop(PrevPid, NextPid);
proc_start(FirstPid, PrevPid, 0) ->
io:format("Last process ~w reached, linking back to first process ~w.~n", [self(), FirstPid]),
FirstPid ! {self(), ready},
proc_loop(PrevPid, FirstPid).
proc_loop(PrevPid, NextPid) ->
receive
{PrevPid, Msg} ->
NextPid ! {self(), Msg},
io:format("Forwarded msg ~w to process ~w.~n", [Msg, NextPid]),
proc_loop(PrevPid, NextPid);
{PrevPid, stop} ->
NextPid ! {self(), stop},
ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment