Created
December 2, 2012 10:32
-
-
Save greggy/4188101 to your computer and use it in GitHub Desktop.
This file contains 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
start(ProcNum, MsgNum, Message) -> | |
init(#state{num_procs=ProcNum, msg_num=MsgNum, msg=Message}). | |
%% first process | |
init(#state{proc_num=Proc, msg=Message}=State) when Proc == 1 -> | |
Pid = spawn(ring, loop, [Proc]), | |
Pid ! Message, | |
init(State#state{proc_num=Proc+1, first_pid=Pid}); | |
%% last process | |
init(#state{proc_num=Proc, num_procs=ProcNum, msg=Message, first_pid=FirstPid}) when Proc == ProcNum -> | |
spawn(ring, loop, [Proc]), | |
FirstPid ! Message; | |
init(#state{proc_num=Proc, msg=Message}=State) -> | |
Pid = spawn(ring, loop, [Proc]), | |
Pid ! Message, | |
init(State#state{proc_num=Proc+1}). | |
%%%=================================================================== | |
%%% Internal functions | |
%%%=================================================================== | |
loop(Num) -> | |
receive | |
stop -> | |
stop; | |
Msg -> | |
print(Num, Msg), | |
loop(Num) | |
%% _ -> | |
%% {error, invalid_message} | |
end. | |
print(Num, Msg) -> | |
lists:flatten(io_lib:format("Process: ~p recieved: ~p~n", [Num, Msg])).start(ProcNum, MsgNum, Message) -> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment