Bengal College of Engineering and Technology, Durgapur
Bengal College of Engineering and Technology, Durgapur
A
Project Report
on
“LAMPORT LOGICAL CLOCK”
Submitted in the Partial Fulfilment of the Requirement for the
Award of the Degree of
BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE AND ENGINEERING
Submitted by
SAJAN SUMAN (12500116031)
SNEH NAYAN (12500116017)
ANIMESH PRASAD (12500116113)
1. INTRODUCTION ………………………………………………………….
5. APPLICATIONS …………………………………………………………….
6. LIMITATIONS ……………………………………………………………... 6
7. CONCLUSION ……………………………………………………………... 7
8. REFERENCES ………………………………………………………………
8
ACKNOWLEDGEMENT
We gratefully acknowledge for the assistance cooperation guidance and
classification provided by our HOD Sir Prof. SK ABDUL RAHIM during the
development of our Mini project on “Lamport Logical Clock”. Our Extreme
gratitude to Mr. Sovan Bhattacharya who guided us throughout the project.
Without his Willing, Disposition, Spirit of accommodation, Frankness, Timely
clarification and above all faith in us.
His readiness to discuss all important matters at work deserves special attention.
We would also like to thanks to our parents and partners for their great effort
and cooperation to finalize this project.
1. INTRODUCTION
Lamport’s Mutual Exclusion in Distributed Systems defines an algorithm for
mutual execution of a critical section. This algorithm makes sure that no two
processes enter the critical section simultaneously. The decision is made based
on the timestamps which the protocol messages and application messages
carry. The basic concept of this algorithm is that if a process wants to enter the
critical section, it sends REQUEST to all the other process in the network and
waits for their REPLY. Once it receives the replies from all the other
processes, it enters the critical section if it is at the top of the queue else waits
till it reaches the top of the queue. When a process exits the critical section, it
sends a RELEASE message to all the other processes. When a process receives
a RELEASE message, it removes the respective REQUEST from its QUEUE,
in order to move the next REQUEST ahead. Thus, this condition makes sure
that only one process enters the critical section at a time. When a node
received an application message it increments its clock value and when it
receives a request message, it enters the request message into a queue and
sends corresponding reply message. On receiving the reply, the requesting
node checks if it has received all the replies, if yes then is it at the head of the
queue, if yes then the node enters critical section. The received replies are
stored in a Boolean array, which turns to false whenever it receives a reply
from the respective node. On exiting the critical section, the node broadcasts a
release message and all the processes remove the request from their queue. In
the second version, there is an optimization. It goes like this: when a process
receives a request, if it has already sent a message with clock value greater
than the request message, then it does not send a reply message. Similarly,
when a process sends a request message it also checks if it has received any
message with a timestamp higher than its request, if yes then that message is
considered as a reply and its Boolean value is set to false. Rests of the
operations are performed as it is. When a process enters critical section, it
creates a log file and writes about its entry in critical section and also about its
exit from critical section on exiting.
2. TIMING MECHANISM
In a single machine where all the components share the same bus, there isn’t
usually a problem to organize event as they are referring to the same CPU
time. However, in distributed system, in order to determine the ordering of
the events, it is much harder as different clocks on different machines have
different values. Also, the clocks may skew or have errors, and network
delays may change the ordering of the events. Therefore, we need to find out
mechanisms that can help us determine the orderings.
In different timing mechanisms, there except two concepts of time:
Physical Time : The time that we use in real world in the format of
years, months, days, hours, minutes and seconds. In computational
systems, we present it using an epoch: the number of seconds elapsed
since a predefined date and time. The Unix epoch is 00:00:00 of 1st
January, 2004.
Logical Time : Essentially a monotonically increasing counter that is
influenced by the events happening in the system, hence only logically
applicable to the participants in the network.
local_time += 1
send(message, local_time)
receive(message, timestamp)
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,k,p1[20],p2[20],e1,e2,dep[20][20];
printf("enter the events : ");
scanf("%d %d",&e1,&e2);
for(i=0;i<e1;i++)
p1[i]=i+1;
for(i=0;i<e2;i++)
p2[i]=i+1;
printf("enter the dependency matrix:\n");
printf("\t enter 1 if e1->e2 \n\t enter -1, if e2->e1 \n\t else enter 0 \n\n");
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++)
scanf("%d",&dep[i][j]);
}
for(i=0;i<e1;i++)
{
for(j=0;j<e2;j++)
{
if(dep[i][j]==1)
{ p2[j]=max(p2[j],p1[i]+1);
for(k=j;k<e2;k++)
p2[k+1]=p2[k]+1;3
}
if(dep[i][j]==-1)
{
p1[i]=max(p1[i],p2[j]+1);
for(k=i;k<e1;k++)
p2[k+1]=p1[k]+1;
}
}
}
printf("P1 : ");
for(i=0;i<e1;i++)
{
printf("%d",p1[i]);
}
printf("\n P2 : ");
for(j=0;j<e2;j++)
printf("%d",p2[j]);
return 0 ;
}
5. APPLICATIONS
6. LIMITATIONS
The communication to all other nodes need a lot of traffic load.
It cannot handle the failure of nodes, or disconnection of the network.
7. CONCLUSION
Logical clocks are used as a way to order the events in distributed systems.
Logical clocks are a set of functions, written as C(event), that map each event in
all the processes to the set of integers. Therefore they can be implemented using
counters with no actual timing mechanism. These counters have to satisfy one
condition. If a and b are two events in a system and a happened-before b then
C(a) has to be less than C(b). To hold this condition two cases can arises :
The two a and b events happen in the same process i, in this case the
process i increments the clock Ci between any two successive events so
Ci(a) < Ci(b).
The event a is sending the message m event from process i to process j so
in this case process i timestamps the message with its own logical clock
Tm=Ci(a).
When process j receives the message m, it sets its clock to the Cj(b) greater than
or equal to its present value and greater Tm. It worthy noting that changing the
logical clock C does not itself constitute an event.
8. REFERENCES
[1] Distributed operating Systems by Pradeep K. Sinha
[2] www.geeksforgeeks.org
[3] www.youtube.com/University Academy
[4] www.youtube.com/udacity
[5] www.wikipedia.com