| Author |
Message |
< Erlang ~ Create N processes in a ring. |
| homayara |
Posted: Thu Sep 11, 2008 7:16 am |
|
|
|
Joined: 10 Sep 2008
Posts: 1
|
Hi all,
I am having trouble to solve this following problem, please give me some ideas.
Create N processes in a ring. Send a message round the ring M times. How long does it take for the message to go around the ring M times? |
|
|
| Back to top |
|
| wolfgke |
Posted: Fri Sep 12, 2008 5:38 pm |
|
|
|
User
Joined: 16 Sep 2007
Posts: 16
|
I already solved this problem some time ago (but lost my source code ). I don't know whether my solution is good - but it worked.
I'll try to remember my ideas:
First we create a list of N process PIDs. This should be rather obvious.
The next step is to send to each process in this list the PID of the next process in the list. The last process PD in the list gets the PID of the first element of the list. Each process stores this in its internal state.
Next you send one process (probably the first) in the list a signal to send M times. The process than sends to the next process a signal "forward", which causes it to send forward to the next process.
When the first process receives the forward signal it sends a next forward - as many times as M says.
When this process receives "forward" the last time it sends an "ok" back to the process which forced it to send the token M times. |
|
|
| Back to top |
|
| dsmith |
Posted: Fri Sep 12, 2008 6:48 pm |
|
|
|
User
Joined: 08 Aug 2007
Posts: 41
Location: Toronto
|
|
| Back to top |
|
| Mazen |
Posted: Tue Sep 16, 2008 7:30 am |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
Posting the code I wrote a long time ago...
Hope it helps  |
|
|
| Back to top |
|
| francesco |
Posted: Sat Sep 27, 2008 7:01 pm |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
Oy! Does your teacher know you are getting online help with your homework
Francesco |
|
|
| Back to top |
|
| C0x |
Posted: Wed Oct 08, 2008 9:09 am |
|
|
|
Joined: 08 Oct 2008
Posts: 1
Location: Munich
|
Hi!
I have another problem with this task. I solved it but i don't think that my solution is nice and correct in terms of Erlang. Please, review my solution and give some advices on how to make it better. Thanks.
Code:
-module(ex2).
-export([start/2, proc/4]).
%%% helper function for add L to the end of the given list
append_list([H|T], L) -> [H|append_list(T, L)];
append_list([], L) -> [L].
%%% starts task N - number of processes, M - number of times message should fly through the ring
start(N, M) ->
Pid = spawn(ex2, proc, [create, N - 1, [], nothing]),
Pid ! {resend, hello, M*N},
done.
%%% creates last N-th process
proc(create, 0, [H|_], _) ->
Next = H,
proc(waitmsg, 0, [], Next);
%%% creates 1,...,N-1 processes
proc(create, N, T, _) when N > 0 ->
Next = spawn(ex2, proc, [create, N-1, append_list(T, self()), nothing]),
proc(waitmsg, N, [], Next);
%%% wait messages for N-th process
proc(waitmsg, 0, _, Next) ->
receive
{resend, Mess, Counter} ->
io:format("accepted ~w:~p~n", [self(), Mess]),
Next ! {resend, Mess, Counter-1},
proc(waitmsg, 0, [], Next);
stop ->
io:format("finished: ~w~n", [self()])
end;
%%% wait messages for 1, ..., N-1 process
proc(waitmsg, N, _, Next) ->
receive
%%% stop resending and terminate processes ring
{resend, _, 0} ->
self() ! stop,
proc(waitmsg, N, [], Next);
%%% resend message to the next process
{resend, Mess, Counter} ->
io:format("accepted ~w:~p~n", [self(), Mess]),
Next ! {resend, Mess, Counter-1},
proc(waitmsg, N, [], Next);
stop ->
Next ! stop,
io:format("finished: ~w~n", [self()])
end.
|
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|