Skip to content

Instantly share code, notes, and snippets.

@mokevnin
Created January 20, 2013 10:29
Show Gist options
  • Select an option

  • Save mokevnin/4577762 to your computer and use it in GitHub Desktop.

Select an option

Save mokevnin/4577762 to your computer and use it in GitHub Desktop.
-module(ring).
-export([start/3, proc/1, master_proc/1]).
start(M, ProcessCount, Message) ->
Pid = spawn_link(ring, master_proc, [ProcessCount]),
Pid ! {M, Message},
ok.
master_proc(ProcessCount) ->
Pid = spawn_link(ring, proc, [self()]),
master_proc(ProcessCount - 1, Pid).
master_proc(1, ChildPid) ->
proc(ChildPid);
master_proc(ProcessCount, ChildPid) ->
Pid = spawn_link(ring, proc, [ChildPid]),
master_proc(ProcessCount - 1, Pid).
proc(ChildPid) ->
receive
stop ->
ChildPid ! stop,
io:format("die ~w ~n", [self()]);
{0, _} -> ChildPid ! stop,
io:format("first die ~w ~n", [self()]);
{M, Message} ->
io:format("~w ~w ~n", [Message, self()]),
ChildPid ! {M - 1, Message},
proc(ChildPid)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment