检查并行进程是否评估相同的功能 [英] Check if parallel processes evaluate same function

查看:133
本文介绍了检查并行进程是否评估相同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天以来,我正在学习Erlang,使用伟大的书籍编程Erlang 通过Joe Armstrong。



我正在关注并发编程的一章,在本章末尾,有一个问题需要解决:


写一个函数start(AnAtom,Fun)将AnAtom注册为spawn(Fun)。
确保您的程序在两个并行
进程同时评估start / 2的情况下正常工作。在这种情况下,您必须
保证其中一个进程成功,另一个失败。


我解决了第一部分的这个问题,确保每一个过程将被产生一个不同的原子。



关于第二部分,我被困住,因为如果我理解正确,我找不到一个方法来检查两个并行进程是否同时评估函数开始/ 2 。起初,我想到了一个计数器检查流程的产生次数,但是我无法理解如何执行此检查。我在这个主题(来自OOP)中发现了一些有趣的东西背景,我想到了Singleton模式),但是我不确定读取和写入一个带有计数器的文件是执行此检查的最佳方式。我认为有一个简单的方法。



我写的代码如下:

   - 模(PROBL)。 
-export([start / 2,stop / 1])。

start(Atom,Fun) - >
%检查这里计数器值
case isUniqueProcess(Times)%在这里我试图使用一个计数器,但我被卡住..
true - >
Pid = spawn(fun() - > doSomething(Atom,Fun)end),
register(Atom,Pid);
false - >
io:format(对不起,没有更多的进程可以产生),
void
end。

stop(Atom) - >
case where(Atom)of
undefined - >无效;
Pid - > io:format(Stopped。),Pid!停止
结束。

isUniqueProcess(Times) - >在这里我应该增加柜台,但怎么样?
如果
Times =:= 0 - >
true;
Times> 0 - >
false
结束。

doSomething(Atom,Fun) - >
收到
停止 - > 15000后的
- >
Fun(),
doSomething(Atom,Fun)
end。

对于我来说,从强大的OOP背景来看,切换另一种编程思维方式并不容易它发生在Erlang,也许这是我困难的原因。如何解决这个问题?

解决方案

如果两个或更多的进程尝试以相同的名称注册,那么只有第一个一个会成功以下将在尝试注册时产生错误。在这种情况下,让后续流程崩溃是不够的吗?要执行此操作,必须在启动过程中进行注册。



如果您不想崩溃进程,可以将进程包含在catch中并终止并使用 exit(normal)显式终止它。


Since few days, I'm learning Erlang, using the great book Programming Erlang by Joe Armstrong.

I'm following the chapter about concurrent programming, and, at the end of chapter, there's a little problem to solve:

Write a function start(AnAtom, Fun) to register AnAtom as spawn(Fun). Make sure your program works correctly in the case when two parallel processes simultaneously evaluate start/2. In this case, you must guarantee that one of these processes succeeds and the other fails.

I solved the first part of this problem, making sure that each process will be spawned with a different atom.

About the second part, I'm stuck because, if I understood correctly, I cannot find a way to check if two parallel processes evaluate simultaneously the function start/2. At first, I thought about a counter checking the number of spawns of the process, but I'm not able to understand how to perform this check. I found something interesting here in this thread (coming from an OOP background, I thought about the Singleton pattern), but I'm not sure if reading and writing a file with a counter is the best way to perform this check. I think there's a simple way to do that.

The code I wrote is below:

-module(probl).
-export([start/2,stop/1]).

start(Atom,Fun) ->
% check here counter value
case isUniqueProcess(Times) of % here I'm trying to use a counter, but I'm stuck..
    true ->
        Pid = spawn(fun() -> doSomething(Atom,Fun) end), 
        register(Atom,Pid);
    false ->
        io:format("Sorry, no more processes to spawn."),
        void
end.    

stop(Atom) -> 
    case whereis(Atom) of
        undefined -> void;
        Pid -> io:format("Stopped."), Pid ! stop
    end.

isUniqueProcess(Times) -> % here I should increment the counter, but how?
    if
        Times =:= 0 ->
            true;
        Times > 0 ->
            false
    end.

doSomething(Atom,Fun) ->
    receive
        stop -> void
    after 15000 ->
        Fun(),
        doSomething(Atom,Fun)
    end.

For me, coming from a strong OOP background, it's not so easy to switch another programming mindset, like it happens in Erlang, and maybe this is the reason of my difficulties. How can I solve this problem?

解决方案

If both, or more processes, try to register under the same name then only the first one will succeed. The following will generate an error when they try to register. Wouldn't be enough just to let the subsequent processes crash in that case? To do this the registering must be done in the started process itself.

If you don't want to crash the process the you could wrap the process in a catch and terminate and explicitly terminate it using exit(normal).

这篇关于检查并行进程是否评估相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆