articles

Home / DeveloperSection / Articles / Process in Erlang

Process in Erlang

Sunil Singh4112 21-Aug-2015

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.

-module(erlang_process).
-export([start_process/0,display_message/2]).
display_message(Message, 0) ->
io:format("finished~n");
display_message(Message, Times) ->
io:format("~p~n", [Message]),
display_message(Message,Times-1).
start_process()->
spawn(?MODULE, display_message, [hi, 3]),
spawn(?MODULE, display_message, [bye, 3]).
output of the program
erlang_process:start_process().
hi
bye
hi
bye
<0.409.0>
hi
bye
(test@localhost)40> finished
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:-

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


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.

-module(erlang_process).
%% ====================================================================
%% API functions
%% ====================================================================
-export([start/0,pong/0,ping/2]).
ping(0, Pong_PID) ->
Pong_PID ! finished,
io:format("ping finished~n", []);
ping(N, Pong_PID) ->
Pong_PID ! {ping, self()},
receive
pong ->
io:format("Ping received pong~n", [])
end,
ping(N - 1, Pong_PID).
pong() ->
receive
finished ->
io:format("Pong finished~n", []);
{ping, Ping_PID} ->
io:format("Pong received ping~n", []),
Ping_PID ! pong,
pong()
end.
start() ->
Pong_PID = spawn(?MODULE, pong, []),
spawn(?MODULE, ping, [3, Pong_PID]).
%% ====================================================================
%% Internal functions
%% ====================================================================



output:-

erlang_process:start().

Pong received ping

<0.415.0>

Ping received pong

(test@localhost)41> Pong received ping

Ping received pong

Pong received ping

Ping received pong

ping finished

Pong finished


Updated 01-Sep-2019

Leave Comment

Comments

Liked By