Вы находитесь на странице: 1из 34

CSC 3172

Synchronization in Distributed
Systems
Physical Clocks & Synchronization
• In a DS, each process has its own clock.
• Clock Skew versus Drift
• Clock Skew = Relative Difference in clock values of two processes
• Clock Drift = Relative Difference in clock frequencies (rates) of two processes

• A non-zero clock drift causes skew to increase (eventually).


• Maximum Drift Rate (MDR) of a clock
• Absolute MDR is defined relative to Coordinated Universal Time
(UTC). UTC is the “correct” time at any point of time.
• MDR of a process depends on the environment.

2
2
3
Drift rate, Clock skew, ……
What are these?
Drift rate ρ
Clock skew δ
Resynchronization
inInterval R

4
Question?
Is it possible in a Distributed System to
synchronize all the clocks to produce a single
unambiguous standard?

5
Clock synchronization using a time
server

mr

mt
p Time server,S

6
Universal Coordinated Time (UTC)

7
Clock Synchronization Algorithms
• Cristian’s Algorithm
• Berkeley Algorithm
• Lamport’s Algorithm

8
Cristian’s Algorithm
• Uses a time server to synchronize clocks
• Time server keeps the reference time (say
UTC)
• A client asks the time server for time, the
server responds with its current time T, and
the client uses this received value to set its
clock
• But network round-trip time introduces an
error…
9
9
Cristian’s Algorithm

• Each Computer periodically asks the “time server” for the


current time.
• The server responds with the current time C UTC
• The client sets its clock to C UTC
10
Problems
• Major Problem: If time from time server is less
than the client
– resulting in time running backwards on the client.
(which cannot happen)
• Minor Problem: results from the delay
introduced by the network request/response:
latency.
Best estimate (T1 - T0)/2
If the interrupt handling time, I is known, (T1 –
T0 – I)/2
11
Question 1
Suppose a client gets a timestamp of 4:12:30.500 from
a time server. The elapsed time between the request
and response was 20 msec. The current time on the
client when it receives the response is, 4:12:30.510.
Using Cristian’s algorithm, what should the client set its
clock to? ( 1 second = 1000 msec)

12
12
Question 2
A client attempts to synchronize its clock using
Cristian's method. It sends requests to three
different servers simultaneously, and records the
round-trip time and timestamp returned by each
server, as shown in the table below.

To achieve the best accuracy, which server should


the client synchronize with, and what time should it
set its software clock to?

13
Question 3
In an execution of Cristian’s synchronization
algorithm, the client sent a request at 7:25:56.700
(according to its own clock) and receives a response at
7:25:56.900. The server’s clock reading in the response
is 7:25:57.100. What should the client set its clock to?

14
14
• The time server (time daemon) polls every
machine periodically to ask their time.
• Based on the answers, it computes an
average time and tells all the other machines
to advance their clocks to the new time or
slow their clocks down until some specified
reduction has been achieved.

15
(a)The time daemon asks all the other machines for their clock
values
(b) The machines answer
(c) The time daemon tells everyone how to adjust their clocks 16
Berkeley Algorithm ………

17
Berkeley Algorithm ………

18
Question
Consider a network consisting of 5 computers, A (coordinator), B, C, D,
and E. At 14: 15, the coordinator decides to synchronize the clock of all
computers in the network. At that moment, just before the
synchronization, the clock of every computer in the network shows the
following.

Computer Clock
A 14:15
B 14:16
C 14:03
D 14:09
E 14:12

Apply the Berkley clock synchronisation algorithm to this situation, show the
stages of computation and indicate the outcome of the synchronisation.
Assume that the time needed for computation and for network communication
19
is negligible.
Lamport’s Algorithm
21
22
23
• Between every two events, the clock must
tick at least once.
• If a process sends or receives two messages in
quick succession, it must advance its clock by
(at least) one tick in between them.

24
Problem
Consider three processes which run on three
different machines each with its own clock at its
own speed as given below. Explain how Lamport’s
algorithm corrects the clocks.

25
Apply Lamport’s algorithm in the
following example

26
Problems with Lamport’s logical clocks
• Lamport’s logical clocks impose only a partial
order on the set of events; pairs of distinct
events generated different processes can have
identical timestamp.
• For certain applications a total ordering is
needed; they consider that no two events can
occur at the same time.
• In order to enforce total ordering a global
logical timestamp is introduced.

27
28
29
Vector clocks
• All processes use a vector of counters (logical clocks),
ith element is the clock value for process i, initially all
zero.
• Each process i increments the ith element of its vector
upon an instruction or send event. Vector value is
timestamp of the event.
• A send(message) event carries its vector timestamp
(counter vector)
• For a receive(message) event,

30
Vector clocks

31
• Causality
– If a->b then event a can affect event b
• Concurrency
– If neither a->b nor b->a then one event cannot
affect the other

32
Averaging Algorithms
• Decentralized algorithms
• Divide time into fixed length
resynchronization intervals.
• The ith interval starts at T0 + iR and runs until
T0 + (i + 1)R, where T0 is an agreed moment in
the past and R is a system parameter.
• At the beginning of each interval, each
machine broadcasts the current time
according to its clock.
33
Averaging Algorithms ….
• After a machine broadcasts its time, it starts a
local timer to collect all other broadcasts that
arrive during some interval S.
• When all the broadcasts arrive, an algorithm
is run to compute a new time from them.
- The simplest algorithm is just to average
the values from all the other machines.
- Discard the m highest and m lowest
values and average the rest.
34