3 Concurrent Programming

One of the main reasons for using Erlang instead of other functional languages is Erlang's ability to handle concurrency and distributed programming. By concurrency we mean programs which can handle several threads of execution at the same time. For example, modern operating systems would allow you to use a word processor, a spreadsheet, a mail client and a print job all running at the same time. Of course each processor (CPU) in the system is probably only handling one thread (or job) at a time, but it swaps between the jobs a such a rate that it gives the illusion of running them all at the same time. It is easy to create parallel threads of execution in an Erlang program and it is easy to allow these threads to communicate with each other. In Erlang we call each thread of execution a process.

(Aside: the term "process" is usually used when the threads of execution share no data with each other and the term "thread" when they share data in some way. Threads of execution in Erlang share no data, that's why we call them processes).

The Erlang BIF spawn is used to create a new process: spawn(Module, Exported_Function, List of Arguments) . Consider the following module:

-module(tut14). -export([start/0, say_something/2]). say_something(What, 0) -> done; say_something(What, Times) -> io:format("~p~n", [What]), say_something(What, Times - 1). start() -> spawn(tut14, say_something, [hello, 3]), spawn(tut14, say_something, [goodbye, 3]).
5> c(tut14). 6> tut14:say_something(hello, 3). hello hello hello done

We can see that function say_something writes its first argument the number of times specified by second argument. Now look at the function start . It starts two Erlang processes, one which writes "hello" three times and one which writes "goodbye" three times. Both of these processes use the function say_something . Note that a function used in this way by spawn to start a process must be exported from the module (i.e. in the -export at the start of the module).

9> tut14:start(). hello goodbye hello goodbye hello goodbye

Notice that it didn't write "hello" three times and then "goodbye" three times, but the first process wrote a "hello", the second a "goodbye", the first another "hello" and so forth. But where did the come from? The return value of a function is of course the return value of the last "thing" in the function. The last thing in the function start is:

spawn(tut14, say_something, [goodbye, 3]).

spawn returns a process identifier, or pid, which uniquely identifies the process. So is the pid of the spawn function call above. We will see how to use pids in the next example.

Note as well that we have used ~p instead of ~w in io:format . To quote the manual: "~p Writes the data with standard syntax in the same way as ~w, but breaks terms whose printed representation is longer than one line into many lines and indents each line sensibly. It also tries to detect lists of printable characters and to output these as strings".

3.2 Message Passing

In the following example we create two processes which send messages to each other a number of times.

-module(tut15). -export([start/0, ping/2, pong/0]). ping(0, Pong_PID) -> Pong_PID ! finished, io:format("ping finished~n", []); ping(N, Pong_PID) -> Pong_PID ! , receive pong -> io:format("Ping received pong~n", []) end, ping(N - 1, Pong_PID). pong() -> receive finished -> io:format("Pong finished~n", []); -> io:format("Pong received ping~n", []), Ping_PID ! pong, pong() end. start() -> Pong_PID = spawn(tut15, pong, []), spawn(tut15, ping, [3, Pong_PID]).
1> c(tut15). 2> tut15: start(). Pong received ping Ping received pong Pong received ping Ping received pong Pong received ping Ping received pong ping finished Pong finished

The function start first creates a process, let's call it "pong":

Pong_PID = spawn(tut15, pong, [])

This process executes tut15:pong() . Pong_PID is the process identity of the "pong" process. The function start now creates another process "ping".

spawn(tut15, ping, [3, Pong_PID]),

This process executes:

tut15:ping(3, Pong_PID)

is the return value from the start function.

The process "pong" now does:

receive finished -> io:format("Pong finished~n", []); -> io:format("Pong received ping~n", []), Ping_PID ! pong, pong() end.

The receive construct is used to allow processes to wait for messages from other processes. It has the format:

receive pattern1 -> actions1; pattern2 -> actions2; . patternN actionsN end.

Note: no ";" before the end .

Messages between Erlang processes are simply valid Erlang terms. I.e. they can be lists, tuples, integers, atoms, pids etc.

Each process has its own input queue for messages it receives. New messages received are put at the end of the queue. When a process executes a receive , the first message in the queue is matched against the first pattern in the receive , if this matches, the message is removed from the queue and the actions corresponding to the the pattern are executed.

However, if the first pattern does not match, the second pattern is tested, if this matches the message is removed from the queue and the actions corresponding to the second pattern are executed. If the second pattern does not match the third is tried and so on until there are no more pattern to test. If there are no more patterns to test, the first message is kept in the queue and we try the second message instead. If this matches any pattern, the appropriate actions are executed and the second message is removed from the queue (keeping the first message and any other messages in the queue). If the second message does not match we try the third message and so on until we reach the end of the queue. If we reach the end of the queue, the process blocks (stops execution) and waits until a new message is received and this procedure is repeated.

Of course the Erlang implementation is "clever" and minimizes the number of times each message is tested against the patterns in each receive .

Now back to the ping pong example.

"Pong" is waiting for messages. If the atom finished is received, "pong" writes "Pong finished" to the output and as it has nothing more to do, terminates. If it receives a message with the format:

it writes "Pong received ping" to the output and sends the atom pong to the process "ping":

Ping_PID ! pong

Note how the operator "!" is used to send messages. The syntax of "!" is:

Pid ! Message

I.e. Message (any Erlang term) is sent to the process with identity Pid .

After sending the message pong to the process "ping", "pong" calls the pong function again, which causes it to get back to the receive again and wait for another message. Now let's look at the process "ping". Recall that it was started by executing:

tut15:ping(3, Pong_PID)

Looking at the function ping/2 we see that the second clause of ping/2 is executed since the value of the first argument is 3 (not 0) (first clause head is ping(0,Pong_PID) , second clause head is ping(N,Pong_PID) , so N becomes 3).

The second clause sends a message to "pong":

Pong_PID ! ,

self() returns the pid of the process which executes self() , in this case the pid of "ping". (Recall the code for "pong", this will land up in the variable Ping_PID in the receive previously explained.)

"Ping" now waits for a reply from "pong":

receive pong -> io:format("Ping received pong~n", []) end,

and writes "Ping received pong" when this reply arrives, after which "ping" calls the ping function again.

ping(N - 1, Pong_PID)

N-1 causes the first argument to be decremented until it becomes 0. When this occurs, the first clause of ping/2 will be executed:

ping(0, Pong_PID) -> Pong_PID ! finished, io:format("ping finished~n", []);

The atom finished is sent to "pong" (causing it to terminate as described above) and "ping finished" is written to the output. "Ping" then itself terminates as it has nothing left to do.

3.3 Registered Process Names

In the above example, we first created "pong" so as to be able to give the identity of "pong" when we started "ping". I.e. in some way "ping" must be able to know the identity of "pong" in order to be able to send a message to it. Sometimes processes which need to know each others identities are started completely independently of each other. Erlang thus provides a mechanism for processes to be given names so that these names can be used as identities instead of pids. This is done by using the register BIF:

register(some_atom, Pid)

We will now re-write the ping pong example using this and giving the name pong to the "pong" process:

-module(tut16). -export([start/0, ping/1, pong/0]). ping(0) -> pong ! finished, io:format("ping finished~n", []); ping(N) -> pong ! , receive pong -> io:format("Ping received pong~n", []) end, ping(N - 1). pong() -> receive finished -> io:format("Pong finished~n", []); -> io:format("Pong received ping~n", []), Ping_PID ! pong, pong() end. start() -> register(pong, spawn(tut16, pong, [])), spawn(tut16, ping, [3]).
2> c(tut16). 3> tut16:start(). Pong received ping Ping received pong Pong received ping Ping received pong Pong received ping Ping received pong ping finished Pong finished

In the start/0 function,

register(pong, spawn(tut16, pong, [])),

both spawns the "pong" process and gives it the name pong . In the "ping" process we can now send messages to pong by:

pong ! ,

so that ping/2 now becomes ping/1 as we don't have to use the argument Pong_PID .

3.4 Distributed Programming

Now let's re-write the ping pong program with "ping" and "pong" on different computers. Before we do this, there are a few things we need to set up to get this to work. The distributed Erlang implementation provides a basic security mechanism to prevent unauthorized access to an Erlang system on another computer. Erlang systems which talk to each other must have the same magic cookie. The easiest way to achieve this is by having a file called .erlang.cookie in your home directory on all machines which on which you are going to run Erlang systems communicating with each other (on Windows systems the home directory is the directory where pointed to by the $HOME environment variable - you may need to set this. On Linux or Unix you can safely ignore this and simply create a file called .erlang.cookie in the directory you get to after executing the command cd without any argument). The .erlang.cookie file should contain one line with the same atom. For example, on Linux or Unix in the OS shell:

$ cd $ cat > .erlang.cookie this_is_very_secret $ chmod 400 .erlang.cookie

The chmod above make the .erlang.cookie file accessible only by the owner of the file. This is a requirement.

When you start an Erlang system which is going to talk to other Erlang systems, you must give it a name, e.g.:

$ erl -sname my_name

We will see more details of this later. If you want to experiment with distributed Erlang, but you only have one computer to work on, you can start two separate Erlang systems on the same computer but give them different names. Each Erlang system running on a computer is called an Erlang node.

(Note: erl -sname assumes that all nodes are in the same IP domain and we can use only the first component of the IP address, if we want to use nodes in different domains we use -name instead, but then all IP address must be given in full.)

Here is the ping pong example modified to run on two separate nodes:

-module(tut17). -export([start_ping/1, start_pong/0, ping/2, pong/0]). ping(0, Pong_Node) -> ! finished, io:format("ping finished~n", []); ping(N, Pong_Node) -> ! , receive pong -> io:format("Ping received pong~n", []) end, ping(N - 1, Pong_Node). pong() -> receive finished -> io:format("Pong finished~n", []); -> io:format("Pong received ping~n", []), Ping_PID ! pong, pong() end. start_pong() -> register(pong, spawn(tut17, pong, [])). start_ping(Pong_Node) -> spawn(tut17, ping, [3, Pong_Node]).

Let us assume we have two computers called gollum and kosken. We will start a node on kosken called ping and then a node on gollum called pong.

On kosken (on a Linux/Unix system):

kosken> erl -sname ping Erlang (BEAM) emulator version 5.2.3.7 [hipe] [threads:0] Eshell V5.2.3.7 (abort with ^G) (ping@kosken)1>
gollum> erl -sname pong Erlang (BEAM) emulator version 5.2.3.7 [hipe] [threads:0] Eshell V5.2.3.7 (abort with ^G) (pong@gollum)1>

Now we start the "pong" process on gollum:

(pong@gollum)1> tut17:start_pong(). true

and start the "ping" process on kosken (from the code above you will see that a parameter of the start_ping function is the node name of the Erlang system where "pong" is running):

(ping@kosken)1> tut17:start_ping(pong@gollum). Ping received pong Ping received pong Ping received pong ping finished

Here we see that the ping pong program has run, on the "pong" side we see:

(pong@gollum)2> Pong received ping Pong received ping Pong received ping Pong finished (pong@gollum)2>

Looking at the tut17 code we see that the pong function itself is unchanged, the lines:

 -> io:format("Pong received ping~n", []), Ping_PID ! pong,

work in the same way irrespective of on which node the "ping" process is executing. Thus Erlang pids contain information about where the process executes so if you know the pid of a process, the "!" operator can be used to send it a message if the process is on the same node or on a different node.

A difference is how we send messages to a registered process on another node:

We use a tuple instead of just the registered_name .

In the previous example, we started "ping" and "pong" from the shells of two separate Erlang nodes. spawn can also be used to start processes in other nodes. The next example is the ping pong program, yet again, but this time we will start "ping" in another node:

-module(tut18). -export([start/1, ping/2, pong/0]). ping(0, Pong_Node) -> ! finished, io:format("ping finished~n", []); ping(N, Pong_Node) -> ! , receive pong -> io:format("Ping received pong~n", []) end, ping(N - 1, Pong_Node). pong() -> receive finished -> io:format("Pong finished~n", []); -> io:format("Pong received ping~n", []), Ping_PID ! pong, pong() end. start(Ping_Node) -> register(pong, spawn(tut18, pong, [])), spawn(Ping_Node, tut18, ping, [3, node()]).

Assuming an Erlang system called ping (but not the "ping" process) has already been started on kosken, then on gollum we do:

(pong@gollum)1> tut18:start(ping@kosken). Pong received ping Ping received pong Pong received ping Ping received pong Pong received ping Ping received pong Pong finished ping finished

Notice we get all the output on gollum. This is because the io system finds out where the process is spawned from and sends all output there.

3.5 A Larger Example

Now for a larger example. We will make an extremely simple "messenger". The messenger is a program which allows users to log in on different nodes and send simple messages to each other.

Before we start, let's note the following:

We will set up the messenger by allowing "clients" to connect to a central server and say who and where they are. I.e. a user won't need to know the name of the Erlang node where another user is located to send a message.

%%% Message passing utility. %%% User interface: %%% logon(Name) %%% One user at a time can log in from each Erlang node in the %%% system messenger: and choose a suitable Name. If the Name %%% is already logged in at another node or if someone else is %%% already logged in at the same node, login will be rejected %%% with a suitable error message. %%% logoff() %%% Logs off anybody at that node %%% message(ToName, Message) %%% sends Message to ToName. Error messages if the user of this %%% function is not logged on or if ToName is not logged on at %%% any node. %%% %%% One node in the network of Erlang nodes runs a server which maintains %%% data about the logged on users. The server is registered as "messenger" %%% Each node where there is a user logged on runs a client process registered %%% as "mess_client" %%% %%% Protocol between the client processes and the server %%% ---------------------------------------------------- %%% %%% To server: %%% Reply stops the client %%% Reply logon was successful %%% %%% To server: %%% Reply: %%% %%% To server: %%% Reply: no reply %%% %%% To server: send a message %%% Reply: stops the client %%% Reply: no user with this name logged on %%% Reply: Message has been sent (but no guarantee) %%% %%% To client: , %%% %%% Protocol between the "commands" and the client %%% ---------------------------------------------- %%% %%% Started: messenger:client(Server_Node, Name) %%% To client: logoff %%% To client: %%% %%% Configuration: change the server_node() function to return the %%% name of the node where the messenger server runs -module(messenger). -export([start_server/0, server/1, logon/1, logoff/0, message/2, client/2]). %%% Change the function below to return the name of the node where the %%% messenger server runs server_node() -> messenger@bill. %%% This is the server process for the "messenger" %%% the user list has the format [,. ] server(User_List) -> receive -> New_User_List = server_logon(From, Name, User_List), server(New_User_List); -> New_User_List = server_logoff(From, User_List), server(New_User_List); -> server_transfer(From, To, Message, User_List), io:format("list is now: ~p~n", [User_List]), server(User_List) end. %%% Start the server start_server() -> register(messenger, spawn(messenger, server, [[]])). %%% Server adds a new user to the user list server_logon(From, Name, User_List) -> %% check if logged on anywhere else case lists:keymember(Name, 2, User_List) of true -> From ! , %reject logon User_List; false -> From ! , [ | User_List] %add user to the list end. %%% Server deletes a user from the user list server_logoff(From, User_List) -> lists:keydelete(From, 1, User_List). %%% Server transfers a message between user server_transfer(From, To, Message, User_List) -> %% check that the user is logged on and who he is case lists:keysearch(From, 1, User_List) of false -> From ! ;  -> server_transfer(From, Name, To, Message, User_List) end. %%% If the user exists, send the message server_transfer(From, Name, To, Message, User_List) -> %% Find the receiver and send the message case lists:keysearch(To, 2, User_List) of false -> From ! ; > -> ToPid ! , From ! end. %%% User Commands logon(Name) -> case whereis(mess_client) of undefined -> register(mess_client, spawn(messenger, client, [server_node(), Name])); _ -> already_logged_on end. logoff() -> mess_client ! logoff. message(ToName, Message) -> case whereis(mess_client) of % Test if the client is running undefined -> not_logged_on; _ -> mess_client ! , ok end. %%% The client process which runs on each server node client(Server_Node, Name) -> ! , await_result(), client(Server_Node). client(Server_Node) -> receive logoff -> ! , exit(normal); -> ! , await_result(); -> io:format("Message from ~p: ~p~n", [FromName, Message]) end, client(Server_Node). %%% wait for a response from the server await_result() -> receive -> % Stop the client io:format("~p~n", [Why]), exit(normal); -> % Normal response io:format("~p~n", [What]) end.

To use this program you need to:

In the following example of use of this program I have started nodes on four different computers, but if you don't have that many machines available on your network you could start up several nodes on the same machine.

We start up four Erlang nodes: messenger@super, c1@bilbo, c2@kosken, c3@gollum.

First we start up a the server at messenger@super:

(messenger@super)1> messenger:start_server(). true

Now Peter logs on at c1@bilbo:

(c1@bilbo)1> messenger:logon(peter). true logged_on