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 is created by calling
spawn(Module, Name, Args) -> pid() Module = Name = atom() Args = [Arg1,...,ArgN] ArgI = term()
The new process will start 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 may be any term.
A process is said to terminate normally, if the exit
reason is the atom
A process terminates with exit reason
A process can terminate itself by calling one of the BIFs
A process may 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 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.
When a process terminates, it will terminate 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 will 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
It is possible to create monitors 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()