370 likes | 508 Vues
Synchronization. Chapter 5. 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.
E N D
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 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. OS2- Sem1 83-84, R. Jalili
Physical Clocks (1) • Counter & Holding register clock tick • Computation of the mean solar day. 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. OS2- Sem1 83-84, R. Jalili
Clock Synchronization Algorithms • The relation between clock time and UTC when clocks tick at different rates. 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 … 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. OS2- Sem1 83-84, R. Jalili
Cristian's Algorithm 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. OS2- Sem1 83-84, R. Jalili
The Berkeley Algorithm • The time daemon asks all the other machines for their clock values • The machines answer • The time daemon tells everyone how to adjust their clock 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. 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. 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 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. 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. OS2- Sem1 83-84, R. Jalili
Lamport Timestamps • Three processes, each with its own clock. The clocks run at different rates. • Lamport's algorithm corrects the clocks. 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) OS2- Sem1 83-84, R. Jalili
Example: Totally-Ordered Multicasting • Updating a replicated database and leaving it in an inconsistent state. 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. 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. 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. 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. OS2- Sem1 83-84, R. Jalili
Global State (1) Relationship of cuts to vt OS2- Sem1 83-84, R. Jalili
Global State (2) • Organization of a process and channels for a distributed snapshot OS2- Sem1 83-84, R. Jalili
Global State (3) • Process Q receives a marker for the first time and records its local state • Q records all incoming message • Q receives a marker for its incoming channel and finishes recording the state of the incoming channel OS2- Sem1 83-84, R. Jalili
Application • The termination detection 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. OS2- Sem1 83-84, R. Jalili
Bully Alg. • P sends an ELECTION to all with higher id# • If no one responds, P wins the election and becomes the coordinator. • 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! 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 OS2- Sem1 83-84, R. Jalili
The Bully Algorithm(3) • Process 6 tells 5 to stop • Process 6 wins and tells everyone 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!! OS2- Sem1 83-84, R. Jalili
A Ring Algorithm • Election algorithm using a ring. OS2- Sem1 83-84, R. Jalili
Mutual Exclusion: A Centralized Algorithm • Process 1 asks the coordinator for permission to enter a critical region. Permission is granted • Process 2 then asks permission to enter the same critical region. The coordinator does not reply. • When process 1 exits the critical region, it tells the coordinator, when then replies to 2 OS2- Sem1 83-84, R. Jalili
A Distributed Algorithm • Two processes want to enter the same critical region at the same moment. • Process 0 has the lowest timestamp, so it wins. • When process 0 is done, it sends an OK also, so 2 can now enter the critical region. OS2- Sem1 83-84, R. Jalili
A Token Ring Algorithm • An unordered group of processes on a network. • A logical ring constructed in software. Process 0 has the token. Token lost!! OS2- Sem1 83-84, R. Jalili
Comparison • A comparison of three mutual exclusion algorithms. OS2- Sem1 83-84, R. Jalili