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.
- 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) ]
- Process: Pi
- Event: Eij, where i is the process in number and j: jth event in the ith process.
- 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.
- [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]
- 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]
- 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.
Below is the C program to implement Lamport’s Logical Clock:
e21 e22 e23 e11 0 0 0 e12 0 0 1 e13 0 0 0 e14 0 0 0 e15 0 -1 0 The time stamps of events in P1: 1 2 3 4 5 The time stamps of events in P2: 1 2 3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.