Introduction to Erlang : Concurrency (Processes)


Concurrency is defined as “the temporal property of two things happening at the same time” (according to WordNet). 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.” (according to Wikipedia).

In practise, if two, or more, tasks are concurrent, then they can run in parallel. This implies that there are no data dependencies among them. For example, the following tasks

  • unlock the car (the car was locked)
  • start the engine of the car (the engine was off)
  • drive the car

are non-concurrent since they have dependencies. One cannot start the engine of the car without first entering the car and she cannot drive it without first starting the engine. These tasks have to be executed sequentially (with a predefined order) no matter if more than one person (processing unit) help. On the other hand

  • doing the laundry
  • washing the dishes
  • go for shopping

can be potentially run in parallel. For example, three different family members can handle one task each. Moreover, if only one person has to do all the tasks, she can decide to perform them in any order she decides since there are no dependencies among them.

Concurrency in Erlang

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. as you shall see later). 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.

Messaging Passing

The communication model (among processes) in Erlang is message passing. No much need to be said about this. Erlang processes share no memory. The way that processes communicate is via (asynchronous) message passing. 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?

But 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.

Memory independency

While executing, no shared memory among processes means that there is no chance that a process will cause a memory corruption to another process. The programmer does not have to worry about data races, data corruption, memory synhronization, etc.

Crash independency

Processes do not share anything. As a result, the failure (crash) of a process cannot affect another. This is very helpful when builting fault-tolerant and reliable systems (most of the “real-world” systems). Due to this, Erlang inherits the “let it fail” programming approach; if something “bad” happens, let the process fail and handle the process failure instead.

Distributed programming

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).

Creating Processes

Erlang provides Built-In Functions (BIFs) 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 Datatype

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.

print() ->
    io:format("I am process ~w~n", [self()]).
print(I) ->
    io:format("I am process ~w~n", [I]).
spawnPrint() ->
    spawn(?MODULE, print, []).
%or spawn(fun print/0).
spawnPrintN(0) ->
spawnPrintN(N) when is_integer(N), N > 0 ->
    spawn(?MODULE, print, [N]),
    spawnPrintN(N - 1).
spawnSpawn(0) ->
    io:format("I am a 'lief'!~n"),
spawnSpawn(N) when is_integer(N), N > 0 ->
    spawn(?MODULE, spawnSpawn, [N - 1]),
    spawnSpawn(N - 1).
spawnSlashOneA() ->
    spawn(fun print/0).
% spawning an anonynous function
spawnSlashOneB() ->
    spawn(fun() -> io:format("I am a process ~w~n!", [self()]) end).
spawnSort(List) ->
    {Even, Odd} = lists:partition(fun(I) -> I rem 2 == 0 end, List),
    spawn(?MODULE, sortPrint, ["Even", Even]),
    spawn(?MODULE, sortPrint, ["Odd", Odd]).
sortPrint(Print, List) ->
    io:format("~p sorted: ~w~n", [Print, lists:sort(List)]).
% run
1> c(spawning).
2> spawning:spawnPrint().
I am process <0.42.0>
3> spawning:spawnPrintN(3).
I am process 3
I am process 2
I am process 1
4> spawning:spawnSpawn(3).
I am a 'lief'!
I am a 'lief'!
I am a 'lief'!
I am a 'lief'!
I am a 'lief'!
I am a 'lief'!
I am a 'lief'!
I am a 'lief'!
5> spawning:spawnSlashOneA().
I am process <0.58.0>
6> spawning:spawnSlashOneB().
I am a process <0.60.0>
7> spawning:spawnSort([4,6,3,7,9,8,2,1]).
"Even" sorted: [2,4,6,8]
"Odd" sorted: [1,3,7,9]

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 (an 'EXIT' signal is propagated). 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.

Notice that linking can be done later, between two already running processes, using the BIF link/1. The difference is that the spawn_link executes spawning and linking atomically (either both operations succeed, or both fail).


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.

spawnLink() ->
    spawn(fun spawnLink_/0).
spawnLink_() ->
    spawn_link(?MODULE, abnormalExit, []),
justLoop() ->
abnormalExit() ->
% run
1> c(spawning).
2> Pid = spawning:spawnLink().  
3> erlang:is_process_alive(Pid).
4> erlang:is_process_alive(Pid).
5> erlang:is_process_alive(Pid).
=ERROR REPORT==== 7-May-2011::12:24:54 ===
Error in process <0.43.0> with exit value: {undef,[{erlang,kill,[not_
6> erlang:is_process_alive(Pid).

Other Processes’-related BIFs

There are several other BIFs related to processes. The following are some commonly used.


Called as error(Reason). Stops the execution of the calling process with the reason Reason, where Reason is any term. The actual exit reason will be {Reason, Where}, where Where is a list of the functions most recently called (the current function first). Since evaluating this function causes the process to terminate, it has no return value.


Called as exit(Reason). Stops the execution of the calling process with the exit reason Reason, where Reason is any term. Since evaluating this function causes the process to terminate, it has no return value.

exit/2 is called as exit(Pid, Reason). Sends an exit signal with exit reason Reason to the process Pid.

The following behavior apply if Reason is any term except normal or kill:

If Pid is not trapping exits, Pid itself will exit with exit reason Reason. If Pid is trapping exits, the exit signal is transformed into a message {'EXIT', From, Reason} and delivered to the message queue of Pid. From is the pid of the process which sent the exit signal.

If Reason is the atom normal, Pid will not exit. If it is trapping exits, the exit signal is transformed into a message {'EXIT', From, normal} and delivered to its message queue.

If Reason is the atom kill, that is if exit(Pid, kill) is called, an untrappable exit signal is sent to Pid which will unconditionally exit with exit reason killed.

1> Rec = fun(F) -> F(F) end.
2> Loop = fun() -> Rec(Rec) end.
3> Pid = spawn(Loop).
4> erlang:is_process_alive(Pid).
5> exit(Pid, no_reason).
6> erlang:is_process_alive(Pid).

Returns true if the argument is a pid, else false.

1> Pid = spawn(io, format, ["Hej~n"]).
2> is_pid(Pid).

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, is not exiting and has not exited. Otherwise, returns false.

13> is_process_alive(self()).

Transforms the input string to a pid. This BIF is intended to be used for debugging and not in application development.

4> Pid == list_to_pid("<0.37.0>").

Returns the textual representation of a pid. Again, this BIF is intended to be used for debugging only.

3> StringPid = pid_to_list(Pid).

Registers a process (or a port) with a name. This name can be later used to refer to the process.

7> ShellPid = self().         
8> register(shell, ShellPid).

Returns a list with the names of all registered processes.

9> registered().

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.

7> ShellPid = self().         

Sends a message to a process. We will see message sending in detail in the next post.


Sends a message after a given amount of time.


Called as throw(Any). A non-local return from a function. If evaluated within a catch, catch will return the value Any.

1> throw(noooooo).                      
** exception throw: noooooo
2> try throw(yeeees) catch _:_ -> ok end.
3> catch throw(yeeees).

Removes the link between two processes. Returns true even if there exist no link.


Called as unregister(Name). Removes the association between the Name and the process which it is associated with.

11> unregister(shell).
12> registered().

Called as whereis(Name). Returns the pid of the process that is register with the name Name.

10> whereis(shell).


Next post will be about message passing. After this post one should be able to unveil the “power of Erlang”.

Series NavigationIntroduction to Erlang : List ComprehensionIntroduction to Erlang : Message Passing

Leave a Reply


Posts’ Calendar
May 2011