Spawn and links
spawn/1
spawn takes a fun and starts a new process that runs it. The call answers at once with the pid of the new process, and the caller keeps running.
The fun runs in its own process with its own heap, so whatever it prints or breaks stays outside the caller.
erl
1> spawn(fun() -> io:format("worker~n") end), ok.
worker
ok
spawn/3
The three-argument form names a module, a function, and the argument list. It is the same spawn, with a named function instead of a fun.
Here io:format/1 is the function: the argument list holds one value, the format string, so the call is io:format("worker~n").
erl
1> spawn(io, format, ["worker~n"]), ok.
worker
ok
link/1
link ties two processes together. The call answers true, and from then on each one watches the other.
A link is symmetric and needs no agreement from the other side. When one of the two dies, the other receives an exit signal. This module starts a child, registers it as child, and can kill it; save it as links.erl.
-module(links).
-export([start/0, start_boom/0, kill/0]).
start() ->
Pid = spawn(fun() -> receive stop -> ok end end),
register(child, Pid),
ok.
start_boom() ->
spawn_link(fun() -> exit(kaboom) end),
ok.
kill() ->
exit(whereis(child), shut_down),
ok.
The session traps exits first, so the shell survives the kill and can read the exit message. Without the flag, the shell would die with the child.
erlc links.erl
erl
1> process_flag(trap_exit, true).
false
2> links:start().
ok
3> link(whereis(child)).
true
4> unlink(whereis(child)).
true
5> link(whereis(child)).
true
6> links:kill().
ok
7> receive {'EXIT', _, Reason} -> Reason end.
shut_down
spawn_link
spawn_link does both steps in one call: it starts the process and links it before the new code runs. There is no window in which the child could die unlinked.
This is the form to use when the child is part of the same failure unit as the parent.
erl
1> process_flag(trap_exit, true).
false
2> links:start_boom().
ok
3> receive {'EXIT', _, Reason} -> Reason end.
kaboom
Exit signals
When a linked process dies, the runtime sends its exit reason to every linked process. Without trapping, that signal kills the receiver with the same reason, and the death spreads along the links.
A normal exit does not kill a linked process. An abnormal reason does, and that is what makes a supervisor able to restart a group as one unit.
trap_exit
process_flag(trap_exit, true) turns exit signals into ordinary messages. The signal arrives as a tuple tagged EXIT with the dying pid and the reason, and the process stays alive.
The flag is per process, and the call answers the previous value. Turn it off with process_flag(trap_exit, false) when the process should die together with its links again.
erl
1> process_flag(trap_exit, true).
false
2> process_flag(trap_exit, false).
true
Next: Monitors and exits
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
spawn/3take?Answer
A module, a function, and an argument list
spawn(Module, Function, Args)names the code to run, as inspawn(io, format, ["worker~n"]). -
What does
link(Pid)answer?Answer
trueLinking succeeds silently and answers
true. -
Is a link symmetric?
Answer
Yes, each process watches the other
A link ties both sides, with no agreement needed from the other process.
-
What does
unlink(Pid)do?Answer
Removes the link between the two processes
The two processes stop watching each other and answer
true. -
Why is
spawn_linksafer thanspawnfollowed bylink?Answer
The child is linked before it runs, so it cannot die unlinked
The link exists before the first line of the child runs, closing the race window.
-
What does a linked process receive when its partner dies abnormally?
Answer
An exit signal with the same reason
The exit reason travels along the link; the receiver dies of it unless it traps exits.
-
Which exit reason does not kill a linked process?
Answer
normalA normal exit is ignored by a linked process; abnormal reasons spread.
-
What does
process_flag(trap_exit, true)answer on its first call?Answer
falseIt answers the previous value, which is
falsein a fresh process. -
What does a trapped exit arrive as?
Answer
{'EXIT', Pid, Reason}The exit signal becomes a message tagged
EXITwith the dying pid and the reason. -
What happens after
process_flag(trap_exit, false)?Answer
Exit signals kill the process again
With trapping off, an abnormal exit reason from a link kills the process as usual.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev