Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock...

26
Synchronization Chapter 5

Transcript of Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock...

Page 1: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Synchronization

Chapter 5

Page 2: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Chapter Outline

• Clock Synchronization– Physical Clocks– Clock synchronization algorithms

• Logical Clocks– Lamport’s logical clock– Vector clocks

• Mutual Exclusion– A centralized algorithm– A decentralized algorithm– A distributed algorithm– A token ring algorithm

• Election Algorithms– Bully algorithm– Ring algorithm

Page 3: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Why do we need to synchronize clocks?

When each machine has its own clock, an event that occurred after another event may nevertheless be assigned an earlier time.

Page 4: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Why do we need to synchronize clocks?

Inconsistency due to incorrect ordering of events

Page 5: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Clock Synchronization Algorithms

The relation between clock time and UTC when clocks tick at different rates.

Page 6: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Network Time Protocol

Getting the current time from a time server.

B T2 T3

A T1 T4

DTreq DTres

Offset θ = T3 + ((T2 – T1) + (T4 – T3))/2 – T4 = ((T2 – T1) + (T3-T4))/2

Page 7: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

The Berkeley Algorithm

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 clock

Page 8: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Logical Clocks

– Sufficient for two processes to agree on the current time without the time being the same as the real time

– If two processes do not interact, it is not necessary for their clocks to be synchronized

– For synchronization, usually the ordering of events is more relevant than an agreement on time

Page 9: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Lamport’s Logical Clocks (1)

– The events are logically ordered by defining a relation happens-before as follows:

– 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 being received by another process, then a →b is also true.

– A message cannot be received before it is sent, or even at the same time it is sent.

Page 10: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Lamport’s Logical Clocks (2)

For implementing Lamport’s logical clock, each process Pi maintains a local counter Ci. These counters are updated as follows:

1. Before executing an event Pi executes Ci →Ci + 1

2. When process Pi sends a message m to Pj it sets m’s

timestamp ts(m) equal to Ci after having executed the

previous step.

3. Upon receipt of a message m, process Pj adjusts its own local

counter as Cj →max{Cj, ts(m)}, after which it then executes

the first step and delivers the message to the application

Page 11: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Lamport’s Logical Clocks (3)

0

6

12

18

24

30

36

42

48

54

60

66

0

8

16

24

32

40

48

56

64

72

80

88

0

10

20

30

40

50

60

70

80

90

100

110

0

6

12

18

24

30

36

42

48

70

76

82

0

8

16

24

32

40

48

61

69

77

85

93

0

10

20

30

40

50

60

70

80

90

100

110

a) Three processes each with its own clock

b) Clocks after applying Lamport’s algorithm

Page 12: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Example: Totally-Ordered Multicasting

Updating a replicated database and leaving it in an inconsistent state.

Page 13: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Why Vector Clocks?

0

6

12

18

24

30

36

42

48

70

76

82

0

8

16

24

32

40

48

61

69

77

85

93

0

10

20

30

40

50

60

70

80

90

100

110

Concurrent message transmission using logical clocks

m1

m2

m3

m4

m5

Page 14: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Vector Clocks (1)

– Vector clocks capture causality– A vector clock VC(a) assigned to an event a has the

property that if VC(a) < VC(b) for some event b, then event a is known to causally precede b.

– Vector clocks are implemented by letting each process Pi maintain a vector VCi With the following properties:

– VCi[i] is the number of events that have occurred so far at Pi.

– If VCi[j] = k then Pi knows that k events have occurred at Pj.

Page 15: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Vector Clocks (2)

– Vector clocks that satisfying the two properties are implemented as follows:

• Before executing an event Pi executes VCi → VCi + 1

• When process Pi sends a message m to Pj it sets m’s vector timestamp ts(m) equal to VCi after having executed the previous step.

• Upon receipt of a message m, process Pj adjusts its own local counter as VCj →max{VCj, ts(m)[k]} for each k, after which it executes the first step and delivers the message to the application

Page 16: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Enforcing Causal Communication– Messages are delivered to the application only when all messages

that causally precede it have been received as well– Weaker model than totally ordered multicasting– Clocks only adjusted only when sending and receiving messages

– While sending a message, a process Pi will only increment VCi[i] by 1

– While receiving a message m with timestamp ts(m), it only adjusts VCi[k] to max{VCi[k], ts(m)[k]} for each k

– When a process Pj receives a message m from Pi with vector timestamp ts(m), the message will be delivered to the application only if

• ts(m)[i] = VCj[i] + 1

• ts(m)[k] <= VCj[k] for all k ≠ i

Page 17: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Mutual Exclusion

Concurrent accesses to resources may result in the shared resource to be in an inconsistent stateMutual exclusive access may be required to prevent inconsistencies.Algorithms for mutual exclusion can be broadly classified intoToken-based: algorithms that use a token (i.e. a special message)Permission based: algorithms that require any process that needs access to a shared resource obtain the required permission from other processes

Page 18: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

A Centralized Algorithm

a) Process 1 asks the coordinator for permission to enter a critical region. Permission is grantedb) Process 2 then asks permission to enter the same critical region. The coordinator does not reply.c) When process 1 exits the critical region, it tells the coordinator, when then replies to 2

Page 19: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

A decentralized Algorithm

Assumptions:– Each resource is replicated n times.– Every replica has its own coordinator for controlling

concurrent access.– When a coordinator crashes it recovers but would have

forgotten the vote it gave before it crashedWhen a process wants to access the resource, it will simply

need to get a majority vote from m > n/2 coordinators.

Page 20: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

A Distributed Algorithm

a) Two processes want to enter the same critical region 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 enter the critical

region.

Page 21: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

A Token Ring Algorithm

a) An unordered group of processes on a network.

b) A logical ring constructed in software.

Page 22: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Comparison

A comparison of four mutual exclusion algorithms.

AlgorithmMessages per

entry/exitDelay before entry (in message times)

Problems

Centralized 3 2 Coordinator crash

Decentralized 3mk, k=1,2,… 2mStarvation, low efficiency

Distributed 2 ( n – 1 ) 2 ( n – 1 )Crash of any process

Token ring 1 to 0 to n – 1Lost token, process crash

Page 23: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

The Bully Algorithm (1)

The bully election algorithma) Process 4 holds an electionb) Process 5 and 6 respond, telling 4 to stopc) Now 5 and 6 each hold an election

Page 24: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

Bully Algorithm (2)

d) Process 6 tells 5 to stope) Process 6 wins and tells everyone

Page 25: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

A Ring Algorithm

Election algorithm using a ring.

Page 26: Synchronization Chapter 5. Chapter Outline Clock Synchronization –Physical Clocks –Clock synchronization algorithms Logical Clocks –Lamport’s logical.

References

Lecture slides of Distributed Systems principles and paradigms by Andrew Tannenbaum and Van Steen, Prentice Hall India, 2002.