[erlang-questions] start_child() on simple_one_for_one supervisor -> {error, {already_started, ...}} ???

Vance Shipley vances@REDACTED
Tue Dec 16 23:02:11 CET 2008


On Tue, Dec 16, 2008 at 08:50:43PM +0100, Dominic Williams wrote:
}  The only point of registering a process is to be able to 
}  call it later, by its name. This is bad enough (it's really 
}  like having a global variable, it's hell to test, etc).

Agreed.  One alternative which I use is to pass the supervisor's
process ID as an argument to the child and later inspect the
children with supervisor:which_children/1.

	-module(foo_sup).
	-export([init/1]).
	-behaviour(supervisor).

	init(_) ->
	    FsmStartMod = foo_server,
	    FsmStartArgs = [self()],
	    FsmStartFunc = {gen_fsm, start_link, [FsmStartMod, FsmStartArgs, []]},
	    SrvStartMod = foo_server,
	    SrvStartFunc = {gen_server, start_link, [SrvStartMod, [], []]},
	    ChildSpecs = [{FsmStartMod, FsmStartFunc, permanent,
	            4000, worker, [FsmStartMod]},
	            {SrvStartMod, SrvStartFunc, permanent,
	            4000, worker, [SrvStartMod]}],
	    {ok, {{one_for_one, 10, 60}, ChildSpecs}}.


	-module(foo_fsm).
	-export([init/1, idle/2]).
	-behaviour(gen_fsm).

	-record(state, {sup}}).

	init([SupRef]) ->
	    {ok, idle, #state{sup = SupRef}}.

	idle(Event, StateData) ->
	    ...
	    Children = supervisor:which_children(StateData#state.sup),
	    {value, {_, Pid, _, _}} = lists:keysearch(foo_server, 1, Children), 
	    Foo = gen_server:call(Pid, foo),
	    ...

Often the child process I am looking for shares a common ancestor
supervisor in which case the SupRef was passed down the chain of
children.  This is funbctional programing, if you need to know 
something in a function you pass it as an argument to the function.  

	-Vance



More information about the erlang-questions mailing list