这篇文章还不是最终版,有时间时,我会再来补充完善。

什么是link

Erlang程序基于进程建模,进程之间的交互机制有收发消息,link和monitor。其中,收发消息通常用于正常的进程间通讯,而link和monitor多用于异常情况处理,本文从应用的角度介绍和分析link机制。link是双向全联通的,用来将两个或多个进程绑定在一起,绑定在一起之后,VM会保证在有进程退出时,对与其绑定在一起的进程执行特定的操作。

创建link和取消link

Two processes can be linked to each other. A link between two

processes Pid1 and Pid2 is created by Pid1 calling the BIF

link(Pid2)(or vice versa). There also exists a number a spawn_link

BIFs, which spawns and links to a process in one operation.

Links are bidirectional and there can only be one link between two

processes.Repeated calls to link(Pid) have no effect.

A link can be removed bycalling the BIF unlink(Pid).

当有进程退出时

发送Exit Signal

When a process terminates, it will terminate with an exit reason as

explained in Process Termination above. This exit reason is emitted in

an exit signal to all linked processes.

A process can also call the function exit(Pid,Reason). This will

result in an exit signal with exit reason Reason being emitted to Pid,

but does not affect the calling process.

Exit Signal的默认处理方式

The default behaviour when a process receives an exit signal with an

exit reason other than normal, is to terminate and in turn emit exit

signals with the same exit reason to its linked processes. An exit

signal with reason normal is ignored

将Exit Signal转换为普通的进程消息

A process can be set to trap exit signals by calling:

process_flag(trap_exit, true)

When a process is trapping exits, it will not terminate when an exit

signal is received. Instead, the signal is transformed into a

message{'EXIT',FromPid,Reason} which is put into the mailbox of the

process just like a regular message.

An exception to the above is if the exit reason is kill, that is if

exit(Pid,kill) has been called. This will unconditionally terminate

the process, regardless of if it is trapping exit signals or not.

link与OTP的关系

OTP作为Erlang官方的编程框架被广泛应用,在OTP的实现中,link机制被广泛的应用。

Erlang has a built-in feature for error handling between processes.

Terminating processes will emit exit signals to all linked processes,

which may terminate as well or handle the exit in some way. This

feature can be used to build hierarchical program structures where

some processes are supervising other processes, for example restarting

them if they terminate abnormally.

Refer to OTP Design Principles for more information about OTP

supervision trees, which uses this feature.

gen_server

假设存在a,b两个进程,其中b是gen_server。我们在进程a中调用b:start_link(),使两个进程link在一起,然后来讨论一些异常情况。

  • a进程正常退出 -> b进程正常运行
  • a进程异常退出 -> b进程退出
  • a进程正常退出, b进程中调用了process_flag(trap_exit, true) -> b进程不会收到exit msg,退出
  • a进程异常退出, b进程中调用了process_flag(trap_exit, true) -> b进程不会收到exit msg,退出
  • b进程正常退出 -> a进程正常运行
  • b进程异常退出 -> a进程退出
  • b进程正常退出, a进程中调用了process_flag(trap_exit, true) -> a进程收到{'EXIT',Pid_b,normal}
  • b进程异常退出, a进程中调用了process_flag(trap_exit, true) -> a进程收到{'EXIT',Pid_b,Reason}

看起来第3项和第4项不太正常,似乎跟刚刚介绍的erlang link机制冲突了。出现这种现象的原因,是gen_server不是普通进程,它在一个普通的进程上,添加一些默认的行为,具体到这个问题,就是gen_server在收到来自父进程(>调用start_link>的进程)的{'EXIT',Pid_Parent,Reason}

decode_msg(Msg, Parent, Name, State, Mod, Time, Debug, Hib) ->
case Msg of
{system, From, get_state} ->
sys:handle_system_msg(get_state, From, Parent, ?MODULE, Debug,
{State, [Name, State, Mod, Time]}, Hib);
{system, From, {replace_state, StateFun}} ->
NState = try StateFun(State) catch _:_ -> State end,
sys:handle_system_msg(replace_state, From, Parent, ?MODULE, Debug,
{NState, [Name, NState, Mod, Time]}, Hib);
{system, From, Req} ->
sys:handle_system_msg(Req, From, Parent, ?MODULE, Debug,
[Name, State, Mod, Time], Hib);
{'EXIT', Parent, Reason} ->
terminate(Reason, Name, Msg, Mod, State, Debug);
_Msg when Debug =:= [] ->
handle_msg(Msg, Parent, Name, State, Mod);
_Msg ->
Debug1 = sys:handle_debug(Debug, fun print_event/3,
Name, {in, Msg}),
handle_msg(Msg, Parent, Name, State, Mod, Debug1)
end.

link与业务建模

待续

Erlang进程的Link机制的更多相关文章

  1. erlang进程监控:link和monitor

    Erlang最开始是为了电信产品而发展起来的语言,因为这样的目的,决定了她对错误处理的严格要求.Erlang除了提供exception,try catch等语法,还支持Link和Monitor两种监控 ...

  2. Erlang进程堆垃圾回收机制

    原文:Erlang进程堆垃圾回收机制 作者:http://blog.csdn.net/mycwq 每一个Erlang进程创建之后都会有自己的PCB,栈,私有堆.erlang不知道他创建的进程会用到哪种 ...

  3. 深入理解docker的link机制

    https://yq.aliyun.com/articles/55912 摘要: 什么是docker的link机制 同一个宿主机上的多个docker容器之间如果想进行通信,可以通过使用容器的ip地址来 ...

  4. 从Erlang进程看协程思想

    从Erlang进程看协程思想 多核慢慢火了以后,协程类编程也开始越来越火了.比较有代表性的有Go的goroutine.Erlang的Erlang进程.Scala的actor.windows下的fibr ...

  5. erlang进程与操作系统线程

    erlang多进程与多线程: 在erlang开发中,我们面对的最小执行单位是进程,当然这个进程并不是系统层面上的进程,也不是线程.而是基于erlang运行时系统的一个进程.那么erlang的多进程是如 ...

  6. erlang进程概述

    一.概述 与大多数的进程相反,Erlang中的并发很廉价,派生出一个进程就跟面向对象的语言中分配一个对象的开销差不多. 在启动一个复杂的运算时,启动运算.派生进程以及返回结果后,所有进程神奇的烟消云散 ...

  7. Linux进程组调度机制分析【转】

    转自:http://oenhan.com/task-group-sched 又碰到一个神奇的进程调度问题,在系统重启过程中,发现系统挂住了,过了30s后才重新复位,真正系统复位的原因是硬件看门狗重启的 ...

  8. 一篇文章了解相见恨晚的 Android Binder 进程间通讯机制【转】

    本文转载自:https://blog.csdn.net/freekiteyu/article/details/70082302 Android-Binder进程间通讯机制 概述 最近在学习Binder ...

  9. Android Binder 进程间通讯机制梳理

    什么是 Binder ? Binder是Android系统中进程间通讯(IPC)的一种方式,也是Android系统中最重要的特性之一.Binder的设计采用了面向对象的思想,在Binder通信模型的四 ...

随机推荐

  1. ASP.NET MVC中实现多个按钮提交的几种方法

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  2. GO語言視頻教程

    第1课:https://github.com/Unknwon/go-fundamental-programming/blob/master/lectures/lecture1.md Go开发环境搭建h ...

  3. Avizo - 高级三维可视化及分析软件

    今天从材料科学应用角度介绍Avizo的基本功能. Avizo是一款先进的三维可视化及分析应用,可用来探索从断层扫描.显微镜.核磁共振成像及更多其他技术获得的材料科学数据.从简单的可视化与测量到高级的图 ...

  4. 移动APP接口遇到的一些小问题

    一:IIS设置站点后无法访问apk文件 首先要给IIS服务器根目录添加MIME类型影射文件扩展名:apkMIME类型 :application/vnd.android.package-archive ...

  5. 【转载】酷酷的CSS3三角形运用

    转载:http://www.cnblogs.com/keepfool/p/5616326.html 概述 在早期的前端Web设计开发年代,完成一些页面元素时,我们必须要有专业的PS美工爸爸,由PS美工 ...

  6. Redmine性能优化方案

    近来公司redmine服务器表现很糟糕,在16核,64GRAM的机器上,压测结果竟然只有每秒5~7个请求,部分页面一个都出不来. 以下是我对Redmine性能优化方案: redmine服务器性能问题排 ...

  7. H5长按事件

    let timeOutEvent = 0 $(function () { $('#debug').on({ touchstart: function (e) { setTimeout(function ...

  8. LR6 碱性电池才能带动微软鼠标

    LR6 碱性电池才能带动微软鼠标   好前一段买个一个微软无线鼠标后来动弹不得,更换电池也不行,本来lp说为什么不扔掉,但因为实在做得很漂亮一直带在身边.改用雷柏的普通无线鼠标后也很是好用.不过要经常 ...

  9. 判断big endian和little endian的方法

    http://blog.sina.com.cn/s/blog_6ab0b9a80101awzr.html   不同体系的CPU在内存中的数据存储往往存在着差异.例如,Intel的x86系列处理器将低序 ...

  10. 日暮·第二章·烽烟传讯

    第二章 烽烟传讯   夜幕降临,整个泉州府更见喧闹,那些个白日里将养了一日的花红柳绿再也耐不住寂寞,招招摇摇着在人来人往的主街上舒展着自己的风情,妖妖娆娆地换却春风一度.    城东的招福客栈在经过了 ...