articles

Home / DeveloperSection / Articles / Concurrent Programming in Erlang

Concurrent Programming in Erlang

Tarun Kumar2417 10-Mar-2016

Concurrency is the backbone of Erlang. Concurrency means the different functions executes parallel without affecting each other. Each and every concurrent activity in Erlang is called a process, and the only way for a process to interact with each other is through message passing, in that way the data is sent from one process to another process. The idea behind the Erlang and its concurrency model is best described by Joe Armstrong:

  • The world is concurrent.
  • Things in the world don't share data.
  • Things communicate with messages.
  • Things fail.


Erlang can handle hundreds of thousands, even millions, of processes in parallel with a small memory footprint. We can do same in C or Java, but we would struggle when scaling the system to hundreds of thousands of concurrent events. So Erlang is one of the most powerful concurrency models available today.

In order to control a set of parallel activities Erlang has primitives for multiprocessing: spawn BIF is used to start a parallel process and send a message to a process through send and receives messages through the receive. spawn will start the execution of a parallel process and returns an identifier that can be used to send messages and receive messages from the process.

Syntax of spawn method:

spawn(Module, Function, Arguments)

After executing this it will creates new process and returns process identifier, and

we refer to as a Pid. Parameters of spawn/3 BIF:

Module: For that spawn BIF will create new process.

Function: It is the exported function of the module.

Arguments: It is a list of arguments.

Example: spawn(module, function, [arguments]).

Syntax for send a message:
Pid ! Message.
Pid is a process identity and Message is the message which is to be sent to Pid.
Example: Pid ! {v1, 32}.
That means message {v1, 32} is sending to the process Pid. Before sending the
message all the arguments will be evaluate.

 Syntax for receive messages:
receive Message.

If the message {v1, 32} is sent from the sender side then it is received in Message, and after pattern matching will be done. 

For complete understanding we will create a simple echo process that will returns a message sent to it:

-module(foo).
-export([start/0, myfun/0]).
start() -> spawn(foo, loop, []).
myfun() ->  receive{From, Message} ->
                                     From ! Message, myfun()
                       end.

 In the above example spawn(foo, myfun, []) causes the function represented by foo:myfun() to be evaluated in parallel with the calling function. So the evaluation will be like this:

Pid1 = foo:start()
Pid1 ! {self(), hello}

 and the whole process doing is a parallel process. Process will start and message {self(), hello} will be sent to the process Pid, and self() BIF will returns the Pid (process identifier) of the current process.


Updated 15-Dec-2017

Leave Comment

Comments

Liked By