Concurrency is defined as “the temporal property of two things happening at the same time”. In Computer Science, the definition is “concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other. The computations may be executing on multiple cores in the same chip, preemptively time-shared threads on the same processor, or executed on physically separated processors.”
Erlang was built with concurrency and fault-tolerance in mind.
The granularity of concurrency in Erlang is a process. A process is an activity/task that runs concurrently with and independently from the other processes (though processes can interact with each other using messages, links, etc.). Processes in Erlang are different than the processes and threads most people are familiar with. Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by the Erlang’s Virtual Machine (VM). The creation time of process is very low, the memory footprint of a just spawned process is very small, and a single Erlang VM can have millions of processes running.
The communication model (among processes) in Erlang is message passing. Erlang processes share no memory. The way that processes communicate is via message passing (asynchronous). Every process (even the shell) holds a mailbox queue where incoming messages are placed until received by the process. Message passing is asynchronous because the sending process does not block on send. Sending a message in Erlang is a non-blocking operation that always succeed (more in the next post).
Why Message Passing?
We are so used to the shared memory model, why changing it? Here are some characteristics that are part of Erlang mostly because of the message passing memory model.
Message passing allows for easier distributed programming. Imagine if you want to distribute an application that uses shared memory. To do this, one should either use a message passing solution (such as MPI) or a Distributed Shared Memory system (DSM), that also uses message passing to operate. Why not using message passing in the first place? Especially in Erlang, message passing allows for location transparency (when sending a message there is no difference to the programmer if the receiver resides in the local or a remote node).
Erlang provides Built-In Functions that are used to spawn new processes. The simplest one is spawn/1|3 (the 1|3 denotes that both spawn/1 and spawn/3 functions exist).
Pid stands for Process identifier and is the datatype used for the unique process, identifiers that are assigned to every process.
Creates a new process and returns its pid. The new process is placed in the system scheduler queue, so it will be run some time later.
Called as spawn(Fun). The new process will run function Fun with an empty list () as input.
Called as spawn(Module, Function, Args). The new process will run function Module:Function with the elements of the list Args as input.
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Creating Linked Processes
A very useful feature is to create a new process that is linked to the “parent” one. The link between them guarantees that if one of the two fails (finishes abnormally), the other one also stops executing. This feature is very helpful because it reduces the need for “cleaning up” in case of a failure. Instead of explicitely handling an error, the “let it fail and let someone else handle it” philosophy can be used. The BIF(s) providing this functionality are the spawn_link/1|3.
Creates a bidirectional link between the calling process and another process (or port), if there is not such a link already. If a process attempts to create a link to itself, nothing is done. Returns true.
Provides the same functionality as spawn/1|3 with the addition that a link is atomically created between the caller and the newly spawned process.
Same call convention as spawn/1.
Same call convention as spawn/3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Other Processes’-related Built-In Functions
There are several other BIFs related to processes. The following are some commonly used.
Returns true if the argument is a pid, else false.
1 2 3 4 5
Called as is_process_alive(Pid). Pid must refer to a process at the local node. Returns true if the process exists and is alive, that is not exiting and has not exited. Otherwise, returns false.
Transforms the input string to a pid. This BIF is intended to be used for debugging and not in the application development.
Returns the textual representation of a pid. This BIF is intended to be used for debugging only.
Registers a process (or a port) with a name. This name can be later used to refer to the process.
1 2 3 4
Returns a list with the names of all registered processes.
1 2 3 4 5 6
One of the most commonly used BIF, returns the pid of the calling processes. As you will see in the next post (about messaging), self is used in almost every message send.
Sends a message to a process. You will see message sending in detail in the next post.
Sends a message after a given amount of time.
Removes the link between two processes. Returns true even if there is no exist link.
Called as unregister(Name). Removes the association between the Name and the process which it is associated with.
1 2 3 4 5 6 7 8
Called as whereis(Name). Returns the pid of the process that is register with the name Name.