Post on 04-Jun-2018
8/13/2019 L06 Synchro
1/8
1
06013214 (Distributed Systems)
Synchronization
Akharin Khunkitti
Faculty of Information Technology
KMITL
2
Topics
Introduction
Clock Synchronization
Logical Clocks
Election Algorithms
Mutual Exclusion
Summary
3
Introduction
How processes cooperate and synchronize withone another
Cooperate in granting each other temporaryexclusive access
Need to agree on the ordering of events
Discussion of issues Actual time
Relative ordering, rather than absolute time
Election algorithms for a coordinator
Distributed mutual exclusion to protect sharedresources against simultaneous access by multipleprocesses
4
Clock Synchronization When a process wants to know the time, it makes a system
call and the kernel tells it
In a centralized system, its easy to know the time value andorder of the times
In a distributed system, achieving agreement on time is nottrivial Example: UNIX make program on different machines
When each machine has its own clock, an event that occurred
after another event may nevertheless be assigned an earlier time.
8/13/2019 L06 Synchro
2/8
8/13/2019 L06 Synchro
3/8
9
Clock Synchronization Algorithms The goal is to keep time of all the machines together as well
as possible
Each machine is assumed to have a timer that causes asinterrupt H times a second The interrupt handler adds 1 to a software clock
Let Cp is the value of the clock on machine p
The UTC time is t
The perfect clock would be Cp(t) = t ;or dC/dt = 1
The relation between clock time and UTC when clocks tick at different rates.
10
Cristians Algorithm
Get CUTC from a time server
The correct time is CUTC + Message propagation time from server to client+ Servers internal processing time
The best estimate of the message propagation time is (T1-T0)/2 Correct time is CUTC + (T1-T0)/2
The best estimate of the one-way propagation time is (T1-T0-I)/2 Correct time is CUTC + (T1-T0-I)/2
Can be averaged to get a better value
Getting the current time from a time server.
11
The Berkeley Algorithm
a) The time daemon asks all the other machines for their clock valuesb) The machines answer
c) The time daemon tells everyone how to adjust their clock
12
Logical Clocks It is not essential that all machines time
agree with the real time as announced on theradio every hour
Logical clocks certain class of algorithms, it
is the internal consistency of the clocks, notwhether they are close to the real time
The agreement on the order in which eventsoccur
Need not be real time, or physical clocks
Lamport (1978) showed that although clocksynchronization is possible, it need not beabsolute
8/13/2019 L06 Synchro
4/8
13
Lamport Timestamps (1) To synchronize logical clocks, Lamport
defined a relation called happens-before If a and b are events in the same process, and a
occurs before b, then a -> b is true If a is the event of a message being sent by one
process, and b is the event of the message beingreceived by another process, then a -> b is alsotrue
Happen-before is a transitive relation
If a -> b and b -> c, then a -> c
Time can be measured for every event
If a -> b, then C(a) < C(b)
14
Lamport Timestamps (2)
a) Three processes, each with its own clock.
The clocks run at different rates
b) Lamports algorithm corrects the clocks
15
Election Algorithms Many Distributed algorithms require one process to
act as coordinator, initiator, or otherwise performsome special roles
Each process has a unique number E.g. network address
In general, election algorithms attempt to Locate the process with the highest process number, and
Designate it as coordinator
Assume that every process knows the processnumber of every other process Do not know which ones are currently up or down
The goal of an election algorithm is to ensure that When an election starts, it concludes with all processesagreeing on who the new coordinator is to be
16
The Bully Election Algorithm (1) Bully algorithm has 3 types of messages
ELECTION send to all higher-number processes
OK or Answer response of ELECTION message
COORDINATOR elected coordinator send to all runninglower-number processes
Any process can initiates an election By sending ELECTION message to all processes with
higher numbers
Try to find the running highest number process
A process, P, holds an election as follow; P sends an ELECTION message to all processes with
higher numbers
If no one responds, P wins the election and becomescoordinator
If one of the higher-ups answers, it takes over. Ps job isdone
8/13/2019 L06 Synchro
5/8
17
The Bully Election Algorithm (2)
The bully election algorithma) Process 4 holds an election
b) Process 5 and 6 respond, telling 4 to stop
c) Now 5 and 6 each hold an election
18
The Bully Election Algorithm (3)
d) Process 6 tells 5 to stop
e) Process 6 wins and tells everyone
19
A Ring Election Algorithm (1) Based on the use of a ring
Processes are physically or logically ordered Each process knows its successor
When any process notices that the coordinator is notfunctioning, it Builds an ELECTION message containing its own process number
Sends the message to its successor
It the successor is down, the sender skips over the successor, andgoes to the next member along the ring, or one after that until arunning process is located
At each step, the sender adds its own process number to the list in themessage => making itself a candidate to be elected as coordinator
The message gets back to the process that started it Recognizes the received message containing its own process number
Find who is the coordinator by looking in the list member with thehighest number
Send COORDINATOR message with coordinator (highest) number
When COORDINATOR message has circulated once, it is removed
Everyone knows who is the new coordinator, and goes back to work
20
A Ring Election Algorithm (2)
Election algorithm using a ring.
8/13/2019 L06 Synchro
6/8
8/13/2019 L06 Synchro
7/8
25
Mutual Exclusion:
A Distributed Algorithm (2)
a) Two processes want to enter the same critical region at thesame moment.
b) Process 0 has the lowest timestamp, so it wins.
c) When process 0 is done, it sends an OK also, so 2 can nowenter the critical region.
26
Mutual Exclusion:
A Token Ring Algorithm (1)
A logical ring is constructed in which each process isassigned a position in the ring Each process knows who is the next in line after itself
When the ring is initialized, a process is given a token The token circulates around the ring
When a process acquire the token, it checks to see if it isattempting to enter a critical region If so, the process enters the region, does al the work it needs to,
and leaves the region After it has exited, it passes the token along the ring
If not interested in entering a critical region, it just passes the tokenalong
If the token is lost, it must be regenerated Detecting lost token is difficult, since amount of time between
successive appearances of the token is unbounded => long usedby a process
27
Mutual Exclusion:A Token Ring Algorithm (2)
a) An unordered group of processes on a network.
b) A logical ring constructed in software.
28
Mutual Exclusion:A Comparison of the Three Algorithms
A comparison of three mutual exclusion algorithms.
Lost token,
process crash0 to n 11 to Token ring
Crash of any
process2 ( n 1 )2 ( n 1 )Distributed
Coordinator crash23Centralized
ProblemsDelay before entry
(in message times)
Messages per
entry/exitAlgori thm
8/13/2019 L06 Synchro
8/8
29
Summary Synchronization is all about doing the right things at the
right time
Processes on different machines have their own clocks
There are various ways to synchronize clock, but allmethods are essentially based on exchanging clock values Variation in communication delays and the way those variations are
dealt with, largely determine the accuracy of clock synchronizationalgorithms
In many cases, knowing the absolute time is not necessary Only want to know the correct order of related events at different
processes
Lamport showed that by introducing a notion of logical clocks When event a happened before b, C(a) < C(b)
Synchronization between processes often requires that oneprocess acts as a coordinator The decision is taken by means of election algorithms
Distributed mutual exclusion algorithms ensure that in adistributed collection of processes At most one process at a time has access to a shared resource
30
END
Questions?