Transmission of variables via output signals [Erlang]

I am wondering if variables can be sent from a dying process to the calling process. I have a process A that spawned another process B through spawn_link. B will soon die by calling exit (killed). I can catch this in via {'EXIT', From, kill}, but I would like to pass some variables from B to A before he dies. I can do this by sending a message from B to A right before it dies, but I wonder if it needs to be done "badly". Because technically I would send two messages from B to A. Right now that I look like this:

B sends a message with values to A
A receives values and re-enters receive loop
B calls exit(killed)
A receives EXIT message and spawns another linked process

The idea is that B must always exist, and when he is killed, he must be resurrected immediately. In my opinion, the best alternative, in my opinion, is something like exit (kill, [Variables]) and catch it with {'EXIT', From, kill, [Variables]}. Is it possible? And if so, are there any reasons for this? The presence of A values ​​for B, when B has not yet died, seems unsuccessful. I would have to start implementing atomic actions to prevent the simultaneous elimination of problems with two related processes. It also forces me to store variables in my receive loop.

What do I mean, if I could send values ​​directly with an EXIT call, my loop would look like this:

loop() ->
    receive ->
        {'EXIT', From, killed, Variables} -> % spawn new linked process with variables
    end.

But if I need to get the message first, go into the loop again to get the exit message, I would get;

loop(Vars) ->
    receive ->
        {values, Variables} -> loop(Variables);
        {'EXIT', From, killed} -> % spawn new linked process with variables
    end.

, , , , .

+4
2

: , , exit({killed, Values}), {'EXIT', From, killed, Values} {'EXIT', From, {killed, Values}}.

!

, , . . ( ) - , , / . , .

! ( !)

? , , , ? , ? ? , , ( , )?

, , , , ​​. , - , A B , , B, B , .

, . , B, A, B, , , - . , , , A B , , A-B! , , , .

+4

, :

main()->
    ParentPid = self(),
    From = spawn_link(?MODULE, child, [ParentPid]),
    receive
    {'EXIT', From, Reason} ->
        Reason
    end.



child(ParentPid) ->
    Value = 2*2,
    exit(ParentPid, {killed, Value}).

, erlang: exit/2

0

All Articles