<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div>Your ring loop becomes something like:<br><br></div><span style="font-family:monospace,monospace">ring(Next) -><br></span></div><span style="font-family:monospace,monospace"> receive<br></span></div><span style="font-family:monospace,monospace"> {_From,Msg} -><br></span></div><span style="font-family:monospace,monospace"> Next ! {self(),Msg},<br></span></div><span style="font-family:monospace,monospace"> loop(Next);<br></span></div><span style="font-family:monospace,monospace"> stop -><br></span></div><span style="font-family:monospace,monospace"> Next ! stop,<br></span></div><span style="font-family:monospace,monospace"> ok<br></span></div><span style="font-family:monospace,monospace"> end.<br></span><br></div>where we include the sender in the message (might be useful) and a <span style="font-family:monospace,monospace">stop</span> message which it sends to the next process and then dies. Starting the processes can be done in a number of ways, for example "backwards" around the ring or each process spawning its successor.<br><br></div>Robert<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 12 January 2015 at 19:25, Harit Himanshu <span dir="ltr"><<a href="mailto:harit.subscriptions@gmail.com" target="_blank">harit.subscriptions@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">That sounds like a good idea Robert, could you please guide me how to achieve this?<div><br></div><div>Thanks</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 12, 2015 at 10:00 AM, Robert Virding <span dir="ltr"><<a href="mailto:rvirding@gmail.com" target="_blank">rvirding@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>A much better way than sending the path around is for every process to keep track for itself the next process in the ring so you just need to send the message between the processes. At least if you are after speed. If you do do need to be able to dynamically restructure the ring it would be easy to add an extra message where you tell a process its new next process.<br><br></div>Robert<br><br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On 11 January 2015 at 20:48, Harit Himanshu <span dir="ltr"><<a href="mailto:harit.subscriptions@gmail.com" target="_blank">harit.subscriptions@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">Hello there,<div><br></div><div>I need help with code review on my attempt to following problem</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> Write a ring benchmark. Create </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">N</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> processes in a ring. Send a message round the ring </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">M</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> times so that a total of </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">N * M </code><a name="14adf64d36a5a9d1_14adf4ddf9a5f55a_14ada8afd1ff49a4_of" style="color:rgb(233,131,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"></a><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px">messages get sent. Time how long this takes for different values of </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">N</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px"> and </span><code style="font-size:14px;color:rgb(0,0,0);line-height:23.3999996185303px">M</code><span style="color:rgb(0,0,0);font-family:verdana,sans-serif;font-size:18px;line-height:23.3999996185303px">.</span><br></div><div><br></div><div>I have not timed this yet, and guidance on recommended ways to time is very much appreciated.</div><div><br></div><div>For now, following is the code for ring that I wrote. Please let me know if there are any shortcomings or code could be written in an much idiomatic way.</div><div><br></div><div>Thanks a lot</div><div>+ Harit</div><div><br></div><div><u><b>Code</b></u></div><div><div><br></div><div>-module(ring).</div><div>-author("harith").</div><div><br></div><div>%% API</div><div>-export([message/2]).</div><div><br></div><div>% create ring of N processes and</div><div>% send M messages between them</div><div>message(N, M) when is_integer(N), is_integer(M), N > 0, M > 0 -></div><div> Ring = create_ring(N),</div><div> [Start | T] = Ring,</div><div> Start ! {T, Ring, 1, M}.</div><div><br></div><div>create_ring(N) -></div><div> Processes = [spawn(fun() -> loop() end) || _ <- lists:seq(1, N)],</div><div> [H | _] = Processes,</div><div> lists:append(Processes, [H]).</div><div><br></div><div>loop() -></div><div> receive</div><div> {[H | T], _L, CurrentMessage, M} -></div><div> io:format("~p received ~p~n", [self(), CurrentMessage]),</div><div> H ! {T, _L, CurrentMessage, M},</div><div> loop();</div><div> {[], Ring, CurrentMessage, M} -></div><div> io:format("~p received ~p with empty list~n", [self(), CurrentMessage]),</div><div> case CurrentMessage < M of</div><div> true -></div><div> [_ | [Next | T]] = Ring,</div><div> NewMessage = CurrentMessage + 1,</div><div> io:format("sending message ~p to ~p~n", [NewMessage, Next]),</div><div> Next ! {T, Ring, NewMessage, M};</div><div> false -> io:format("done sending ~p messages in ~p ring, taking rest now.~n",[M, Ring])</div><div> end,</div><div> loop()</div><div> end.</div></div><div><br></div><div><b><u>Output</u></b><br></div><div><div>1> ring:message(4, 3).</div><div><0.33.0> received 1</div><div>{[<0.34.0>,<0.35.0>,<0.36.0>,<0.33.0>],</div><div> [<0.33.0>,<0.34.0>,<0.35.0>,<0.36.0>,<0.33.0>],</div><div> 1,3}</div><div><0.34.0> received 1</div><div><0.35.0> received 1</div><div>2> <0.36.0> received 1</div><div>2> <0.33.0> received 1 with empty list</div><div>2> sending message 2 to <0.34.0></div><div>2> <0.34.0> received 2</div><div>2> <0.35.0> received 2</div><div>2> <0.36.0> received 2</div><div>2> <0.33.0> received 2 with empty list</div><div>2> sending message 3 to <0.34.0></div><div>2> <0.34.0> received 3</div><div>2> <0.35.0> received 3</div><div>2> <0.36.0> received 3</div><div>2> <0.33.0> received 3 with empty list</div><div>2> done sending 3 messages in [<0.33.0>,<0.34.0>,<0.35.0>,<0.36.0>,<0.33.0>] ring, taking rest now.</div><div>2> </div></div></div>
<br></div></div>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>