【问题标题】:Erlang ring controller process relatedErlang环控制器流程相关
【发布时间】:2013-05-02 13:08:00
【问题描述】:

我是 Erlang 的新手,我有以下作业问题要解决:

“控制”进程必须提供一个用户函数 go(N,M) 在 {1,2,...,M} 中生成包含 M 个随机整数的列表 L, 建立 N 个进程(所谓的“工人”)的环并发送一个令牌 给第一个工人。当工人 k 收到一个令牌时,它会发送一个 消息 {eat, self()} 控制令牌并将其发送给下一个工人。

当控件接收到消息{eat, Pid}时,它会撤回列表的头部H L 并将元组 {H, Pid} 附加到结果列表。当列表 L 为空时,控制 向终止工作人员并打印结果的环发送停止消息 列表。

任何帮助将不胜感激

【问题讨论】:

    标签: process erlang


    【解决方案1】:

    实际上有两种方法可以解决这个问题。
    第一个是: 控制spawns 环中的所有工人,这是解决方案:

    -module(ring). 
    -export([start/3, create/4]). 
    
    start(M, N, Message) -> 
            create(undef, N, M, Message). 
    
    create(Parent, 0, M, Message) -> 
            Parent ! {created, self()}, 
            evaluate(Parent, M, Message); 
    
    create(Parent, N, M, Message) -> 
            Child = spawn(?MODULE, create, [self(), N-1, M, Message]), 
            io:format("~w ~w created~n", [Child, N]), 
            evaluate(Parent, M, Message). 
    
    evaluate(undef, M, Message) -> 
            receive 
                    {created, Last} -> 
                            Last ! Message, 
                            io:format("~w sent ~w to ~w~n", [self(), Message, Last]), 
                            evaluate(Last, M-1, Message) 
            end; 
    
    evaluate(Parent, 0, _) -> 
            receive 
                    Msg -> 
                            io:format("~w received ~w~n", [self(), Msg]), 
                            Parent ! stop, 
                            io:format("~w sent ~w to ~w~n", [self(), stop, Parent]) 
            end; 
    
    evaluate(Parent, M, Message) -> 
            receive 
                    {created, Last} -> 
                            Parent ! {created, Last}, 
                            evaluate(Parent, M, Message); 
                    Message -> 
                            io:format("~w received ~w~n", [self(), Message]), 
                            Parent ! Message, 
                            io:format("~w sent ~w to ~w~n", [self(), Message, Parent]), 
                            evaluate(Parent, M-1, Message) 
            end. 
    

    第二个是:控制spawns只有环中的第一个工人。每一个新工人 在环中,但最后一个产生下一个工人:

    -module(ring). 
    -export([start/3, start_process/1, start_process/2]). 
    
    start(M, N, Message) -> 
        Pid = spawn(ring, start_process, [N]), 
        Pid ! {message, Message, M}, 
        ok. 
    
    start_process(Count) -> 
        % This is the first spawned process - send its 
        % pid down the chain so the last process knows who its 
        % next pid is. 
        io:format("~p: Spawned ~p~n", [self(), Count]), 
        Pid = spawn(ring, start_process, [Count-1, self()]), 
        loop(Pid). 
    
    start_process(0, Last) -> 
        % This is the last process 
        io:format("~p: Linking to last ~p~n", [self(), Last]), 
        loop(Last); 
    start_process(Count, Last) -> 
        io:format("~p: Spawned ~p~n", [self(), Count]), 
        Pid = spawn(ring, start_process, [Count-1, Last]), 
        loop(Pid). 
    
    loop(NextPid) -> 
        receive 
            {message, _,   0} -> true; 
            {message, Msg, M} -> 
                io:format("~p (~p) ~p~n", [Msg, self(), M]), 
                NextPid ! {message, Msg, M-1}, 
                loop(NextPid) 
        end. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多