10 Processes

Erlang is designed for massive concurrency. Erlang processes are
light-weight (grow and shrink dynamically) with small memory
footprint, fast to create and terminate and the scheduling
overhead is low.

A process can terminate itself by calling one of the BIFs
exit(Reason),
erlang:error(Reason), erlang:error(Reason, Args),
erlang:fault(Reason) or erlang:fault(Reason, Args).
The process then terminates with reason Reason for
exit/1 or {Reason,Stack} for the others.

A process may also be terminated if it receives an exit signal
with another exit reason than normal, see
Error Handling below.

Two processes can be linked to each other. A link
between two processes Pid1 and Pid2 is created
by Pid1 calling the BIF link(Pid2) (or vice versa).
There also exists a number a spawn_link BIFs, which spawns
and links to a process in one operation.

Links are bidirectional and there can only be one link between
two processes. Repeated calls to link(Pid) have no effect.

A link can be removed by calling the BIF unlink(Pid).

Links are used to monitor the behaviour of other processes, see
Error Handling below.

Erlang has a built-in feature for error handling between
processes. Terminating processes will emit exit signals to all
linked processes, which may terminate as well or handle the exit
in some way. This feature can be used to build hierarchical
program structures where some processes are supervising other
processes, for example restarting them if they terminate
abnormally.

Refer to OTP Design Principles for more information about
OTP supervision trees, which uses this feature.

Emitting Exit Signals

When a process terminates, it will terminate with an exit reason as explained in Process Termination above. This exit reason is emitted in
an exit signal to all linked processes.

A process can also call the function exit(Pid,Reason).
This will result in an exit signal with exit reason
Reason being emitted to Pid, but does not affect
the calling process.

Receiving Exit Signals

The default behaviour when a process receives an exit signal
with an exit reason other than normal, is to terminate
and in turn emit exit signals with the same exit reason to its
linked processes. An exit signal with reason normal is
ignored.

A process can be set to trap exit signals by calling:

process_flag(trap_exit, true)

When a process is trapping exits, it will not terminate when
an exit signal is received. Instead, the signal is transformed
into a message {'EXIT',FromPid,Reason} which is put into
the mailbox of the process just like a regular message.

An exception to the above is if the exit reason is kill,
that is if exit(Pid,kill) has been called. This will
unconditionally terminate the process, regardless of if it is
trapping exit signals or not.