What s it or? Temporal ordering o events produced by concurrent processes Clock Synchronization Synchronization between senders and receivers o messages Coordination o joint activity Serialization o concurrent access or shared objects Logical clocks Assign sequence numbers to messages All cooperating processes can agree on order o events vs. physical clocks: o day Assume no central source Each system maintains its own local clock No total ordering o events No concept o happened-when Happened-beore Lamport s happened-beore notation a b e.g.: event a happened beore event b a: message being sent, b: receipt Transitive: i a b and b c then a c Logical clocks Assign value to each event i a b then clock(a) < clock(b) since cannot run backwards I a and b occur on dierent processes that do not exchange messages, then neither a b nor b a are true These events are concurrent Event counting example Three systems: P 0,, Events a, b, c, Local event counter on each system Systems occasionally communicate 1
Event counting example Event counting example a b 1 j 1 e 3 4 5 6 g h i 1 3 k a b 1 j 1 e 3 4 5 6 g h i 1 3 k Bad ordering: e h k Lamport s algorithm Each message carries a stamp o the sender s clock When a message arrives: i receiver s clock < message stamp set system clock to (message stamp + 1) else do nothing Lamport s algorithm Algorithm allows us to maintain ordering among related events Partial ordering clock must be advanced between any two events Event counting example a b 1 j 1 e 3 4 5 6 g h i 1 7 6 k 7 Summary Algorithm needs a monotonically increasing sotware counter. Incremented or each event o interest. Each such event has a Lamport stamp associated with it. For any two events, where a b: L(a) < L(b)
Problem 1: Identical stamps a b e 1 3 4 5 6 g h i 1 6 7 j k 1 7 a b, b c, : local events are sequenced i c, d, d g, : Lamport imposes a send receive relationship Concurrent events (e.g., a, i) may have the same stamp or not Creating unique stamps (total ordering) We can orce each stamp to be unique Deine global logical stamp (T i, i) T i represents local Lamport stamp i represents process number (globally unique) E.g. (host address, process ID) Compare stamps: (T i, i) < (T j, j) i and only i T i < T j or T i = T j and i < j Does not relate to event ordering Unique (totally ordered) stamps a b e 1.1.1 3.1 4.1 5.1 6.1 g h i 1. 6. 7. j 1.3 k 7.3 Problem : Detecting causal relations I L(e) < L(e ) Cannot conclude that e e Looking at Lamport stamps Cannot conclude which events are causally related Solution: use a vector clock Vector clocks Rules: 1. Vector initialized to 0 at each process V i [j] = 0 or i,j=1,, N. Process increments its element o the vector in local vector beore stamping event: V i [i] = V i [i] +1 3. Message is sent rom process P i with V i attached to it 4. When P j receives message, compares vectors element by element Sets local vector to higher o two values V j [i] = max(v i [i], V j [i]) or i=1,, N Comparing vector stamps Deine V=V i V [i] = V [i]) or i=1,, N V V i V [i] V [i]) or i=1,, N For any two events e, e i e e then V(e) < V(e ) Just like Lamport s algorithm i V(e) < V(e ) then e e Two events are concurrent i neither V(e) V(e ) nor V(e ) V(e) 3
a b e (1,0,0) a b e (1,0,0)(,0,0) a b e (1,0,0)(,0,0) a b (,1,0) e b (,0,0) b (,0,0) c (,1,0) (1,0,0)(,0,0) a b (,1,0)(,,0) e b (,0,0) c (,1,0) d (,,0) (1,0,0)(,0,0) a b (,1,0)(,,0) (0,0,1) e b (,0,0) c (,1,0) d (,,0) e (0,0,1) 4
(1,0,0)(,0,0) a b (,1,0)(,,0) (0,0,1) (,,) e (1,0,0)(,0,0) a b (,1,0)(,,0) (0,0,1) (,,) e b (,0,0) c (,1,0) d (,,0) e (0,0,1) (,,) b (,0,0) c (,1,0) d (,,0) e (0,0,1) (,,) concurrent events (1,0,0)(,0,0) a b (,1,0)(,,0) (0,0,1) (,,) e (1,0,0)(,0,0) a b (,1,0)(,,0) (0,0,1) (,,) e b (,0,0) c (,1,0) d (,,0) e (0,0,1) (,,) concurrent events b (,0,0) c (,1,0) d (,,0) e (0,0,1) (,,) concurrent events (1,0,0)(,0,0) a b (,1,0)(,,0) (0,0,1) (,,) e b (,0,0) c (,1,0) d (,,0) e (0,0,1) (,,) concurrent events Physical clocks 5
Logical vs. physical clocks Logical clock keeps track o event ordering among related (causal) events Useul to have the system keep good real Set to o day Consistent across systems A brie history o keeping Sundial Earliest orm o keeping Origin o dial Latin dies, medieval Latin dialis Divide daylight into equal segments Temporal (temporary) hours Summer hours were longer than winter hours! Water clock Best solution until perection o pendulum c. 1700 Problems: Hole should not get clogged or worn Outlow should not increase with the pressure o more water Calibration diicult Romans used sundial to calibrate water clocks Temporal hours Always 1 hours daylight Hourglass 8 th century Sand does not evaporate Useul in colder climates Sand does not reeze Columbus noted by a ½ hour hourglass Mechanical First need or mechanical Summon people or prayer Alarm clocks Early clocks were bells Mechanical indicators Single hour hand Clock dial invented in 1344 15 minute subdivisions Low accuracy Weight-driven clocks: c. 1400 Verge escapement or regulation 6
Better clocks 1510: Spring clock Slowed down when mainspring unwound Spiral pulley (usee) created in 155 to exert constant orce 1577: minute hand created Generally useless clocks not accurate enough 1656: Pendulum clock Christiaan Huygens 1 min/day; reined to <10 sec/day Minute hand used regularly c. 1680 Accurate Accurate clock needed or computing longitude at sea Use sextant or latitude Cannot use pendulum or clockwork mechanism 175: dead beat escapement Accurate to 1 second per day 1889:Accurate to 0.01 second/day 1761: marine chronometer with spring and balance wheel escapement Kept to 1/5 second per day on sea Determine longitude to within ½ degree 191: dual pendulum clock Accurate to several milliseconds per day Electrical clocks 1840, 1906: Battery Clocks sel-contained battery clock 1916: Synchronous Electric Motor Clock Based on requency o power source Quartz Clocks 1880: Piezoelectric eect Curie brothers Squeeze a quartz crystal & it generates an electric ield Apply an electric ield and it bends 199: Quartz crystal clock Resonator shaped like tuning ork Laser-trimmed to vibrate at 3,768 Hz Standard resonators accurate to 6 parts per million at 31 C Watch will gain/lose < ½ sec/day Stability > accuracy: stable to sec/month Good resonator can have accuracy o 1 second in 10 years Atomic clocks Second is deined as 9,19,631,770 periods o radiation corresponding to the transition between two hyperine levels o cesium-133 Accuracy: better than 1 second in six million years NIST standard since 1960 UTC UT0 Mean solar on Greenwich meridian Obtained rom astronomical observation UT1 UT0 corrected or polar motion UT UT1 corrected or seasonal variations in Earth s rotation UTC Civil measured on an atomic scale 7
UTC Coordinated Universal Time Temps Universel Coordonné Kept within 0.9 seconds o UT1 Atomic clocks cannot keep mean Mean is measure o Earth s rotation Physical clocks in computers CMOS clock (counter) circuit driven by a quartz oscillator battery backup to continue measuring when power is o OS generally programs a r circuit to generate an interrupt periodically e.g., 60 or 100 interrupts per second Programmable Interrupt Controller (PIC) Interrupt service procedure adds 1 to a counter in memory Problem Getting two systems to agree on Two clocks hardly ever agree Quartz oscillators oscillate at slightly dierent requencies Clocks tick at dierent rates Create ever-widening gap in perceived Clock Drit Dierence between two clocks at one point in Clock Skew Perect clock Computer s, C dc = 1 dt UTC, t Drit with slow clock Drit with ast clock Computer s, C skew dc < 1 dt Computer s, C dc > 1 dt skew UTC, t UTC, t 8
Dealing with drit Assume we set computer to true Not good idea to set clock back Illusion o moving backwards can conuse message ordering and sotware development environments Dealing with drit Go or gradual clock correction I ast: Make clock run slower until it synchronizes I slow: Make clock run aster until it synchronizes Dealing with drit OS can do this: Change rate at which it requests interrupts e.g.: i system requests interrupts every 17 msec but clock is too slow: request interrupts at (e.g.) 15 msec Adjustment changes slope o system : Linear compensating unction Compensating or a ast clock Computer s, C dc > 1 dt skew Clock synchronized Linear compensating unction applied UTC, t Compensating or a ast clock Computer s, C UTC, t dc < 1 dt Resynchronizing Ater synchronization period is reached Resynchronize periodically Successive application o a second linear compensating unction can bring us closer to true slope Keep track o adjustments and apply continuously e.g., UNIX adj system call 9
Getting accurate Attach GPS receiver to each computer ±1 mseco UTC Attach WWV radio receiver Obtain broadcasts rom Boulder or DC ± 3 msec o UTC (depending on distance) Attach GOES receiver ±0.1 mseco UTC Getting accurate Synchronize rom another machine One with a more accurate clock Machine that provides inormation Time Not practical solution or every machine Cost, size, convenience, environment RPC Simplest synchronization technique Issue RPC to obtain Set Does not account or network or processing latency what s the? 3:4:19 Cristian s algorithm Compensate or delays Note s: request sent: T 0 reply received: T 1 Assume network delays are symmetric request T reply T 0 T 1 Cristian s algorithm T request reply T 0 T 1 T 1 T0 = estimated overhead in each direction Error bounds I minimum message transit (T min ) is known: Place bounds on accuracy o result Client sets to: T1 T0 Tnew = T + 10
Error bounds request T min Earliest message arrives T reply T 0 T 1 T min range = T 1 -T 0 -T min T T accuracy o result = ± Latest message leaves 1 0 Tmin Cristian s algorithm: example Send request at 5:08:15.100 (T 0 ) Receive response at 5:08:15.900 (T 1 ) Response contains 5:09:5.300 (T ) Elapsed is T 1 -T 0 5:08:15.900-5:08:15.100 = 800 msec Best guess: stamp was generated 400 msec ago Set to T + elapsed 5:09:5.300 + 400 = 5:09.5.700 Cristian s algorithm: example I best-case message =00 msec T request reply T 0 T 1 00 00 800 T 0 = 5:08:15.100 T 1 = 5:08:15.900 T s = 5:09:5:300 T min = 00msec Berkeley Algorithm Gusella & Zatti, 1989 Assumes no machine has an accurate source Obtains average rom participating computers Synchronizes all clocks to average 900 100 800 Error = ± 00 = ± 00 = ± 00 Berkeley Algorithm Machines run dæmon Process that implements protocol One machine is elected (or designated) as the (master) Others are slaves Berkeley Algorithm Master polls each machine periodically Ask each machine or Can use Cristian s algorithm to compensate or network latency When results are in, compute average Including master s Hope: average cancels out individual clock s tendencies to run ast or slow Send oset by which each clock needs adjustment to each slave Avoids problems with network delays i we send a stamp 11
Berkeley Algorithm Berkeley Algorithm: example Algorithm has provisions or ignoring readings rom clocks whose skew is too great Compute a ault-tolerant average 3:00 3:5:50 9:10 I master ails Any slave can take over 3:5 :50 9:10 1. Request stamps rom all slaves Berkeley Algorithm: example Berkeley Algorithm: example 3:00 3:5:50 9:10-0:0 +0:15 3:00-6:05 +0.15 3:5 :50 9:10 3:5 :50 9:10. Compute ault-tolerant average: 3:5 + :50 + 3:00 = 3:05 3 3. Send oset to each Network Time Protocol, NTP 1991, 199 Internet Standard, version 3: RFC 1305 NTP Goals Enable s across Internet to be accurately synchronized to UTC despite message delays Use statistical techniques to ilter data and gauge quality o results Provide reliable service Survive lengthy losses o connectivity Redundant paths Redundant s Enable s to synchronize requently oset eects o clock drit Provide protection against intererence Authenticate source o data 1
NTP s Arranged in strata 1 st stratum: machines connected directly to accurate source nd stratum: machines synchronized rom 1 st stratum machines SYNCHRONIZATION SUBNET 1 3 4 NTP Synchronization Modes Multicast mode or high speed LANS Lower accuracy but eicient Procedure call mode Similar to Cristian s algorithm Symmetric mode Intended or master s Pair o s exchange messages and retain data to improve synchronization over All messages delivered unreliably with UDP NTP messages Procedure call and symmetric mode Messages exchanged in pairs NTP calculates: Oset or each pair o messages Estimate o oset between two clocks Delay Transmit between two messages Filter Dispersion Estimate o error quality o results Based on accuracy o s clock and consistency o network transit Use this data to ind preerred : lower stratum & lowest total dispersion NTP message structure Leap second indicator Last minute has 59, 60, 61 seconds Version number Mode (symmetric, unicast, broadcast) Stratum (1=primary reerence, -15) Poll interval Maximum interval between successive messages, nearest power o Precision o local clock Nearest power o NTP message structure Root delay Total roundtrip delay to primary source (16 bits seconds, 16 bits decimal) Root dispersion Nominal error relative to primary source Reerence clock ID Atomic, NIST dial-up, radio, LORAN-C navigation system, GOES, GPS, Reerence stamp Time at which clock was last set (64 bit) Authenticator (key ID, digest) Signature (ignored in SNTP) NTP message structure T 1 : originate stamp Time request departed ( s ) T : receive stamp Time request arrived at ( s ) T 3 : transmit stamp Time request let ( s ) 13
NTP s validation tests Timestamp provided last stamp received duplicate message? Originating stamp in message consistent with sent data Messages arriving in order? Timestamp within range? Originating and received stamps 0? Authentication disabled? Else authenticate Peer clock is synchronized? Don t sync with clock o higher stratum # Reasonable data or delay & dispersion SNTP Simple Network Time Protocol Based on Unicast mode o NTP Subset o NTP, not new protocol Operates in multicast or procedure call mode Recommended or environments where is root node and is lea o synchronization subnet Root delay, root dispersion, reerence stamp ignored RFC 030, October 1996 SNTP SNTP example request T 1 T T 3 reply T 4 request T =800 T 1 =1100 T 3 =850 reply T 4 =100 Roundtrip delay: d=(t 4 -T 1 )-(T -T 3 ) Time oset: (T T1 ) + (T3 = T ) 4 t Oset = ((800-1100)+(850-100))/ =((-300) + (-350))/ = -650/ = -35 Set to T 4 +t = 100-35 = 875 Time oset: (T T1 ) + (T3 = T ) 4 t Cristian s algorithm request T =800 T 3 =850 T 1 =1100 T s =85 reply T 4 =100 The End Oset = (100-1100)/ = 50 Set to T s + oset = 85+50 = 875 14