0% found this document useful (0 votes)
35 views

Lamport's Algorithm For Mutual Exclusion in Distributed System

Lamport's algorithm is a distributed mutual exclusion algorithm proposed by Leslie Lamport. It uses logical clocks and timestamp ordering to coordinate processes requesting access to a critical section. Each process maintains a queue of requests from other processes and only enters the critical section when it has the smallest timestamp. When a process exits the critical section, it sends a release message to all other processes. The algorithm requires 3(N-1) messages per critical section execution.

Uploaded by

Nabonita Halder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lamport's Algorithm For Mutual Exclusion in Distributed System

Lamport's algorithm is a distributed mutual exclusion algorithm proposed by Leslie Lamport. It uses logical clocks and timestamp ordering to coordinate processes requesting access to a critical section. Each process maintains a queue of requests from other processes and only enters the critical section when it has the smallest timestamp. When a process exits the critical section, it sends a release message to all other processes. The algorithm requires 3(N-1) messages per critical section execution.

Uploaded by

Nabonita Halder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lamport’s Algorithm for Mutual

Exclusion in Distributed System

 Difficulty Level : Hard


 Last Updated : 14 Aug, 2019
Prerequisite: Mutual exclusion in distributed systems
Lamport’s Distributed Mutual Exclusion Algorithm is a permission based algorithm
proposed by Lamport as an illustration of his synchronization scheme for distributed
systems.
In permission based timestamp is used to order critical section requests and to resolve any
conflict between requests.
In Lamport’s Algorithm critical section requests are executed in the increasing order of
timestamps i.e a request with smaller timestamp will be given permission to execute
critical section first than a request with larger timestamp.
In this algorithm:
 Three type of messages ( REQUEST, REPLY and RELEASE) are used and
communication channels are assumed to follow FIFO order.
 A site send a REQUEST message to all other site to get their permission to enter
critical section.
 A site send a REPLY message to requesting site to give its permission to enter the
critical section.
 A site send a RELEASE message to all other site upon exiting the critical section.
 Every site Si, keeps a queue to store critical section requests ordered by their
timestamps.
request_queuei denotes the queue of site Si
 A timestamp is given to each critical section request using Lamport’s logical
clock.
 Timestamp is used to determine priority of critical section requests. Smaller
timestamp gets high priority over larger timestamp. The execution of critical section
request is always in the order of their timestamp.
Algorithm:
 To enter Critical section:
 When a site Si wants to enter the critical section, it sends a request
message Request(tsi, i) to all other sites and places the request
on request_queuei. Here, Tsi denotes the timestamp of Site Si
 When a site Sj receives the request message REQUEST(tsi, i) from site Si,
it returns a timestamped REPLY message to site Si and places the request of
site Si on request_queuej
.
 To execute the critical section:
 A site Si can enter the critical section if it has received the message with
timestamp larger than (tsi, i) from all other sites and its own request is at the
top of request_queuei
 To release the critical section:
 When a site Si exits the critical section, it removes its own request from the
top of its request queue and sends a timestamped RELEASE message to all
other sites
 When a site Sj receives the timestamped RELEASE message from site Si, it
removes the request of Si from its request queue
Message Complexity:
Lamport’s Algorithm requires invocation of 3(N – 1) messages per critical section
execution. These 3(N – 1) messages involves

 (N – 1) request messages
 (N – 1) reply messages
 (N – 1) release messages
Drawbacks of Lamport’s Algorithm:
 Unreliable approach: failure of any one of the processes will halt the progress of
entire system.
 High message complexity: Algorithm requires 3(N-1) messages per critical
section invocation.
Performance:
 Synchronization delay is equal to maximum message transmission time
 It requires 3(N – 1) messages per CS execution.
 Algorithm can be optimized to 2(N – 1) messages by omitting
the REPLY message in some situations.
 
India
Bangladesh
Canada
6.30pm
7pm
7 am

Lamport’s logical clock


Lamport’s Logical Clock was created by Leslie Lamport. It is a procedure to determine
the order of events occurring. It provides a basis for the more advanced Vector Clock
Algorithm. Due to the absence of a Global Clock in a Distributed Operating
System Lamport Logical Clock is needed.
Algorithm:
 Happened before relation(->): a -> b, means ‘a’ happened before ‘b’.
 Logical Clock: The criteria for the logical clocks are:
 [C1]: Ci (a) < Ci(b), [ Ci -> Logical Clock, If ‘a’ happened before ‘b’, then
time of ‘a’ will be less than ‘b’ in a particular process. ]
 [C2]: Ci(a) < Cj(b), [ Clock value of Ci(a) is less than Cj(b) ]

Reference:
 Process: Pi
 Event: Eij, where i is the process in number and j: j  event in the ith process.
th

 tm: vector time span for message m.


 Ci vector clock associated with process Pi, the jth element is Ci[j] and
contains Pi‘s latest value for the current time in process Pj.
 d: drift time, generally d is 1.
 (When your server’s time doesn’t match an authoritative time, such as ntp.org, you
have a problem called time drift. This problem is incredibly common and happens slowly
over weeks and months. To easily check the time drift, compare the server’s current date
and time to The Official NIST Time. Make sure you are comparing to the correct time zone)

Implementation Rules[IR]:
 [IR1]: If a -> b [‘a’ happened before ‘b’ within the same process] then, 
Ci(b)  =Ci(a) + d
 [IR2]: Cj = max(Cj, tm + d) [If there’s more number of processes, then tm = value of
Ci(a), Cj = max value between Cj and tm + d]
For Example:
 Take the starting value as 1, since it is the 1st event and there is no incoming value
at the starting point:
 e11 = 1
 e21 = 1
 The value of the next point will go on increasing by d (d = 1), if there is no
incoming value i.e., to follow [IR1].
 e12 = e11 + d = 1 + 1 = 2
 e13 = e12 + d = 2 + 1 = 3
 e14 = e13 + d = 3 + 1 = 4
 e15 = e14 + d = 4 + 1 = 5
 e16 = e15 + d = 5 + 1 = 6
 e22 = e21 + d = 1 + 1 = 2
 e24 = e23 + d = 3 + 1 = 4
 e26 = e25 + d = 6 + 1 = 7
 When there will be incoming value, then follow [IR2] i.e., take the maximum
value between Cj and Tm + d.
 e17 = max(7, 5) = 7, [e16 + d = 6 + 1 = 7, e24 + d = 4 + 1 = 5, maximum
among 7 and 5 is 7]
 e23 = max(3, 3) = 3, [e22 + d = 2 + 1 = 3, e12 + d = 2 + 1 = 3, maximum
among 3 and 3 is 3]
 e25 = max(5, 6) = 6, [e24 + 1 = 4 + 1 = 5, e15 + d = 5 + 1 = 6, maximum
among 5 and 6 is 6]
Limitation:
 In case of [IR1], if a -> b, then C(a) < C(b) -> true.
 In case of [IR2], if  a -> b, then C(a) < C(b) -> May be true or may not be true.
// C program to illustrate the Lamport's

// Logical Clock

#include <stdio.h>

// Function to find the maximum timestamp

// between 2 events

int max1(int a, int b)

// Return the greatest of th two

if (a > b)

return a;

else

return b;

// Function to display the logical timestamp

void display(int e1, int e2,

int p1[5], int p2[3])

int i;

printf("\nThe time stamps of "

"events in P1:\n");
for (i = 0; i < e1; i++) {

printf("%d ", p1[i]);

printf("\nThe time stamps of "

"events in P2:\n");

// Print the array p2[]

for (i = 0; i < e2; i++)

printf("%d ", p2[i]);

// Function to find the timestamp of events

void lamportLogicalClock(int e1, int e2,

int m[5][3])

int i, j, k, p1[e1], p2[e2];

// Initialize p1[] and p2[]

for (i = 0; i < e1; i++)

p1[i] = i + 1;

for (i = 0; i < e2; i++)

p2[i] = i + 1;
for (i = 0; i < e2; i++)

printf("\te2%d", i + 1);

for (i = 0; i < e1; i++) {

printf("\n e1%d \t", i + 1);

for (j = 0; j < e2; j++)

printf("%d\t", m[i][j]);

for (i = 0; i < e1; i++) {

for (j = 0; j < e2; j++) {

// Change the timestamp if the

// message is sent

if (m[i][j] == 1) {

p2[j] = max1(p2[j], p1[i] + 1);

for (k = j + 1; k < e2; k++)

p2[k] = p2[k - 1] + 1;

// Change the timestamp if the

// message is reeived
if (m[i][j] == -1) {

p1[i] = max1(p1[i], p2[j] + 1);

for (k = i + 1; k < e1; k++)

p1[k] = p1[k - 1] + 1;

// Function Call

display(e1, e2, p1, p2);

// Driver Code

int main()

int e1 = 5, e2 = 3, m[5][3];

// message is sent and received

// between two process

/*dep[i][j] = 1, if message is sent

from ei to ej

dep[i][j] = -1, if message is received

by ei from ej

dep[i][j] = 0, otherwise*/
m[0][0] = 0;

m[0][1] = 0;

m[0][2] = 0;

m[1][0] = 0;

m[1][1] = 0;

m[1][2] = 1;

m[2][0] = 0;

m[2][1] = 0;

m[2][2] = 0;

m[3][0] = 0;

m[3][1] = 0;

m[3][2] = 0;

m[4][0] = 0;

m[4][1] = -1;

m[4][2] = 0;

// Function Call

lamportLogicalClock(e1, e2, m);

return 0;

Edge Chasing Algorithms


 Last Updated : 24 Jun, 2020

Deadlock might be a virtual disadvantage that may get up at some point of a community
of cooperating or competitive procedures. A deadlock is that state of affairs where a
group of methods are blocked as a result of every technique requiring a fixed process,
sources needed for its headaches and anticipating release of ultimate assets held by using
others in equal organization for that reason making it impossible for any of methods to
proceed.
Deadlock involves following elements :
1. Resource :
Operating gadget and useful resource manager system can be concerned in deadlock
scenarios.Finite numbers of assets are on market inside system.These resources are
distributed among a selection of competitive methods.
 Reusable sources –
A reusable aid is one that can be correctly used by using handiest one method at a
time and isn’t depleted by means of that use. Processes accumulate aid units that they
later unharness for application through different tactics.
Examples of reusable sources are embody processors, I/O channels, I/O devices,
number one and secondary memory, files, database, semaphores, etc.
 Expendable sources –
The expendable aid is one that can be created and destroyed. There’s no restriction on
quantity of expendable assets of a particular kind. Samples of expendable sources are
interrupts, indicators messages, and expertise in I/O buffers.A technique ought to
request a resource before victimizing it and need to unleash useful resource once
victimizing it. The quantity of assets requested won’t exceed complete wide variety of
assets on market within gadget.
2. Request :
Request may be understood as when any processor asks for use of any resource. If
request can’t be granted in real time, then requesting technique must wait till it will
accumulate sources.

3. Use :
This state arises when resources are allotted to the processor.
4. Release :
After performing required operation ,resources are set to free state which may be
concerned as release of resources.
Distributed Deadlock Detection Algorithms :
The algorithmic program will be initiated on every occasion when a way of deadlock is
pressured to attend. The algorithmic program could be initiated either by using native
web site of method or by using positioning anyplace method waits.Distributed situations
will be detected through taking a shot of device and examining it for condition of an
impasse.
Distributed deadlock detection algorithmic program can be divided into four classes as
follows:
 Path push-in –
Path info- dispatched to waiting node to blocking off node.
 Edge-chasing –
Probe message area unit dispatched on graph edge.
 Diffusion computation –
Echo messages location unit dispatched on graph edges.
 Global nation detection –
Sweep-out, sweep-in WFG creation and reduction.
Edge Chasing Algorithm :
 Edge chasing algorithm makes use of a unique message on every occasion,
impasse detection is initiated by procedure Pi and it’s miles being sent by means of
house web site of manner Pi to house web site of procedure Pk.
 A probe message travels along rims ofworldwide TWF graph. An impasse is
detected if a probe message returns to its initiating process.
Note –
A Boolean array, dependents, for each technique Pi , is maintained. If Pj is aware that Pi is
depending on it, dependent (j) is ready to true. Otherwise, it’s far false.
Example: Contemplate a system as given in Fig. If method P1 initiates situation detection
, it sends probe (1, 3, 4) to S2. Since P6 is awaiting P8 and P7 is awaiting P10. S2 sends probe
( 1, 6, 8) and (1, 7, 10) to S3 that successively sends probe ( 1, 9, 1) to S1. On receiving
probe (1, 9, 1), S1 declares that P1 is obstructed (deadlock).
If Pi is locally dependent on itself then declare deadlock
Else for all Pj and Pk such that
Pi is locally dependent upon Pj, and
Pj is waiting on Pk, and
Pj and Pk are on totally different sites
Send probe(i,j,k) to home site of P k
On receipt of probe(i, j,k), positioning takes subsequent actions.
If
Pk is blocked, and
dependentk(i) is false. and
Pk has not replied to any or all requests of P j
then
begin
dependentk(i)= true;
if k = i then declare that Pi is under deadlock
else for all Pm and Pn such that
Pk is locally dependent upon Pm, and
Pm is waiting on Pn and
Pm and Pn are on totally different sites,
send Prob(I,m,n) to house sites of P n
end
Solution of given example using Edge chasing algorithm :
1. P6 at beginning asks P8 for its public label and changes it’s own 2 to 3.
2. P3 asks P4 and changes its public label 1 to 3.
3. P9 asks P1 and finds its own public label 3 then detects situation.
4. P1 → P2 → P3 → P4 → P5 → P6 → P8 → P9 → P1.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory
concepts for SDE interviews with the CS Theory Course at a student-friendly price and
become industry ready.
 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy