Home > DeveloperSection > Articles > Process in Erlang

Process in Erlang

erlang Concurrent Programming  OTP  erlang 
0 Comment(s)
 941  View(s)
Rate this:

Processes in Erlang:

Erlang is designed for massive concurrency. Erlang processes are lightweight (grow and shrink dynamically) with small memory footprint, fast to create and terminate, and the scheduling overhead is low. By concurrency is meant programs that can handle several threads of execution at the same time. It is easy to create parallel threads of execution in an Erlang program and to allow these threads to communicate with each other. In Erlang, each thread of execution is called a process.

How to create process in erlang:-

%% @author sunil

%% @doc @todo sample of erlang_process.



display_message(Message, 0) ->


display_message(Message, Times) ->

io:format("~p~n", [Message]),



spawn(?MODULE, display_message, [hi, 3]),

spawn(?MODULE, display_message, [bye, 3]).

output of the program









(test@localhost)40> finished


function display_message writes its first argument the number of times specified by second argument. The function start_process starts two Erlang processes, one that writes "hi" three times and one that writes "bye" three times. Both processes use the function display_message. Notice that a function used in this way by spawn, to start a process, must be exported from the module. spawn returns a process identifier, or pid (<0.409.0>), which uniquely identifies the process.

Message Passing Between Processes:

Messages between Erlang processes are simply valid Erlang terms. That is, they can be lists, tuples, integers, atoms, pids, and so on.

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 pattern are executed.

Syntax of sending message:-

Pid ! Message

Syntax of receving message:-

   pattern1 ->
   pattern2 ->

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 patterns to test. If there are no more patterns to test, the first message is kept in the queue and the second message is tried 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, the third message is tried, and so on, until the end of the queue is reached. If the end of the queue is reached, the process blocks (stops execution) and waits until a new message is received and this procedure is repeated.

Here is example of message passing :-

%% @author sunil

%% @doc @todo sample of erlang_process.


%% ====================================================================

%% API functions

%% ====================================================================


ping(0, Pong_PID) ->

Pong_PID ! finished,

io:format("ping finished~n", []);

ping(N, Pong_PID) ->

Pong_PID ! {ping, self()},


pong ->

io:format("Ping received pong~n", [])


ping(N - 1, Pong_PID).

pong() ->


finished ->

io:format("Pong finished~n", []);

{ping, Ping_PID} ->

io:format("Pong received ping~n", []),

Ping_PID ! pong,



start() ->

Pong_PID = spawn(?MODULE, pong, []),

spawn(?MODULE, ping, [3, Pong_PID]).

%% ====================================================================

%% Internal functions

%% ====================================================================



Pong received ping


Ping received pong

(test@localhost)41> Pong received ping

Ping received pong

Pong received ping

Ping received pong

ping finished

Pong finished

Don't want to miss updates? Please click the below button!

Follow MindStick