Lamport Algorithm
Lamport Algorithm
2. Theory
Introduction
Each process within a distributed system maintains its own local counter, which is referred to
as a logical clock. These logical clocks do not measure real-world physical time; instead, they
provide a sequential ordering for events, reflecting their causal relationships. The numerical
value assigned by a logical clock to an event is known as its timestamp.
Lamport’s Clock Algorithm, introduced by Leslie Lamport in 1978, is a logical clock
mechanism used in distributed systems to order events without relying on synchronized
physical clocks. In a distributed system, events occur across multiple processes, and es-
tablishing a consistent order is crucial for understanding causality. The algorithm assigns
timestamps to events based on logical clocks maintained by each process, ensuring that if
one event causally precedes another, its timestamp is less than that of the subsequent event.
The pivotal property that Lamport’s logical clocks are designed to satisfy is the Clock
Condition: If event ’a’ happened before event ’b’ (a → b), then the logical timestamp of
’a’ (C(a)) must be strictly less than the logical timestamp of ’b’ (C(b)). This condition
establishes a unidirectional causal guarantee. It ensures that if a causal relationship exists
between two events, their respective timestamps will accurately reflect that order. However,
the converse of the Clock Condition is not guaranteed. That is, if C(a) < C(b), it does not
necessarily imply that a → b.
3. Algorithm
1. Initialization: Each process Pi maintains a logical clock Ci , initialized to 0.
1
2. Internal Event at Pi :
• Increment Ci : Ci = Ci + 1.
• Assign Ci as the timestamp for the event.
• Increment Ci : Ci = Ci + 1.
• Assign Ci as the timestamp for the event.
• Include Ci in the message to Pj .
• Update Ci : Ci = max(Ci , T ) + 1.
• Assign Ci as the timestamp for the event.
5. Ordering: Events are ordered globally by their timestamps. Ties are resolved using
process IDs if needed.
4. Source Code
LamportClock.java
1 import java.util.*;
2
3 class Process {
4 private int processId;
5 private int logicalClock;
6 private List<String> eventLog;
7
2
16 logicalClock++;
17 String event = String.format("P%d: Local Event '%s' at time %d",
18 processId,
19 eventDescription,
20 logicalClock);
21 eventLog.add(event);
22 System.out.println(event);
23 }
24
38 return msg;
39 }
40
3
52 }
53
54 // Getters
55 public int getProcessId() { return processId; }
56 public int getLogicalClock() { return logicalClock; }
57 public List<String> getEventLog() { return eventLog; }
58
69 class Message {
70 private int senderId;
71 private int receiverId;
72 private String content;
73 private int timestamp;
74
85 // Getters
86 public int getSenderId() { return senderId; }
4
87 public int getReceiverId() { return receiverId; }
88 public String getContent() { return content; }
89 public int getTimestamp() { return timestamp; }
90
91 @Override
92 public String toString() {
93 return String.format("Message[from P%d to P%d: '%s' @ %d]",
94 senderId, receiverId, content, timestamp);
95 }
96 }
97
118 System.out.println();
119
5
122 p2.receiveMessage(msg1);
123
124 System.out.println();
125
129 System.out.println();
130
135 System.out.println();
136
141 p1.receiveMessage(msg3);
142 p1.receiveMessage(msg4);
143
144 System.out.println();
145
156 p1.displayEventLog();
6
157 p2.displayEventLog();
158 p3.displayEventLog();
159
7
192 // Display chronological order
193 System.out.println("Events in Logical Time Order:");
194 System.out.println("-".repeat(80));
195 for (EventInfo event : allEvents) {
196 System.out.printf("Time %d: %s%n",
197 event.timestamp,
198 event.description);
199 }
200
5. Output
8
9
6. Discussion
This lab involved implementing Lamport’s Logical Clock Algorithm in Java to simulate
the ordering of events in a distributed system. Through this exercise, we observed how
logical clocks enable coordination between processes without relying on physical time syn-
chronization. Each process in the simulation maintained its own logical clock, incrementing
it during internal events and message exchanges. The experiment clearly demonstrated the
importance of updating logical clocks based on received timestamps to maintain causal con-
sistency. Events were successfully timestamped and ordered in a way that preserved the
“happened-before” relationship.
7. Conclusion
In conclusion, this lab on implementation and analysis of Lamport’s Logical Clock Algorithm
reinforced the theoretical understanding of event ordering in distributed systems. This lab
underscored the significance of logical time in managing distributed processes, particularly
in environments where global time consensus is infeasible
10