【问题标题】:Erlang multiprocessing messages receiving and sendingErlang 多处理消息接收和发送
【发布时间】:2016-03-19 10:39:52
【问题描述】:

我正在学习“Learn you some Erlang”一书。我有几个问题要解决关于使用 Erlang 进行多处理编程的问题。基本上,我正在阅读和模仿“Learn you some Erlang”。我已经在我的程序中生成了一个带有一些面向函数的程序。它说冰箱可以像它自己一样储存和取用。因此,我正在处理我的程序以执行以下基本操作:

  • 首先,我需要终止消息应该对应于与存储和获取相同的形式。这意味着消息应该是{from, terminate},并且接收者回复消息{self(), terminated}

  • 其次,我想用消息{self(), {not_recognized, Msg}}回复发件人一个无组织的消息{From, Msg}

  • 第三,添加库存消息{From, {inventory} }将在冰箱2进程中将当前FoodList返回给发件人(From)。

  • 四、添加peek消息{From, {peek, a} }会查看进程中存储的项目。如果存在 a,则将消息发送回发件人(发件人):{present, a}。如果一个是 不存在,将邮件发回给发件人(发件人):{not_present, a}

我对冰箱本身的用途完全感到困惑。我已经用教程中的示例代码想出了类似下面的内容:

-module(kitchen).
-compile(export_all).

fridge1() ->
receive
    {From, {store, _Food}} ->
        From ! {self(), ok},
        fridge1();
    {From, {take, _Food}} ->
        %% uh ...
        From ! {self(), not_found},
        fridge1();
    terminate             ->
            ok
end.

fridge2(FoodList) ->                        
receive
    {From, {store, Food}} ->
        From ! {self(), ok},
        fridge2( [Food | FoodList] );       
    {From, {take, Food}} ->
        case lists:member(Food, FoodList) of
            true   ->
                From ! {self(), {ok, Food}},
                fridge2(lists:delete(Food, FoodList));      
            false  ->
                From ! { self(), not_found},                    
                fridge2(FoodList)                           
            end;
    terminate            ->
        ok
end. 

store(Pid, Food) ->
Pid ! {self(), {store, Food}},
receive
    {Pid, Msg} -> Msg
end.

take(Pid, Food) ->
Pid ! {self(), {take, Food}},
receive 
    {Pid, Msg} -> Msg
end.

start(FoodList) ->
spawn(?MODULE, fridge2, [FoodList]).

此外,还有一些我一直在思考的问题:

  1. 如何创建冰箱进程?还是已经存在了?

  2. 我如何创建一个厨师进程,将冰箱进程 pid 传递给它?

  3. 如何将物品存放在冰箱中,然后休眠几秒钟,最终我可以从冰箱中取出物品?

这可能不是最复杂的问题,但我找不到任何关于此的文档,所以我希望你们中的一些人知道答案。

【问题讨论】:

    标签: erlang multiprocessing message


    【解决方案1】:

    回答您的问题:

    1. 如何创建冰箱进程?还是已经存在?

    一旦您从 erlang shell 或其他模块调用 kitchen:start/1,函数 spawn(?MODULE, fridge2, [FoodList]) 将为您生成一个进程。查看文档了解更多详情:http://www.erlang.org/doc/man/erlang.html#spawn-3

    1. 如何创建一个厨师进程,将冰箱进程 pid 传递给它?

    正如我在第一个答案中所说,spawn/3modulefunctionarguments 作为参数,是可用于生成新进程的函数之一。因此,您应该使用它来生成一个接收kitchen:fridge2/1 pid 作为参数的进程,例如

    Kitchen = kitchen:start([]),
    Cook = spawn(cook_module, cook_function, [Kitchen]).
    

    这样您将生成一个执行cook_module:cook_function/1 的进程并将Kitchen(冰箱PID)作为参数传递。

    1. 如何将物品存放在冰箱中,然后睡几秒钟,最终我可以从冰箱中取出物品?

    您可以使用store/2 函数将物品存储到冰箱中,稍等片刻(或几秒钟,如您所愿),然后使用take/2 函数从冰箱中取出一些东西。就像这样:

    Pid = kitchen:start([]), %% this will spawn a fridge process
    kitchen:store(Pid, "delicious insect pie"), %% this will store a delicious insect pie in your fridge by executing the function fridge2( [Food | FoodList] );
    timer:sleep(1000), %% this function will make the caller process sleeps for 1000 milliseconds (you can change it for your several seconds instead...)
    kitchen:take(Pid, "delicious insect pie"). %% this will take the delicious insect pie out of the fridge, by executing the function fridge2(lists:delete(Food, FoodList));
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 2021-05-28
      • 2014-05-28
      • 2018-04-12
      • 2016-10-04
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多