[erlang-questions] OTP Supervisor Question

Ulf Wiger ulf.wiger@REDACTED
Mon Jan 4 13:35:01 CET 2010


Chandru wrote:
> 2010/1/4 Jayson Vantuyl <kagato@REDACTED>
> 
>> If I'm an OTP-style process (say, a gen_server), and I want to know who my
>> parent is (say, a supervisor), is this possible using any API, or do I need
>> to pass in the Parent explicitly?  Is using supervisor:get_child a good way
>> to find a process's sibling?  Would it be better to start a single
>> gen_server and have it use normal, linked processes?
>>
> 
> I think using supervisor:which_children(Sup_ref)  is quite reasonable. But
> for this, you'll have to store the process id of the supervisor in all the
> supervised processes.

The supervisor PID will be the head of get('$ancestors'), provided
the child is started using proc_lib (which is true for all OTP
behaviors).

Eshell V5.6.5  (abort with ^G)
1> proc_lib:spawn(fun() -> io:fwrite(">> ~p~n", [get()]) end).
 >> 
[{'$ancestors',[<0.30.0>]},{'$initial_call',{erl_eval,'-expr/5-fun-1-',0}}]
<0.32.0>
2> self().
<0.30.0>
3> proc_lib:spawn(fun() -> io:fwrite(">> Parent is ~p~n", 
[hd(get('$ancestors'))]) end).
 >> Parent is <0.30.0>
<0.35.0>

This is not documented, but has been stable since the dawn of OTP,
and is unlikely to change anytime soon.

Of course, the proc_lib could export some of the helper functions,
such as get_ancestors/[0,1] or my_info/0...

BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Solutions Ltd, formerly Erlang Training & Consulting Ltd
http://www.erlang-solutions.com
---------------------------------------------------

---------------------------------------------------

WE'VE CHANGED NAMES!

Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.

www.erlang-solutions.com



More information about the erlang-questions mailing list