避免竞争条件

发布于 2024-10-06 09:53:36 字数 620 浏览 2 评论 0原文

以下代码片段摘自 Francesco Cesarini 和 Simon Thompson 所著《Erlang 编程》一书的第 112 页,作为 Erlang 中可能存在的竞争条件的说明。

start() -> 
  case whereis(db_server) of
    undefined -> 
      Pid = spawn(db_server, init, []), 
      register(db_server, Pid), 
      {ok, Pid};
    Pid when is_pid(Pid) ->
      {error, already_started}
  end.

在不逐字复制详细信息的情况下,作者解释说,如果两个进程同时执行 start(),则运行“未定义”部分的进程 1 可能无法完成,因为进程 2 会导致它被抢占。然后,进程 2 将运行“未定义”部分直至完成。现在,当进程 1 恢复时,db_server 已被进程 2 注册,导致其对 register() 的调用引发运行时错误。我希望你能明白我的意思,因为我不想窃取这本书的文字。

我的问题是如何对上述精确功能进行编码以避免两个进程同时执行 start() 时潜在的竞争条件?

The following code snippet is taken page 112 of the book by Francesco Cesarini and Simon Thompson, Erlang Programming, as an illustration of a possible race condition in Erlang.

start() -> 
  case whereis(db_server) of
    undefined -> 
      Pid = spawn(db_server, init, []), 
      register(db_server, Pid), 
      {ok, Pid};
    Pid when is_pid(Pid) ->
      {error, already_started}
  end.

Without copying the details verbatim, the authors explain that if two processes simultaneously execute start(), then process 1 running the "undefined" section might not complete, because process 2 causes it to be preempted. Process 2 would then run the "undefined" section to completion. Now, when process 1 resumes, db_server has already been registered by process 2, causing the its call to register() to throw a runtime error. I hope you can understand what I mean, because I don't want to filch the book's text.

My question is how can the above exact functionality be coded to avoid the potential race condition when two processes simultaneously execute start()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-10-13 09:53:36

通常,这是通过让生成的进程注册自己的名称,然后向其父进程发回响应,告诉父进程是否成功来解决的。

start() ->
   Pid = spawn(db_server, init, [self()]),
   receive {Pid, StartResult} ->
       StartResult
   end.

init(Parent) ->
    try register(db_server, self()) of
        true ->
            Parent ! {ok, started},
            real_init()
    catch error:_ ->
        Parent ! {error, already_started}
    end.

(可能无法编译或工作。在此处键入而不进行检查。:))

您可以在 gen.erl 中找到该版本的精心实现的版本。实际上,在实际代码中,您只需使用 OTP 行为来重用该版本。

Generally this is solved by having the spawned process register its own name, and then send back a response to its parent telling the parent whether or not it succeeded.

start() ->
   Pid = spawn(db_server, init, [self()]),
   receive {Pid, StartResult} ->
       StartResult
   end.

init(Parent) ->
    try register(db_server, self()) of
        true ->
            Parent ! {ok, started},
            real_init()
    catch error:_ ->
        Parent ! {error, already_started}
    end.

(May not compile or work. Typed in here without checking. :))

You can find a carefully implemented version of this in gen.erl. In practice, in real code you just use the OTP behaviours to reuse that version.

热风软妹 2024-10-13 09:53:36

您可以使用 gen_server 来序列化请求。

You might use a gen_server to serialize the requests.

旧城烟雨 2024-10-13 09:53:36

您要启动多少台服务器?您最初的问题暗示了一个,而对 @cthulahoops 的评论则暗示了两个,一台服务器和一台备份。对于两台服务器,您可以尝试类似的操作:

start() ->
    case whereis(db_server) of
        undefined ->
            Spid = spawn(db_server, init, []),
            %% In race condition there can be only one that succeeds to register
            case catch register(db_server, Spid) of
                true -> {ok,Spid};             %We are it
                {error,_} ->                   %Server registered, register as backup
                    register(db_server_backup, Spid),
                    {ok,Spid}
            end;
        _ ->                                   %Server registered, start backup
            Bpid = spawn(db_server, init, []),
            register(db_server_backup, Bpid),
            {ok,Bpid}
    end.

不过我还没有运行它。

How many servers do you want to start? Your original question implies one, while a comment to @cthulahoops says two, a server and a backup. For two servers you could try something like:

start() ->
    case whereis(db_server) of
        undefined ->
            Spid = spawn(db_server, init, []),
            %% In race condition there can be only one that succeeds to register
            case catch register(db_server, Spid) of
                true -> {ok,Spid};             %We are it
                {error,_} ->                   %Server registered, register as backup
                    register(db_server_backup, Spid),
                    {ok,Spid}
            end;
        _ ->                                   %Server registered, start backup
            Bpid = spawn(db_server, init, []),
            register(db_server_backup, Bpid),
            {ok,Bpid}
    end.

I haven't run it though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文