Question 8 of 15 · CIE-1 QB · Set 1 · 7 marks
Open in ChatGPTWhat are Logical Clocks? Explain Lamport's logical clock algorithm with a suitable example. Explain Lamport logical clocks and derive the rules for updating timestamps during local events, message sending, and message receiving.
Introduction
Physical clocks on different machines drift, so two events on two machines cannot be ordered by their timestamps. Lamport's insight was that a distributed system needs only a partial order based on causality, and that a simple counter is enough to capture it.
The happened-before relation
a → b (a happened before b) if:
- a and b are in the same process and a occurs first, or
- a is the sending of a message and b is its receipt, or
- there is a c such that
a → candc → b(transitive).
If neither a → b nor b → a, the events are concurrent (a || b). Causal order is a partial order, not a total one.
Lamport's algorithm
Each process keeps a counter C. Three rules:
- Local event: before an event,
C = C + 1. - Sending a message: send the message with the current value of
C, i.e.(m, C). - Receiving a message with timestamp t:
C = max(C, t) + 1.
A timestamp is then the integer on each event, and for every a → b the algorithm guarantees C(a) < C(b).
Diagram

Worked example
- P1 sends m1 with timestamp 2.
- P2 receives it, so P2's next event is
max(0, 2) + 1 = 3. - P2 later sends m2 with timestamp 5.
- P1 receives it, so P1's next event is
max(3, 5) + 1 = 6. - P1's later event therefore carries 6, which is greater than every event it causally follows.
Limitation
- The converse is not true:
C(a) < C(b)does not provea → b. - Two concurrent events can receive any relative timestamps.
- So Lamport clocks cannot detect concurrency.
- Vector clocks fix this by keeping one counter per process.
Conclusion
Lamport clocks give a cheap, total timestamp order that respects causality and is enough for mutual exclusion and total-order multicast, at the cost of being unable to tell concurrent events apart.