Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  why does supervisor not work?

kornerhill
Posted: Sat Apr 24, 2010 2:22 pm Reply with quote
Joined: 24 Apr 2010 Posts: 6
I am a newbie to erlang, and I write a script to test supervisor, but it seems there is something wrong with this script. I just cannot figure out why. my script is attached at the end of my post. It compiles successfully, but I run it in the shell and get an exception:

8> superv:start().
** exception exit: shutdown

I thought the child process should run forever, why does it shutdown? Could anyone figure me out?


%% filename : superv.erl
-module(superv).
-compile(export_all).
-behaviour(supervisor).

start() ->
supervisor:start_link(superv, []).

init(Param) ->
{ok,{{one_for_one, 100, 2},[{proc1, {superv, start_calc, []}, permanent,
500, worker, [superv]}]}}.

start_proc() ->
spawn_link(superv, loop, []).

loop() ->
io:format("loop"),
receive X -> X end,
loop().
View user's profile Send private message
zajda
Posted: Sun Apr 25, 2010 10:42 am Reply with quote
User Joined: 22 Aug 2009 Posts: 83
First of all, there is a typo in your child_spec, should be start_proc not start_calc:
Code:
[{proc1, {superv, start_proc, []}, permanent,
500, worker, [superv]}]


More important is that it will not work anyway, because in start_proc you only start new process end exit with normal reason..

So, it doesnt make any sense to hook up to supervisor process which exits at the beginning. To fix it, copy paste body of loop to start_proc.

Usually it is gen_server, gen_fsm, gen_event or supervisor what is building supervision tree.




Michał Zajda
------------------
Erlang Solutions Ltd
View user's profile Send private message
kornerhill
Posted: Sun Apr 25, 2010 12:02 pm Reply with quote
Joined: 24 Apr 2010 Posts: 6
hi, zajda, thanks! It works. I thought start_proc/0 was used to create the worker process, but it seems that start_proc/1 itself is the process loop.
View user's profile Send private message
zajda
Posted: Sun Apr 25, 2010 1:11 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
hi, one more thing: try to stick to regular function's names i.e. start_link instead of start.

Also using export_all is not recommended (it makes a mess - no clear api and produces worse code after compilation).



Michał Zajda
------------------
Erlang Solutions Ltd
View user's profile Send private message
kornerhill
Posted: Mon Apr 26, 2010 2:20 am Reply with quote
Joined: 24 Apr 2010 Posts: 6
hi, Michal, Ok, I see.
View user's profile Send private message
MathewBracken
Posted: Wed Jun 02, 2010 6:36 am Reply with quote
Joined: 24 Feb 2010 Posts: 3
More important is that it will not work anyway, because in start_process you only start new process end exit with normal reason. He is always manage all workers.
View user's profile Send private message
zajda
Posted: Fri Jun 04, 2010 4:43 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
I guess you wanted to quote 1st sentence (mine) and comment it (second sentence).

Yes, supervisor watches all workers = the specific processes which are configured in supervisor init/1. But if those processes spawn some new processes supervisor knows nothing about them, cannot control them any how.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum