Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OS2- Sem1 83-84, R. Jalili Synchronization Chapter 5."— Presentation transcript:

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

2 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

3 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.

4 OS2- Sem1 83-84, R. Jalili Physical Clocks (1) Counter & Holding register  clock tick Computation of the mean solar day.

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.

6 OS2- Sem1 83-84, R. Jalili Clock Synchronization Algorithms The relation between clock time and UTC when clocks tick at different rates.

7 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 …

8 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.

9 OS2- Sem1 83-84, R. Jalili Cristian's Algorithm

10 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.

11 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

12 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.

13 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.

14 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

15 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.

16 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.

17 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.

18 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)

19 OS2- Sem1 83-84, R. Jalili Example: Totally-Ordered Multicasting Updating a replicated database and leaving it in an inconsistent state.

20 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.

21 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.

22 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. –V i [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.

23 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.

24 OS2- Sem1 83-84, R. Jalili Global State (1) Relationship of cuts to vt

25 OS2- Sem1 83-84, R. Jalili Global State (2) a)Organization of a process and channels for a distributed snapshot

26 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 message d)Q receives a marker for its incoming channel and finishes recording the state of the incoming channel

27 OS2- Sem1 83-84, R. Jalili Application The termination detection

28 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.

29 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!

30 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

31 OS2- Sem1 83-84, R. Jalili The Bully Algorithm(3) d)Process 6 tells 5 to stop e)Process 6 wins and tells everyone

32 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!!

33 OS2- Sem1 83-84, R. Jalili A Ring Algorithm Election algorithm using a ring.

34 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 granted b)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

35 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.

36 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!!

37 OS2- Sem1 83-84, R. Jalili Comparison A comparison of three mutual exclusion algorithms. Algorithm Messages per entry/exit Delay before entry (in message times) Problems Centralized32Coordinator crash Distributed2 ( n – 1 ) Crash of any process Token ring 1 to  0 to n – 1 Lost token, process crash


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

Similar presentations


Ads by Google