Created
February 10, 2011 06:45
-
-
Save xslogic/820056 to your computer and use it in GitHub Desktop.
Ring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -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