[erlang-questions] Programming question

Sean Hinde sean.hinde@REDACTED
Wed Jan 24 16:10:20 CET 2007


On 24 Jan 2007, at 14:28, Ladislav Lenart wrote:

> 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.

This won't do it. If A dies while processing the call from B, even  
when linked, then B does not die.

>
> 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
>      state.
>    * Modify loopB so it can receive the following message:
>        {'DOWN', Ref, process, PidA, Info}

You will notice that B does not have a receive statement of its own.  
It is relying on gen_server, so this will not help.

>      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?

Yes, that would be the thing to do. OTP behaviours are pretty  
strightforward once you get you head around them

Sean






More information about the erlang-questions mailing list