37
OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

Embed Size (px)

Citation preview

Page 1: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Synchronization

•Chapter 5

Page 2: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Synchronization

• Processes need synchronization.– Access to resources,– Sharing printers– Ordering of events

• Sync based on the actual time.

• Relative ordering instead of absolute ordering.

• Election algorithms for group management.

• Distributed Mutual Exclusion

Page 3: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Clock Synchronization

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

Page 4: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Physical Clocks (1)• Counter & Holding register clock tick• Computation of the mean solar day.

Page 5: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Physical Clocks (2)

• TAI seconds are of constant length, unlike solar seconds. Leap seconds are introduced when necessary to keep in phase with the sun.

Page 6: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Clock Synchronization Algorithms• The relation between clock time and UTC when

clocks tick at different rates.

Page 7: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Clock Synchronization

• The clocks drift at most per time unit. So, we have

1- ≤ dC/ dt ≤ 1+ • If two clocks are drifting in opposite directions, at

a time t, they may be as much as 2 t apart.

• If the OS designers want to guarantee that two clocks ever differ by more than , the clocks must be re-synch at least every /2 seconds.

• Various algorithms …

Page 8: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Cristian's Algorithm

• It is assumed that a time server exists.• Getting the current time from a time server periodically,

certainly not more than every /2 seconds.• The points

– Time must never run backward gradual decrement or increment

– The propagation delay which is ≥ 0. This time is vary and depends on the network load. Cristian’s attempt is to measure the delay WITH the SAME clock. (the round trip of sending a query to the time server and getting reply = (T1 – T0)/2 or a better estimate ::

(T1 – T0 – I )/2, I is the interrupt handling time in the time server.

Page 9: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Cristian's Algorithm

Page 10: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

The Berkeley Algorithm

• In the Cristian’s algorithm, the time server was passive. Other machines periodically ask it for the time.

• In the Berekley Unix, the time server is polling every machine and checking its time. Based on the average of such times, it advises all to correct their time.

• Discarding faulty values from the both extremes.

Page 11: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

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 12: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Additional Clock Synch. Alg.

• Most widely used in the Internet is NTP (Network Time Protocol).

• It is now possible to have millions of clocks synch.• Use of Synch Clocks:

– Enforcing at-most-once message delivery to a server, even in the face of crashes.

– Conventional method: seq# and keeping all numbers; if a server crashes, it forgets everything!

– If we attach each message a timestamp, and we keep the most recent timestamp seen for each connection, the msg with less timestamp is regarded as duplicate and rejected.

Page 13: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Use of Synch Clocks

– Old timestamps in the table can be removed using a global variable:

• G = CurrentTime – MaxLifeTime – MaxClockSkew

– If an incoming msg has no correspondent connection and its time is more recent than G, it is accepted; otherwise it is rejected.

– In the case of server crash and reboot, G is reloaded from the time stored on disk and increments by the update period and again the same criterion rejects or accepts the incoming messages.

Page 14: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Logical Clocks

• Many cases need agreement on the same time, not exactly the actual time.

Logical clocks.• Lamport in 1978 introduced the non-absolute

clocks.• If two processes do not communicate, it is not

necessary to synch their clocks.• All processes do not agree on time, they agree on

the order in which events happen.• Base idea is the Happens-Before relation

Page 15: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Logical Clocks-2• A B means A happened before B• Two rules• 1- If A and B belong to the same process and A occurs

before B, then A B is true.• 2- If A is the event of sending m, and B is the event of

receipt of m by another process, then A B is true.• Happens-Before is a transitive relation.• If we do not have either A B or B A then A and B

are concurrent.• We need a way to measure the time for each event; so we

assign C(A) as the time of event A.

Page 16: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Logical Clocks-3

• A B implies that C(A) < C(B).• For local events, we have the above relation.• For the case of sending and receiving a message,

again we believe t hat we should have the above relation.

• The clock value C must go forward.• Correction of a time is achieved through adding a

positive number.• Lamport proposed how to assign time to events.

Page 17: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Lamport Timestamps

a) Three processes, each with its own clock. The clocks run at different rates.

b) Lamport's algorithm corrects the clocks.

Page 18: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Logical Clocks

• Rules for assignment of the time values:

If A happens before B in the same process, then C(A) < C(B)

If A and B represent the sending and receiving of a message, C(A) < C(B)

For distinctive events, C(A) is irrelevant to C(B)

Page 19: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Example: Totally-Ordered Multicasting

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

Page 20: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Totally-Ordered Multicasting

• Assume that msgs are multicast. When a process receives a msg, it broadcast an ack. Eventually all processes have the same queue, including msgs and acks.

• A process can deliver a queued msg to its application when it is acked by all processes.

• As all processes have the same queue, the order is guaranteed.

Page 21: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Vector Timestamp• Lamport clock guarantees a total order of events while they

the happened-before relation.– A B C(A) < C(B)

• However, if C(A) < C(B) then either A B or B A or A || B !!!

• What we have is the time values of the events, not their relations

• In fact, Lamport timestamps do not capture causality. If I have received an event, I am not sure if I have received its cause.

Page 22: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Vector Timestamp• Each event is assigned a VT(A) to en event A.• VT(A) < VT (B) A B• Example: chained e-mails or news

• Each process maintains a V as its vector clock.– Vi[i] is the # of events had occurred at Pi, so each event is

happened in Pi, Vi[i] is incremented.– Vi[j] is the # events occurred at Pj, based on Pi knowledge: this

property is maintained by piggybacking vectors along messages.

• vt (m) says how many events at other processes have taken place before Pi sent m. Thus causality of m to those events can be specified.

Page 23: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Vector Timestamp

• The definition of <

• Upgrade:– When Pi receives m, it adjusts its own vector:

• Vj[k] = max {Vj[k] , vt(m)[k]}

• Vi[i] is incremented by 1

• Msg is deliverable from Pj in Pk, iff– Vt( r)[j] = Vk[j] +1– Vt (r )[i] ≤ Vk[i] for all i≠ j.

Page 24: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Global State (1)

Relationship of cuts to vt

Page 25: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Global State (2)

a) Organization of a process and channels for a distributed snapshot

Page 26: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Global State (3)

b) Process Q receives a marker for the first time and records its local state

c) Q records all incoming messaged) Q receives a marker for its incoming channel and finishes recording

the state of the incoming channel

Page 27: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Application

• The termination detection

Page 28: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Election Algorithms

• Requirement for the coordinator.

• Assume that processes can be distinguished somehow; through their MAC address, …

• Assume that any process knows the process# of all other processes and if they are up or down currently.

• Various algorithms including Bully alg. By Garcia-Molina-1982.

• When a process notices that the coordinator no longer responding to requests, it initiates the alg.

Page 29: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Bully Alg.

1. P sends an ELECTION to all with higher id#

2. If no one responds, P wins the election and becomes the coordinator.

3. If one of the higher id#’s responds, it takes over.

• At any moment, a process can get an ELECTION msg from its lower id# colleagues.

• The winner announces its victory by sending a msg announcing its leadership.

• The biggest guy in town wins! Bully alg!

Page 30: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

The Bully Algorithm (2)

• The bully election algorithm• Process 4 holds an election• Process 5 and 6 respond, telling 4 to stop• Now 5 and 6 each hold an election

Page 31: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

The Bully Algorithm(3)

d) Process 6 tells 5 to stop

e) Process 6 wins and tells everyone

Page 32: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

A Ring Algorithm• This alg. is based on ring without token.• Assume that processes are physically or logically ordered.

Each process knows its successor.• To start, a process sends its id# inside the ELECTION msg

to its successor. If it is down, the next one is selected to send to.

• At each intermediate node, the list of id#s is expanded, so the new ring is introduced.

• When the initiator received the msg including its id#, it circulated the msg announcing itself as coordinator.

• Two concurrent initiation!!

Page 33: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

A Ring Algorithm

• Election algorithm using a ring.

Page 34: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Mutual Exclusion: 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 35: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

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 36: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

A Token Ring Algorithm

a) An unordered group of processes on a network. b) A logical ring constructed in software.Process 0 has the token. Token lost!!

Page 37: OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5

OS2- Sem1 83-84, R. Jalili

Comparison

• A comparison of three mutual exclusion algorithms.

AlgorithmMessages per entry/exit

Delay before entry (in message times)

Problems

Centralized 3 2 Coordinator crash

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

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