xcoder
xcoder

Reputation: 1436

Erlang unlinked process terminates

If I have a process A that makes call to a function in process B (procB:func().), and func() generates an error during execution. Process B would terminate, but what about process A? Consider the following in process A:

Case 1:

 {ok, Reply} = procB:func().

Case 2:

 procB:func().

Will process A terminate in both cases? Or just in case 1 because of mismatch? Please note that the two processes are not linked.

Thanks in advance!

Upvotes: 0

Views: 83

Answers (2)

tkowal
tkowal

Reputation: 9299

You are not able to call function in another process. That is the beauty of Erlang: all communication between processes is via message passing. People sometimes confuse modules with processes. I even wrote article about it.

For example process A:

  • spawns process B
  • sends message which is for example tuple {fun_to_call, Args, self()} (you need the self() to know, where to respond
  • waits for reply using receive

Process B:

  • immediately after start waits for message
  • when receives message, does some computation and sends response back

This looks like a lot of boilerplate, so this exact pattern is abstracted in gen_server

Upvotes: 1

David Budworth
David Budworth

Reputation: 11646

There is no such thing as calling a function in another process, you can send a message to a process that it then may choose to call a function based on message content.

gen_servers work this way, you send a message to the gen_server, and it does a match on the message and chooses if it should invoke call/cast/info/terminate functions.

Assuming you are really talking about sending a message from A to B and B decides to exit, it's all about if process A is linked/monitoring process B.

If you monitor B, you are sent a message saying that B went down and the reason. If you are linked to B, I believe the rule is you are killed if B died with a status other than 'normal'

A could also have set the flag trap_exit, which means that even if linked and B dies, A is sent a message that he should die and you get to interact with that message (ie: you may restart B, if you choose)

learn you some erlang has a good tutorial on how this works.

Upvotes: 2

Related Questions