[erlang-questions] Programming question

Ladislav Lenart <>
Wed Jan 24 15:28:02 CET 2007

Sean Hinde wrote:
> Hi,
> A quick question for Erlang gurus out there.
> I have two processes A and B. B spends most of its life in a series  
> of gen_server calls towards A:
> loopB(PidA, S) ->
>       Res = gen_server:call(PidA, {op, stuff}),
>       S1 = process(Res, S),
>       loopB(PidA, S1).
> The question is how do I set things up so that if A dies B is killed?

I am not a guru, but if "to die" means to terminate abnormally,
you can link the two processes. BUT since links are bi-directional,
if B dies, A will be killed as well.

If this is not what you want, you can either start trapping exit
signals OR you might take a look at erlang:monitor. It should
allow you to create one-directional link. Modify B as follows:
   * Create a monitor to process A (during initialization perhaps)
     and store the resulting reference (Ref) as part of B's internal
   * Modify loopB so it can receive the following message:
       {'DOWN', Ref, process, PidA, Info}
     where Info is either the exit reason, noproc or noconnection.
     You can terminate the process when this message is received.

BTW I have a question too. If B is implemented in terms of
gen_server, I have to implement handle_info callback function
in order to react upon receive of a 'DOWN' message from the
monitor, right?

Hope this helps,

Ladislav Lenart

More information about the erlang-questions mailing list