[erlang-questions] code sample request
Mazen Harake
mazen.harake@REDACTED
Tue Jan 19 10:46:09 CET 2010
Interesting, I did something similar a while back.
Check this out:
http://www.trapexit.org/index.php?title=Process_Ring_Across_Nodes
/Mazen
On 18/01/2010 20:00, 黃耀賢 (Yau-Hsien Huang) wrote:
> And my solution:
>
> *** The Code
>
> -export([round_nodes/3]).
>
> round_nodes(M, N, Msg) ->
> Pids = get_nodes(M, [N, Msg]),
> [P|Ps] = Pids,
> Pids1 = lists:append(Ps, [P]),
> Pids2 = lists:zip(Pids, Pids1),
> [ S ! T || {S, T}<- Pids2 ].
>
> get_nodes(0, _Args) ->
> [];
> get_nodes(N, [Times, Msg]) ->
> P = spawn(test, node, [Times, Msg]),
> Ps = get_nodes(N-1, [Times, Msg]),
> [P|Ps].
>
> node(N, Msg) ->
> Pid = target_node(),
> traffic(N, self(), Pid, Msg).
>
> target_node() ->
> receive
> Pid -> Pid
> end.
>
> traffic(0, _Pid1, _Pid2, _Msg) ->
> true;
> traffic(N, Pid1, Pid2, Msg) ->
> Pid2 ! {Pid1, Msg},
> receive
> {Pid, Msg1} when Pid /= Pid2 ->
> Pid ! {Pid1, {Msg1, ok}}
> end,
> receive
> {Pid2, Response} ->
> error_logger:info_msg("~w reveived \"~w\" from ~w.~n",
> [self(), Response, Pid2])
> end,
> traffic(N-1, Pid1, Pid2, Msg).
>
> *** Execution
>
>
>> test:round_nodes(3, 2, "HELLO").
>>
> =INFO REPORT==== 19-Jan-2010::01:58:54 ===
> <0.35.0> reveived "{[72,69,76,76,79],ok}" from<0.36.0>.
>
> =INFO REPORT==== 19-Jan-2010::01:58:54 ===
> <0.36.0> reveived "{[72,69,76,76,79],ok}" from<0.37.0>.
> [<0.36.0>,<0.37.0>,<0.35.0>]
>
> =INFO REPORT==== 19-Jan-2010::01:58:54 ===
> <0.37.0> reveived "{[72,69,76,76,79],ok}" from<0.35.0>.
>
> =INFO REPORT==== 19-Jan-2010::01:58:54 ===
> <0.35.0> reveived "{[72,69,76,76,79],ok}" from<0.36.0>.
> 3>
> =INFO REPORT==== 19-Jan-2010::01:58:54 ===
> <0.36.0> reveived "{[72,69,76,76,79],ok}" from<0.37.0>.
>
> =INFO REPORT==== 19-Jan-2010::01:58:54 ===
> <0.37.0> reveived "{[72,69,76,76,79],ok}" from<0.35.0>.
>
>
> On Tue, Jan 19, 2010 at 12:45 AM, Wallentin Dahlberg<
> wallentin.dahlberg@REDACTED> wrote:
>
>
>> Another one,
>>
>> -module(ring).
>> -export([run/2, run/3]).
>>
>> run(N, M) -> run(N, M, msg).
>> run(N, M, Msg) -> run(N - 1, M, Msg, self()).
>>
>> run(0, 0, _, _) -> ok;
>> run(0, M, Msg, Pid) ->
>> Pid ! {self(), Msg},
>> receive {_From, Msg} -> run(0, M - 1, Msg, Pid) end;
>> run(N, M, Msg, Pid) -> run(N - 1, M, Msg, spawn_link(fun() -> loop(Pid)
>> end)).
>>
>> loop(To) ->
>> receive
>> {From, Msg} ->
>> io:format("~p -> (Me:~p) -> ~p~n", [From, self(), To]),
>> To ! {self(), Msg},
>> loop(To)
>> end.
>>
>>
>> Beware of typos and other confusing errors. =)
>>
>> // egil
>>
>> 2010/1/18 Fred Hebert<mononcqc@REDACTED>
>>
>>
>>> On Mon, Jan 18, 2010 at 10:26 AM, Ish Rattan<ishwar@REDACTED
>>>
>>>> wrote:
>>>>
>>>
>>>> I am trying to write the code for
>>>> "Create N processes in a ring. Send a message
>>>> around the ring M times so that a total of N*M
>>>> messages get sent." problem from Armstrong's book.
>>>>
>>>> A solution code wiil be appreciated.
>>>>
>>>> -ishwar
>>>>
>>>>
>>>> ________________________________________________________________
>>>> erlang-questions mailing list. See http://www.erlang.org/faq.html
>>>> erlang-questions (at) erlang.org
>>>>
>>>> See
>>>>
>>>
>>>
>> http://shootout.alioth.debian.org/u64/benchmark.php?test=threadring&lang=hipe&id=1for
>>
>>> a common efficient implementation.
>>>
>>>
>>
>
More information about the erlang-questions
mailing list