Physical Clock Synchronization
Physical Clock Synchronization
In physical clock synchronization, All the computers will have their own clocks.
The physical clocks are needed to adjust the time of nodes. All the nodes in the system can share
their local time with all other nodes in the system.
The time will be set based on UTC (Universal Coordinate Timer).
The time difference between the two computers is known as “Time drift”. Clock drifts over the
time is known as “Skew”. Synchronization is necessary here.
Physical clocks: In physical synchronization, physical clocks are used to time stamp an event on
that computer.
If two events, E1 and E2, having different time stamps t1 and t2, the order of the event occurring
will be considered and not on the exact time or the day at which they are occur.
Several methods are used to attempt the synchronization of the physical clocks in Distributed
synchronization:
Rules
A logical clock must specify the following two rules to implement proper functionality:
Rule 1 determines how a local process updates its clock when an event occurs.
Rule 2 determines how a local process updates its global clock upon receiving a message
from another process to update its view of global time.
P1
P2 P3
C1=2
C2=3 C3=1
C(b)=2 C(m)=2
b
m c
There are three conditions in which we can say an event a happens before b:
A happens-before relation does not order all events. For instance, the events a and d are not
related by ->. Hence, they are concurrent. Such events are written as a || d.
Lamport clocks tag events in a distributed system and order them accordingly. We seek a clock
time C(a) for every event a. The clock condition is defined as follows:
Each process maintains an event counter. This event counter is the local Lamport clock.
The Lamport clock algorithm works in the following way:
Before the execution of an event, the local clock is updated. This can be explained by the
equation Ci = Ci+1, where i is the process identifier.
When a message is sent to another process, the message contains the process’ local clock,
Cm.
When a process receives a message m, it sets its local clock to 1+max(CI, Cm).
In the example above, all local clocks start from a value of 0. Before the execution of an
event, the local clock increments by 1. Notice that P2’s clock starts from 0, but on the
arrival of the message m1 from P1, it updates its clock in accordance with the third rule
mentioned above, i.e., 1+max(Ci, Cm) where Ci = 0 and Cm = 2.
Hence, P2’s final clock value is 3.
Note: d is an event on P3, so, C(d) = 1, where d is parallel to a.
Applications
It is used to resolve conflicts, track individual events, and take concurrency measures.
Logical time also has its application in debugging distributed systems. With all the
information about the causal relationship of events, it is easy to spot which event caused
the error.
Logical Clocks
This means that if one PC has a time 2:00 pm then every PC should have the same time which
is quite not possible. Not every clock can sync at one time. Then we can’t follow this method.
Method-2:
Another approach is to assign Timestamps to events.
Taking the example into consideration, this means if we assign the first place as 1, second
place as 2, third place as 3 and so on. Then we always know that the first place will always
come first and then so on. Similarly, If we give each PC their individual number than it will be
organized in a way that 1st PC will complete its process first and then second and so on.
BUT, Timestamps will only work as long as they obey causality.
What is causality ?
Causality is fully based on HAPPEN BEFORE RELATIONSHIP.
Taking single PC only if 2 events A and B are occurring one by one then TS(A) < TS(B). If
A has timestamp of 1, then B should have timestamp more than 1, then only happen before
relationship occurs.
Taking 2 PCs and event A in P1 (PC.1) and event B in P2 (PC.2) then also the condition
will be TS(A) < TS(B). Taking example- suppose you are sending message to someone at
2:00:00 pm, and the other person is receiving it at 2:00:02 pm.Then it’s obvious that
TS(sender) < TS(receiver).
Properties Derived from Happen Before Relationship –
Transitive Relation –
If, TS(A) <TS(B) and TS(B) <TS(C), then TS(A) < TS(C)
Causally Ordered Relation –
a->b, this means that a is occurring before b and if there is any changes in a it will surely
reflect on b.