[erlang-questions] supervisor:start_child

Vance Shipley <>
Mon May 2 22:56:38 CEST 2011

On Mon, May 02, 2011 at 11:19:59PM +0300, Martin Dimitrov wrote:
}  In our app we actually use the sup functions (delete_child and so on)
}  but we keep only the pids and since the relationship is one-to-one 

But really the relationship is not one to one but one to many,
at least over time.  Consider this:

Start a supervisor with one child:

     1> {ok, Sup} = supervisor:start_link(super, []).

Start a new child:

     2> Id = make_ref().
     3>  ChildSpec = {Id, {gen_server, start_link, [server, [], []]}, permanent, 4000, worker, [server]}.  
     4>  supervisor:start_child(Sup, ChildSpec).

Now we have a process with pid() <0.38.0> created by the child specification:

     5> Children = supervisor:which_children(Sup).

Now something tragic happens to that child:

     6> {Id, Pid, _, _} = lists:keyfind(Id, 1, Children).
     7> exit(Pid, oops).

That child specification is still identified by the unique reference
however the process it is managing has changed to <0.42.0>:

     8> supervisor:which_children(Sup).

}  we retrieve the dynamic id. I thought that there might be an easier
}  way but from what I understand now there isn't.

You either need to save the Id or dynamically learn it:

     9> {Id, Pid, _, _} = lists:keyfind(Pid, 2, Children).

I would add though that it is likely you really be considering using
another supervision layer.  Start a simple_one_for_one supervisor
for each type of worker under the supervisor you are using now.


More information about the erlang-questions mailing list