0% found this document useful (0 votes)
33 views13 pages

Bengal College of Engineering and Technology, Durgapur

This document describes a project report submitted for the degree of Bachelor of Technology in Computer Science and Engineering. The report discusses the implementation of Lamport logical clocks using C/C++. Lamport logical clocks are extensively used in asynchronous distributed systems to determine the ordering of events. The report presents the algorithms and pseudocode to assign logical timestamps to events based on happened-before relationships. It also discusses some applications and limitations of Lamport logical clocks.

Uploaded by

Animesh Prasad
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)
33 views13 pages

Bengal College of Engineering and Technology, Durgapur

This document describes a project report submitted for the degree of Bachelor of Technology in Computer Science and Engineering. The report discusses the implementation of Lamport logical clocks using C/C++. Lamport logical clocks are extensively used in asynchronous distributed systems to determine the ordering of events. The report presents the algorithms and pseudocode to assign logical timestamps to events based on happened-before relationships. It also discusses some applications and limitations of Lamport logical clocks.

Uploaded by

Animesh Prasad
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/ 13

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)

Subject :- Distributed Operating System (CS-704A)

Under the Guidance of


Mr. Sovan Bhattacharya
Assistant Professor, BCET
ABSTRACT
Logical clocks are extensively used in asynchronous distributed systems. This
project shows, “how does the logical clock work?” To address this problem,
the paper motivates and proposes logical clocks of arbitrary dimensions. It
then identifies and explores the conceptual link between such clocks and
knowledge. It establishes the necessary and sufficient conditions on the size
and dimension of clocks required to attain any specified level of knowledge
about the timestamp of the most recent system state for which this is possible
without using any messages in the clock protocol. The project then gives
algorithms to determine the timestamp of the latest system state about which a
specified level of knowledge is attainable in a given system state, and to
compute the timestamp of the earliest system state in which a specified level of
knowledge about a given system state is attainable. The results are applicable
to applications that deal with a certain class of properties, identified as
monotonic properties.
TABLE OF CONTENTS
ABSTRACT …………………………………………………………………...

ACKNOWLEDGEMENT …………………………………………………... iii

1. INTRODUCTION ………………………………………………………….

2. TIMING MECHANISM …………………………………………………...

3. LAMPORT LOGICAL CLOCK …………………………………………...

4. IMPLEMENTATION OF LAMPORT CLOCK USING C/C++ ……………

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.

3. LAMPORT LOGICAL CLOCK


Lamport algorithm is a simple algorithm in order to determine the logical
ordering of events in a distributed system. With this algorithm, we can
determine happened-before relationships with minimal overheads.
A counter is kept in each node of a system. The algorithm works based on the
following rules :-
 Increment the counter for each internal event.
 When a node sends a message, it increases its counter by 1 and include
the new timestamp in the message
 When a node receives a message, it adopts either the local counter or the
timestamp in the message as the new local counter, depends on
whichever is larger. The local counter is then incremented by 1 to
represent the event of receiving the message.
Fig. 1 : Illustration on how Lamport clocks work.
Pseudocode for sending a message:

local_time += 1

send(message, local_time)

Pseudocode for receiving a message:

receive(message, timestamp)

local_time = max(timestamp, local_time) + 1

By comparing the timestamp of the events, we can define



happened-before ( ) relationships as the following:
 On the same node, a → b if and only if C(a) < C(b)
 A node sends a message → another node receives the message
 Relationship is transitive: if a → b and b → c, then a → c
If two nodes have never communicated with each other, not even through any
third parties, the ordering of the events between them cannot be determined.
They are said to be concurrent events. Yet, if they do not exchange message,
then they probably do not need a shared common clock. Therefore, only partial
ordering can be reached with Lamport Clock. That is, if a → b, then C(a) <
C(b), but the other way (saying if C(a) < C(b) then a → b) does not always
hold. Just by having the Lamport timestamps we cannot determine if there
exist a casual happened-before relationship. A strong clock consistency
condition cannot be achieved with Lamport Clock.
4. IMPLEMENTATION OF LAMPORT LOGICAL CLOCK
USING C/C++

#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

 Lamport proposes this pioneering and very simple algorithm for


synchronization based on the reconsideration of clock in distributed
system.
 It is widely used in current synchronized distributed systems.

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

You might also like