Start a normal process in Erlang supervisor

Source: Internet
Author: User

Http://www.cnblogs.com/little-ant/p/3192968.html

Text part turn from: http://1234n.com/?post/qou3eb

Supervisor Sub-process

When I started using supervisor, I used INIT/1 to return the list of child process specifications, and there were only two types of child processes, one was the supervisor process, and the other was gen_server.

But this time in code refactoring, I encountered a situation. If I can start a normal process instead of gen_server, I can simplify some of the module code that I don't think is necessary to make gen_server. Because some module code completely useless to any gen_server advantage, just borrow gen_server to do as Supervisor child process start. So I started experimenting with how to start a normal process in supervisor.

Through experiments, I found that my original understanding of supervisor is completely unfounded speculation, and indeed practice is the only criterion to test the truth.

First, I thought that the module, Function, and arguments provided in Supervisor's child process specification are the portals of the child process, and supervisor will start the child process by itself. In practice, however, the child process specification provides the START function entry for the child process, and supervisor initiates the child process by invoking the entry function, which tells the supervisor child process to create the success by returning {OK, Pid}.

Secondly, it is not simple to start a process in the child process startup function by Spawn and then return {OK, Pid} can let the child process have the function of automatic restart error. In fact, you need to start a subprocess with Proc_lib:spawn_link or Proc_lib:start_link to let supervisor automatically restart the child process if it exits with an error.

In the 2nd above, I learned by reading Gen_server code, because the code I used to do the experiment did not use spawn (because of the 1th mistake), but it was easy to find out, then I used the spawn seemingly correctly started the child process, In practice, however, these child processes will not be started again if they fail to exit. I changed Gen_server experiment again, convinced that using Gen_server can be restarted. So I read Gen_server:start_link's code to find out, from the source can see the Gen_server module call Gen module to start the process, and the Gen module finally through the Proc_lib:start_link to start the process.

The difference between Proc_lib:start_link and Proc_lib:spawn_link is that the former creates the child process synchronously, which is the asynchronous creation of the child process, which is blocked after the proc_lib:start_link call until the child process is initialized. Returned after calling Proc_lib:init_ack. A proc_lib:spawn_link call returns the child process ID immediately.

Test the code (which has no subject-related test code, please disregard):

Parent_sup.erl

-module (Parent_sup).-behaviour (Supervisor).-export ([start_link/0]).-export ([INIT/1]). Start_link ()    Supervisor:start_link (? MODULE, []). Init (Args), {    ok, {{one_for_one, 1, $},          [%%{child, {child, Start_link, ["Hello"]},           percent Permanent, Brutal_kill, worker, [child]},           {parent, {parent, start_link, ["Args"]},           permanent, Brutal_kill, Worker, [parent]}          ]}.

Parent.erl

-module (parent).-export ([START_LINK/1]).-export ([START_LOOP/1]). Start_link (Args)    , Io:format ("~p~n", [ Args]),    Ret = Proc_lib:start_link (? MODULE, Start_loop, [Self ()]),    io:format ("~p~n", [Ret]),    Ret.start_loop (Parent),    proc_lib:init _ack (Parent, {OK, self ()}),    loop (). Loop (),    receive        Args,            io:format ("~p~n", [Args])    end.

Test command:

Erl-boot START_SASL

22> C (parent).
{Ok,parent}
23> C (parent_sup). Parent_sup.erl:10:warning:variable ' Args ' is unused{ok,parent_sup}24> C (parent). {ok,parent}25> Parent_sup:start_link (). " Args "{ok,<0.123.0>}=progress report==== 16-jul-2013::11:05:24 = = = Supervisor: {<0.122.0>,parent_sup} Started: [{pid,<0.123.0>}, {name,parent}, {mfargs,{parent,st art_link,["Args"}}, {restart_type,permanent}, {Shutdown,brutal_kill}, {Child_type,worker}] {ok,<0.122.0>}26> exit (PID (0,123,0), Some_reason). =supervisor report==== 16-jul-2013::11:06:28 = = = Supervisor: {<0.122.0>,parent_sup} context:child_terminated Reason:some_reason offender: [{pi D,<0.123.0>}, {name,parent}, {mfargs,{parent,start_link,["args"}}, {restart_type,permanent}, {Shutdown,brutal_kIll}, {child_type,worker}]=supervisor report==== 16-jul-2013::11:06:28 = = = SUPERVISOR: {&LT;0.122.0&G T;,parent_sup} context:shutdown reason:reached_max_restart_intensity offender: [{Pid,<0.123.0&gt ;}, {name,parent}, {mfargs,{parent,start_link,["args"}}, {Restart_type , permanent}, {Shutdown,brutal_kill}, {child_type,worker}]** exception EXIT:SHUTDOWN27&G T C (parent). {ok,parent}28> C (parent_sup). Parent_sup.erl:10:warning:variable ' Args ' is unused{ok,parent_sup}29> parent_sup:start_link (). "args" {ok,<0.138.0>}=progress report==== 16-jul-2013::11:07:19 = = = Supervisor: {<0.137.0>,parent_sup} Started: [{pid,<0.138.0>}, {name,parent}, {Mfargs,{parent,s tart_link,["Args"}}, {restart_type,permanent}, {Shutdown,brutal_kill}, {child_type,worker}]{ok,<0.137.0>}30> exit (PID (0,13 8,0), Some_reason). " Args "=supervisor report==== 16-jul-2013::11:07:28 = = = SUPERVISOR: {<0.137.0>,parent_sup} context:child_t erminated Reason:some_reason offender: [{pid,<0.138.0>}, {name,parent}, {mfargs,{parent,start_link,["args"}}, {restart_type,permanent}, {Shutdown,brutal_k Ill}, {child_type,worker}]{ok,<0.140.0>}=progress report==== 16-jul-2013::11:07:28 = = = sup Ervisor: {<0.137.0>,parent_sup} started: [{pid,<0.140.0>}, {name,parent}, {mfargs,{parent,start_link,["args"}}, {restart_type,permanent}, {Shutdown,brutal_kill}, {child_type,worker}]true31>

Start a normal process in Erlang supervisor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.