Erlang is designed for massive concurrency. Erlang processes are lightweight (grow and shrink dynamically) with small memory footprint, fast to create and terminate, and the scheduling overhead is low.
A process is created by calling
spawn(Module, Name, Args) -> pid() Module = Name = atom() Args = [Arg1,...,ArgN] ArgI = term()
The new process starts executing in
There exist a number of other
Besides addressing a process by using its pid, there are also BIFs for registering a process under a name. The name must be an atom and is automatically unregistered if the process terminates:
When a process terminates, it always terminates with an exit reason. The reason can be any term.
A process is said to terminate normally, if the exit
reason is the atom
A process terminates with an exit reason
A process can terminate itself by calling one of the following BIFs:
The process then terminates with reason
A process can also be terminated if it receives an exit signal
with another exit reason than
Processes communicate by sending and receiving messages.
Messages are sent by using
the
Message sending is asynchronous and safe, the message is guaranteed to eventually reach the recipient, provided that the recipient exists.
Two processes can be linked to each other. A link
between two processes
Links are bidirectional and there can only be one link between
two processes. Repeated calls to
A link can be removed by calling the BIF
Links are used to monitor the behaviour of other processes, see
Erlang has a built-in feature for error handling between processes. Terminating processes emit exit signals to all linked processes, which can 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.
See
When a process terminates, it terminates with an
exit reason as explained in
A process can also call the function
The default behaviour when a process receives an exit signal
with an exit reason other than
A process can be set to trap exit signals by calling:
process_flag(trap_exit, true)
When a process is trapping exits, it does not terminate when
an exit signal is received. Instead, the signal is transformed
into a message
An exception to the above is if the exit reason is
An alternative to links are monitors. A process
If
{'DOWN', Ref, process, Pid2, Reason}
If
Monitors are unidirectional. Repeated calls to
A monitor can be removed by calling
Monitors can be created for processes with registered names, also at other nodes.
Each process has its own process dictionary, accessed by calling the following BIFs:
put(Key, Value) get(Key) get() get_keys(Value) erase(Key) erase()