# Dr. Kalpakis CMSC621 Advanced Operating Systems Synchronization.

## Presentation on theme: "Dr. Kalpakis CMSC621 Advanced Operating Systems Synchronization."— Presentation transcript:

Dr. Kalpakis CMSC621 Advanced Operating Systems Synchronization

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

CMSC 621 3 Physical Clocks Implemented via timers Counter + holding register with value H Clock tick = the interrupt generated every H oscillations of a quartz crystal Clock skew = difference in the time readings of different clocks

CMSC 621 4 Solar Time Transit of the sun = the event of the sun reaching the highest point in the horizon every day Solar day = time duration between two consecutive transits of the sun Solar second = 1/86400 th of the solar day the period of earths rotation is not constant Solar seconds get longer over time!

CMSC 621 5 Atomic Time Atomic clocks 1 second = the time it takes for the Cesium 133 atom to make 9,192,631,770 transitions Bureau International de LHeure averages the readings fo several atomic clocks to produce the International Atomic Time (TAI) TAI= #seconds since Jan 1, 1958. Since solar days get longer over time, keeping time more accurately with atomic clocks leads to problems Over time, noon will be occurring in the early morning hours!

CMSC 621 6 Atomic Time and leap seconds TAI seconds are of constant length, unlike solar seconds TAI and Solar time can get out of synch over time Leap seconds are introduced when necessary to keep in phase with the sun. Introduced whenever the difference between atomic time and solar time reaches 800 msecs Universal Coordinated Time (UTC) = TAI with leap seconds Total leap seconds in UTC is about 30

CMSC 621 7 UTC Servers Could also utilize the frequency of AC in power lines for keeping track of time NIST operates the WWV short-wave radio station that broadcasts a pulse at the beginning of each UTC second WWV has is accurate within 1 msec

CMSC 621 8 Measuring time with the Global Positioning System GPS uses 29 satellites with atomic clocks in an orbit of height about 20,000Km Each satellite broadcasts its location and its local atomic time Receivers can estimate their clock skew Ts since Distance from satellite I = speed of light x (T – Ti) Delay in getting msg from satellite I = (T-Ti) + Tdrift Using 4 satellites, receiver can find its location (x,y,z) and Tdrift by solving a system of 4 eqs. With 4 unknowns

CMSC 621 9 Physical Clock Synchronization Cp(t) = the local time at p when UTC is t Clock skew = Cp(t) -1 Clock offset wrt t = Cp(t) – t

CMSC 621 10 Network Time Protocol Estimating As clock offset relative to Bs clock A sends request to B and receives response from B Request and response are timestamped by the local clocks of A and B T1 = time req. is send by A T2 = time req. is received by B T3 = time response is send by B T4 = time response is received by A Assuming the propagation delays are symmetric (ie (T2-T1 = T4-T3), the offset of A relative to B is use the offset corresponding to the least delay among 8 such measurements between A and B If As clock is fast/slow, A slowdowns/fasten gradually over time

CMSC 621 11 Network Time Protocol Problem: possible that node with morew accurate clock adapts to a less accurate clock! Stratify the nodes Server with reference clock is at stratum 1 A node A adjuststs clock only when it has an offset from a node B with lower stratum, in which case stratum(A) becomes stratum(B)+1

CMSC 621 12 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

CMSC 621 13 Clock synchronization in wireless networks Reference Broadcasting Synchronization (RBS) Server queries nodes for their times to compute an offset for them Servers clock is left out of the synchronization Assumes no multihop routing Sender sends message Propagation delay is same for all receivers Nodes estimate their offsets from each other T(p,m) = time p receives m Relative offset of node p wrt to node q is Can also estimate relative offset via a linear regression

CMSC 621 14 Lamport Timestamps a) Three processes, each with its own clock. The clocks run at different rates. b) Lamport's algorithm corrects the clocks.

CMSC 621 15 Example: Totally-Ordered Multicasting Updating a replicated database and leaving it in an inconsistent state.

CMSC 621 16 Global State (1) a) A consistent cut b) An inconsistent cut

CMSC 621 17 Global State (2) a) Organization of a process and channels for a distributed snapshot

CMSC 621 18 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

CMSC 621 19 The Bully Algorithm (1) 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

CMSC 621 20 Global State (3) d) Process 6 tells 5 to stop e) Process 6 wins and tells everyone

CMSC 621 21 A Ring Algorithm Election algorithm using a ring.

CMSC 621 22 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

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

CMSC 621 24 A Toke Ring Algorithm a) An unordered group of processes on a network. b) A logical ring constructed in software.

CMSC 621 25 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 ring1 to 0 to n – 1 Lost token, process crash

CMSC 621 26 The Transaction Model (1) Updating a master tape is fault tolerant.

CMSC 621 27 The Transaction Model (2) Examples of primitives for transactions. PrimitiveDescription BEGIN_TRANSACTIONMake the start of a transaction END_TRANSACTIONTerminate the transaction and try to commit ABORT_TRANSACTIONKill the transaction and restore the old values READRead data from a file, a table, or otherwise WRITEWrite data to a file, a table, or otherwise

CMSC 621 28 The Transaction Model (3) a) Transaction to reserve three flights commits b) Transaction aborts when third flight is unavailable BEGIN_TRANSACTION reserve WP -> JFK; reserve JFK -> Nairobi; reserve Nairobi -> Malindi; END_TRANSACTION (a) BEGIN_TRANSACTION reserve WP -> JFK; reserve JFK -> Nairobi; reserve Nairobi -> Malindi full => ABORT_TRANSACTION (b)

CMSC 621 29 Distributed Transactions a) A nested transaction b) A distributed transaction

CMSC 621 30 Private Workspace a) The file index and disk blocks for a three-block file b) The situation after a transaction has modified block 0 and appended block 3 c) After committing

CMSC 621 31 Writeahead Log a) A transaction b) – d) The log before each statement is executed x = 0; y = 0; BEGIN_TRANSACTION; x = x + 1; y = y + 2 x = y * y; END_TRANSACTION; (a) Log [x = 0 / 1] (b) Log [x = 0 / 1] [y = 0/2] (c) Log [x = 0 / 1] [y = 0/2] [x = 1/4] (d)

CMSC 621 32 Concurrency Control (1) General organization of managers for handling transactions.

CMSC 621 33 Concurrency Control (2) General organization of managers for handling distributed transactions.

CMSC 621 34 Serializability a) – c) Three transactions T 1, T 2, and T 3 d) Possible schedules BEGIN_TRANSACTION x = 0; x = x + 1; END_TRANSACTION (a) BEGIN_TRANSACTION x = 0; x = x + 2; END_TRANSACTION (b) BEGIN_TRANSACTION x = 0; x = x + 3; END_TRANSACTION (c) Schedule 1x = 0; x = x + 1; x = 0; x = x + 2; x = 0; x = x + 3Legal Schedule 2x = 0; x = 0; x = x + 1; x = x + 2; x = 0; x = x + 3;Legal Schedule 3x = 0; x = 0; x = x + 1; x = 0; x = x + 2; x = x + 3;Illegal (d)

CMSC 621 35 Two-Phase Locking (1) Two-phase locking.

CMSC 621 36 Two-Phase Locking (2) Strict two-phase locking.

CMSC 621 37 Pessimistic Timestamp Ordering Concurrency control using timestamps.