Erlang / Erlang Basics Interview questions
What is the difference between spawn and spawn_link?
Both start a new process and return its PID, but they differ in what happens when that new process terminates abnormally.
| spawn | spawn_link |
| Caller and new process are independent; if the child crashes, the caller is unaffected. | Caller and new process are linked; if either crashes, the other receives an exit signal too. |
| You must set up monitoring manually to be notified of a crash. | By default, an abnormal exit propagates and kills the linked process unless it's trapping exits. |
Because a crash silently propagating can be exactly what you want (fail the whole group together) or exactly
what you don't (an isolated worker), OTP supervisors use spawn_link internally combined with
process_flag(trap_exit, true), so the supervisor is notified of the crash instead of being killed
by it.
More Related questions...