erlang消息传递架构 [英] erlang message passing architecture

查看:227
本文介绍了erlang消息传递架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果两个进程A和B同时向进程C发送消息,则在erlang中。会有竞争条件吗?




  • C! A发送的{非常大的邮件}
  • C! B发送的{非常大的消息}



将C从A接收完整的消息,然后继续来自B的消息?或者是C可能会收到A的消息块以及B的消息块?

解决方案

消息接收是一个原子操作。



如果您有兴趣如何完成,请阅读VM的源代码。如果我简化,发送过程就是执行以下步骤:


  1. 在发送过程中分配一个目标内存空间(称为环境)。

  2. 将邮件复制到该内存空间

  3. 在目标进程上执行外部锁定

  4. 链接消息进入邮箱链接列表

  5. 释放目标进程上的外部锁定

看到,复制是在外面(之前)关键部分完成的,关键部分是相当快的。这只是玩弄几个指针。


In erlang if two processes A and B are sending message to a process C simultaneously. Will there be a race condition?

  • C ! {very large message} sent by A
  • C ! {very large message} sent by B

Will C receive the complete message from A and then proceed for the message from B? or is it that C is likely be going to receive chunks of A's message along with chunks of B's message?

解决方案

Message receiving is an atomic operation.

If you are interested how it is done, read the source code of VM. If I simplify it, the sending process is doing those steps:

  1. Allocate a target memory space in the sending process (it's called environment).
  2. Copy the message to that memory space
  3. Take the external lock on the target process
  4. Link message into the mailbox linked list
  5. Release the external lock on the target process

As you can see, copying is done outside (before) critical section and the critical section is pretty fast. It is just juggling with few pointers.

这篇关于erlang消息传递架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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