simple_one_for_one vs one_for_one:

相同点:

这种Restart Strategy和one_for_one基本相同(即当一个child process挂掉后,仅仅重启该child process 而不影响其他child process)。

异同点:

1, simple_one_for_one supvisor启动时不会启动任何子进程,所有子进程都只能通过调用 supervisor:start_child(Sup, Args) 来动态启动。

2, simple_one_for_one supvisor持有child process的定义,并有一个dict存放数据, 其实就是如干个child process,共享一份数据。

3, 一个simple_one_for_one supervisor只有一个(single)simple_one_for_one的定义, 也就是说它只能生产出一种类型worker process。

supervisor:start_child(Sup, Args) :

Sup:  Supervisor的Pid或者registered Name.

Args: 当supervisor的类型是simple_one_for_one时,Args会追加到spec的参数中。

例如:

-module(simple_sup).
-behaviour(supervisor). -export([start_link/0]).
-export([init/1]). start_link() ->
supervisor:start_link({local,?MODULE}, ?MODULE, []). init(_Args) ->
  {M, F, A} = _Args,
{ok, {{simple_one_for_one, 0, 1},
[{M, {M, F, A},
temporary, brutal_kill, worker, [M]}]}}.

调用supervisor:start_child(simple_sup, Args)后,最终启动子进程的代码会是:apply(M, F, A++Args).

调用supervisor:start_child(Sup, Args)可能会遇到的错误:

1, noproc: 可能的原因是在调用supervisor:start_link时没写参数{local, ?MODULE},即上面代码红色部分,此时进程Sup并不存在,所以会产生

noproc错误。

2,undef: 可能的原因是A++Args后,与child process的start_link函数参数不一致。

实例代码:

实例包含两部分,一是监控普通进程(normal_process.erl),二是监控gen_server进程(gen_server_process.erl)。

common.hrl

%%-define(CALL, normal_process).
-define(CALL, gen_server_process).

simple_sup.erl

-module(simple_sup).
-behaviour(supervisor). -export([start_link/0]).
-export([init/1]). -include("common.hrl"). start_link() ->
supervisor:start_link({local, ?MODULE}, simple_sup, []). init(_Args) ->
{ok, {{simple_one_for_one, 0, 1},
[{?CALL, {?CALL, start_link, []},
temporary, brutal_kill, worker, [?CALL]}]}}.

normal_process.erl

-module(normal_process).

-export([start_link/0, start_loop/1]).

start_link() ->
proc_lib:start_link(?MODULE, start_loop, [self()]). start_loop(Parent) ->
proc_lib:init_ack(Parent, {ok, self()}),
loop(). loop() ->
receive
Args ->
io:format("~p~n", [Args])
end.

gen_server_process.erl

-module(gen_server_process).
-behaviour(gen_server).
-export([start_link/0]). %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). %% interface
-export([start/0, stop/0, echo/1]). %% interface implement
start() -> start_link().
stop() -> gen_server:call(?MODULE, stop).
echo(String) -> gen_server:call(?MODULE, {echo, String}). %% gen_server callbacks implement
start_link() -> gen_server:start_link({local,?MODULE}, ?MODULE, [], []). init([]) ->
{ok, 0}. handle_call({echo, String}, _From, Tab) ->
Reply = String,
{reply, Reply, Tab}; handle_call(stop, _From, Tab) ->
{stop, normal, stopped, Tab}. handle_cast(_Msg, State) -> {noreply, State}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.

编译命令:

c(simple_sup).
c(normal_process).
c(gen_server_process).

测试命令:

simple_sup:start_link().
supervisor:start_child(simple_sup, []).

测试通过start_child启动普通进程:

修改common.hrl:-define(CALL, normal_process). 执行编译命令 + 测试命令。

测试通过start_child启动gen_server进程:

修改common.hrl:-define(CALL, gen_server_process). 执行编译命令 + 测试命令。

erlang supervisor simple_one_for_one实例的更多相关文章

  1. erlang supervisor说明

    Supervisor Behaviour是一个用来实现一个supervisor进程来监控其他子进程的模块 子进程可以是另一个supervisor,也可以是一个worker进程. worker进程一般使 ...

  2. [erlang]supervisor(监控树)的重启策略

    1. init函数 init() -> {ok, {SupFlags, [ChildSpec,...]}} | ignore. [ChildSpec,...] 是在init之后默认要启动的子进程 ...

  3. erlang supervisor中启动普通的进程

    文字部分转自: http://1234n.com/?post/qou3eb supervisor的子进程 一开始使用supervisor的时候,我用的是init/1返回子进程规格列表的方式,并且所有子 ...

  4. 理解Erlang/OTP Supervisor

    http://www.cnblogs.com/me-sa/archive/2012/01/10/erlang0030.html Supervisors are used to build an hie ...

  5. Supervisor行为分析和实践

    1.简介     Erlang要编写高容错性.稳定性的系统,supervisor就是用来解决这一问题的核心思想.通过建立一颗监控树,来组织进程之间的关系,通过确定重启策略.子进程说明书等参数信息来确定 ...

  6. erlang四种监控策略

    转自:http://jasionq.blog.163.com/blog/static/10970577920133883158424/ Supervisor Behaviour是一个用来实现一个sup ...

  7. 分布式mongodb分片集群

    本博客先简单介绍mongodb入门以及单实例以及mongodb的主从(主从官网是不提倡用的,原因后续介绍),副本集,分片. 第一:nosql介绍: 数据库分为关系型数据库与非关系型数据库,及具代表性的 ...

  8. Syncthing源码解析 - 启动过程

    我相信很多朋友会认为启动就是双击一下Syncthing程序图标,随后就启动完毕了!如果这样认为,对,也不对!对,是因为的确是这样操作,启动了Syncthing:不对是因为在调试Syncthing启动过 ...

  9. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

随机推荐

  1. LaTeX:Figures, Tables, and Equations 插入图表和公式

    Figures To insert a figure in a LaTeX document, you write lines like this: \begin{figure} \centering ...

  2. Spring @Autowired 注解不生效

    @Autowired默认不生效.为了生效,需要在xml配置:<context:annotation-config> 注解一<context:component-scan base-p ...

  3. ajax常用请求方式

    1.JAVA @RequestMapping(value = "testAjax") @ResponseBody public Map<String, Object> ...

  4. spring的6个不同的功能模块

    Spring自带的jar文件 Spring模块组成图 Spring的主要模块各自是核心Spring容器,spring的AOP模块,数据訪问与集成,web和远程调用,測试. 核心spring容器: 容器 ...

  5. SQL盲注测试高级技巧

    写在前面: 这篇文章主要写了一些加快盲注速度的技巧和盲注中比较精巧的语句,虽然注入并不是什么新技术了.但是数据库注入漏洞依然困扰着每一个安全厂商,也鞭策着每一个安全从业者不断前进. 正文: 首先来简单 ...

  6. scikit-learn入门学习记录

    一加载示例数据集 from sklearn import datasets iris = datasets.load_iris() digits = datasets.load_digits() 数据 ...

  7. 非意外的PDB错误 OK(0)

    用ib编项目会出现这个error 用vs重新编译全部 就没有问题 ib的设置改下 Visual Studio Builds--Advanced --PDB File Allocation Force ...

  8. 大气散射 Aerial Perspective

    http://mathinfo.univ-reims.fr/IMG/pdf/PreethamSig2003CourseNotes.pdf https://blog.csdn.net/toughbro/ ...

  9. MFC【17-3】线程和线程同步化

    17.3小知识点 17.3.1消息泵 编写一个应用程序,让它响应某菜单命令,画几千个椭圆. void CMFC线程View::OnStartDrawing(void) { m_bQuit=FALSE; ...

  10. EPF与Myeclipse 增强代码自动智能提示

    摘自: http://blog.csdn.net/ylchou/article/details/7639467 数字证书文件,导入用. EPF文件是著名的软件开发工具——Eclipse(IDE)的配置 ...