Guest User

Untitled

a guest
Jul 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. -module(ringu).
  2. -export([start/2]).
  3.  
  4. start(N, M) ->
  5. Pid_0 = self(),
  6. statistics(runtime),
  7. statistics(wall_clock),
  8. Pid = spawn(fun() -> ringu(N, M, self(), Pid_0) end),
  9. Pid ! x,
  10. receive
  11. _ -> true
  12. end,
  13. {_, TCPU} = statistics(runtime),
  14. {_, Time} = statistics(wall_clock),
  15. io:format("~nElapsed time: ~w ms~nCPU time: ~w ms ~n" ,
  16. [Time, TCPU]).
  17.  
  18. ringu(1, M, Pid_1, Pid_0) -> ringu_loop(M, Pid_1), Pid_0 ! itsover;
  19. ringu(N, M, Pid_1, Pid_0) ->
  20. ringu_loop(M, spawn(fun() -> ringu(N-1, M, Pid_1, Pid_0) end)).
  21.  
  22. ringu_loop(1, Pid) -> ringu_fwd_msg(Pid);
  23. ringu_loop(M, Pid) -> ringu_fwd_msg(Pid), ringu_loop(M-1, Pid).
  24.  
  25. ringu_fwd_msg(Pid) ->
  26. receive
  27. X -> Pid ! X %, io:format(".", [])
  28. end.
Add Comment
Please, Sign In to add comment