0

假设我有 3 个进程,每个进程都已注册,以便我可以轻松地发送和接收消息。

如何创建一个循环运行,例如,10 次迭代并在进程 1 和 3 之间向前和向后发送消息,通过进程 2?意思是,我希望进程 1 向进程 2 发送一条消息,它应该将它发送到进程 3,反之亦然。

4

2 回答 2

4

这是一个小模块来做你正在寻找的东西。但是当我看到您正在寻找很多答案时,我猜您是 erlang 的新手。所以我认为你应该看书或在线书籍来开始你的学习曲线。我的首选是: http: //learnyousomeerlang.com/content 作者在帮助初学者开始使用 Erlang 方面做得很好。当你对 erlang 越来越熟悉时,官方文档也很有趣(尽管它对开始没有帮助!)。

-module(ping3).
-compile(export_all).

% Launch 3 loops, init phase
start() ->
    P1 = spawn(?MODULE,loop,[]),
    P2 = spawn(?MODULE,loop,[]),
    P3 = spawn(?MODULE,loop,[]),
    {P1,P2,P3}.

% user interface
start_ring(P1,P2,P3,Loop) ->
    P1 ! {start,P2,P3,Loop}.

% kind of server, the messages content the informations about what to do, and all needed parameters
% it implements your protocol
loop() ->
    receive
        {start,By,To,Loop} ->
            Ref = make_ref(),
            io:format("start ring for ~p iterations of ~p~n",[Loop,Ref]),
            By ! {go_via,Ref,self(),By,To,Loop},
            loop();
        {go_via,Ref,From,By,To,Loop} ->
            To ! {go,Ref,From,By,To,Loop},
            loop();
        {go,Ref,From,By,To,Loop} ->
            By ! {back_via,Ref,From,By,To,Loop},
            loop();
        {back_via,Ref,From,By,To,Loop} ->
            To ! {back,Ref,From,By,To,Loop},
            loop();
        {back,Ref,_,_,_,0} ->
            io:format("end of ring of ~p~n",[Ref]),
            loop();
        {back,Ref,From,By,To,Loop} ->
            io:format("continue ring for ~p iterations of ~p~n",[NewLoop=Loop-1,Ref]),
            By ! {go_via,Ref,From,By,To,NewLoop},
            loop();
        stop -> bye
    end.

在外壳中:

(exec@WXFRB1824L)48> {P1,P2,P3} = ping3:start().
{<0.93.0>,<0.94.0>,<0.95.0>}
(exec@WXFRB1824L)49> ping3:start_ring(P1,P2,P3,20),ping3:start_ring(P2,P1,P3,15).     
start ring for 20 iterations of #Ref<0.0.1.91077>
start ring for 15 iterations of #Ref<0.0.1.91080>
{start,<0.93.0>,<0.95.0>,15}
continue ring for 19 iterations of #Ref<0.0.1.91077>
continue ring for 14 iterations of #Ref<0.0.1.91080>
continue ring for 18 iterations of #Ref<0.0.1.91077>
continue ring for 13 iterations of #Ref<0.0.1.91080>
continue ring for 17 iterations of #Ref<0.0.1.91077>
continue ring for 12 iterations of #Ref<0.0.1.91080>
continue ring for 16 iterations of #Ref<0.0.1.91077>
continue ring for 11 iterations of #Ref<0.0.1.91080>
continue ring for 15 iterations of #Ref<0.0.1.91077>
continue ring for 10 iterations of #Ref<0.0.1.91080>
continue ring for 14 iterations of #Ref<0.0.1.91077>
continue ring for 9 iterations of #Ref<0.0.1.91080>
continue ring for 13 iterations of #Ref<0.0.1.91077>
continue ring for 8 iterations of #Ref<0.0.1.91080>
continue ring for 7 iterations of #Ref<0.0.1.91080>
continue ring for 6 iterations of #Ref<0.0.1.91080>
continue ring for 5 iterations of #Ref<0.0.1.91080>
continue ring for 4 iterations of #Ref<0.0.1.91080>
continue ring for 3 iterations of #Ref<0.0.1.91080>
continue ring for 2 iterations of #Ref<0.0.1.91080>
continue ring for 1 iterations of #Ref<0.0.1.91080>
continue ring for 0 iterations of #Ref<0.0.1.91080>
end of ring of #Ref<0.0.1.91080>
continue ring for 12 iterations of #Ref<0.0.1.91077>
continue ring for 11 iterations of #Ref<0.0.1.91077>
continue ring for 10 iterations of #Ref<0.0.1.91077>
continue ring for 9 iterations of #Ref<0.0.1.91077>
continue ring for 8 iterations of #Ref<0.0.1.91077>
continue ring for 7 iterations of #Ref<0.0.1.91077>
continue ring for 6 iterations of #Ref<0.0.1.91077>
continue ring for 5 iterations of #Ref<0.0.1.91077>
continue ring for 4 iterations of #Ref<0.0.1.91077>
continue ring for 3 iterations of #Ref<0.0.1.91077>
continue ring for 2 iterations of #Ref<0.0.1.91077>
continue ring for 1 iterations of #Ref<0.0.1.91077>
continue ring for 0 iterations of #Ref<0.0.1.91077>
end of ring of #Ref<0.0.1.91077>
(exec@WXFRB1824L)50> 
于 2012-12-07T10:44:25.247 回答
1

您可以编写一个函数来执行您告诉它的任何N时间:

times(0, Fun) -> ok;
times(N, Fun) -> Fun(), times(N - 1, Fun).

然后在进程1的代码中调用它:

times(10, fun() -> process2 ! SomeMessage end)

进程 2 的代码需要知道如何处理这个消息,例如向进程 3 发送一些东西。

于 2012-12-07T09:50:02.293 回答