Skip to content

Instantly share code, notes, and snippets.

@xslogic
Created February 10, 2011 06:45
Show Gist options
  • Select an option

  • Save xslogic/820056 to your computer and use it in GitHub Desktop.

Select an option

Save xslogic/820056 to your computer and use it in GitHub Desktop.
Ring
-module(ringu).
-export([start/2]).
start(N, M) ->
Pid_0 = self(),
statistics(runtime),
statistics(wall_clock),
Pid = spawn(fun() -> ringu(N, M, self(), Pid_0) end),
Pid ! x,
receive
_ -> true
end,
{_, TCPU} = statistics(runtime),
{_, Time} = statistics(wall_clock),
io:format("~nElapsed time: ~w ms~nCPU time: ~w ms ~n" ,
[Time, TCPU]).
ringu(1, M, Pid_1, Pid_0) -> ringu_loop(M, Pid_1), Pid_0 ! itsover;
ringu(N, M, Pid_1, Pid_0) ->
ringu_loop(M, spawn(fun() -> ringu(N-1, M, Pid_1, Pid_0) end)).
ringu_loop(1, Pid) -> ringu_fwd_msg(Pid);
ringu_loop(M, Pid) -> ringu_fwd_msg(Pid), ringu_loop(M-1, Pid).
ringu_fwd_msg(Pid) ->
receive
X -> Pid ! X %, io:format(".", [])
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment