Chapter 6 Synchronization
Chapter 6 Synchronization
Chapter 6 Synchronization
Synchronization
1
Synchronization
•An important issue in distributed system is how processes cooperate
and synchronize with one another
– Cooperation is partially supported by naming, which allow them to share
resources
• Example of synchronization
– Access to shared resources
– Agreement on the ordering of events
• Will discuss
– Synchronization based on actual time
– Synchronization based on relative orders
– Algorithms for election of synchronization coordinators
2
Clock Synchronization
• When each machine has its own clock, an event that
occurred after another event may nevertheless be
assigned an earlier time
3
Physical Clock
• All computers have a circuit to keep track of time using a
quartz crystal
• However, quartz crystals at different computers often run at
a slightly different speed
– This leads to the so called clock skew between different machines
• Some systems (e.g., real-time systems) need external
physical clock
– Solar day: inter val between two consecutive noons
• Solar day varies due to many reasons
4
Global Positioning System
• Used to locate a physical point on earth
• Need at least 3 satellites to measure:
– Longitude, Latitude, and Altitude (height)
• Example: computing a position in a 2D space
5
GPS Challenges
• Clock skew complicates the GPS localization
– The receiver’s clock is generally not well synchronized
with that of a satellite
– E.g. 1 sec of clock offset could lead to 300,000 kilometers error in
distance estimation
• Other source of errors
– The position of satellite is not known precisely
– The receivers clock has a finite accuracy
– The signal propagation speed is not constant
– Earth is not a perfect sphere - need further correction
6
Clock Synchronization Algorithms
• The goal of synchronization is to
– keep all machines synchronized to an external reference clock, or
– just keep all machines together as well as possible
• The relation between clock time and UTC when clocks tick at
different rates
7
Network Time Protocol (NTP)
• Pairwise clock synchronization
– e.g., a client synchronize its clock with a ser ver
8
The Berkeley Algorithm
• Goal: just keep all machine together
• Steps
– The time daemon tell all machines its time
– Other machines answer how far ahead or behind
– The time daemon computes the average and tell other how to adjust
9
Clock Sync. in Wireless Networks
• In traditional distributed systems, we can deploy
many time ser vers
– that can easily contact each other for efficient
information dissemination
• However, in wireless networks, communication
becomes expensive and unreliable
• RBS (Reference Broadcast Synchronization) is a
clock synchronization protocol
– where a sender broadcasts a reference message that
will allow its receivers to adjust their clocks
10
Reference Broadcast Synchronization
11
Logical Clocks
• In many applications, what matters is not the real time
– It is the order of the events
12
Lamport’s Logical Clocks
13
Lamport’s Algorithm
• Each process Pi maintains a counter Ci
1. Before executing an event P executes C ← C + 1.
i i i
14
Application of Lamport’s Algorithm
Updating a replicated database and
leaving it in an inconsistent state.
15
Totally Ordered Multicasting
• Apply Lamport’s algorithm
– Every message is timestamped and the local counter is adjusted
according to every message
• Each update triggers a multicast to all ser vers
• Each ser ver multicasts an acknowledgement for every
received update request
• Pass the message to the application only when
– the message is at the head of the queue
– all acknowledgements of this message has been received
• The above steps guarantees that the messages are in the
same order at every ser ver, assuming
– message transmission is reliable
16
Problem with Lamport’s Algorithm
• Lamport’s algorithm guarantees that
– if event a happened before event b, then we have C(a)<C(b)
• However, this does not mean that
– C(a)<C(b) implies event a happened before event b
17
Vector Clocks (1)
• Vector clocks are constructed by letting each
process Pi maintain a vector VCi with the
18
Vector Clocks (2)
• Steps carried out to accomplish the property 2
of previous slide
– Before executing an event Pi executes VCi[i]←VCi[i]+1
18
Mutual Exclusion
• Concurrent access may corrupt the resource or make it
inconsistent
• Token-based approach for mutual exclusion
– only 1 token is passed around in the system
– Process can only access when it has the token
– Easy to avoid star vation and deadlock
– However, situation becomes complicated if token is lost
20
A Centralized Algorithm
•
Three steps:
(a) Process 1 asks the coordinator for resource. Permission is granted
(b) Process 2 asks the coordinator for resource. The coordinator does not reply
(c) When process 1 releases the resource, it notifies the coordinator. The
coordinator then grants permission to process 2
21
A Decentralized Algorithm
Whenever a process wants to access the resource, it will simply need to get a
majority vote from m > n/2 coordinators.
Voting System
21
A Distributed Algorithm
• When a process wants to access a shared resource, it builds a
messages containing:
– Name of the resource, Its process number, and the current time
22
An Example
(a) Two processes want to access a shared resource at the same 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 now go ahead.
23
Problems in the Distributed Algorithm
• Any process fails, the algorithm fail
– worse than the centralized algorithm
28
An Example (2)
The bully election algorithm. (d) Process 6 tells 5 to
stop. (e) Process 6 wins and tells everyone.
29
The Ring Algorithm
• No token is used in the algorithm
• Every process knows the ring structure
– i.e., how processes are arranged
31
Election in Wireless Networks
• Bully or Ring algorithms are not realistic in wireless
environments since
– they assume message passing is reliable which is not true in
wireless environments
32
An Example (1)
33
A n Example (2)
Election algorithm in a wireless network, with node a as the
source. (a) Initial network. (b)–(e) The build-tree phase
34
A n Example (3)
Election algorithm in a wireless network, with node a as the
source. (a) Initial network. (b)–(e) The build-tree phase
35