330 likes | 556 Views
第 4 讲 进程同步. §4.1 时钟同步 Clock Synchronization §4.2 逻辑时钟 Logical Clocks §4.3 互斥 Mutual Exclusion §4.4 选举 Leader Election. Synchronizing Processes. From communication to synchronization Time, order, etc. No global/universal information. §4.1Clock Synchronization.
E N D
第4讲 进程同步 §4.1 时钟同步 Clock Synchronization §4.2 逻辑时钟 Logical Clocks §4.3 互斥 Mutual Exclusion §4.4 选举 Leader Election
Synchronizing Processes • From communication to synchronization • Time, order, etc. • No global/universal information
§4.1Clock Synchronization Figure 6-1. When each machine has its own clock, an event that occurred after another event may nevertheless be assigned an earlier time
Clock Synchronization Algorithms Figure 6-5. The relation between clock time and UTC when clocks tick at different rates.
Network Time Protocol (NTP) Figure 6-6. Getting the current time from a time server.
The Berkeley Algorithm Figure 6-7. (a) The time daemon asks all the other machines for their clock values. Figure 6-7. (c) The time daemon tells everyone how to adjust their clock. Figure 6-7. (b) The machines answer.
Clock Synchronization in Wireless Networks Figure 6-8. (a) The usual critical path in determining network delays. Figure 6-8. (b) The critical path in the case of RBS.
Lamport’s Logical Clocks • The "happens-before" relation → • If a and b are events in the same process, and a occurs before b, then a → b is true. • If a is the event of a message being sent by one process, and b is the event of the message being received by another process, then a → b
Lamport’s Logical Clocks Figure 6-9. (a) Three processes with clocks at different rates. (b) Lamport’s algorithm corrects the clocks.
Lamport’s Logical Clocks • Updating counter Ci for process Pi • Before executing an event Pi executes Ci ← Ci + 1. • When process Pi sends a message m to Pj, it sets m’s timestamp ts (m) equal to Ci after having executed the previous step. • Upon the receipt of a message m, process Pjadjusts its own local counter as Cj ← max{Cj , ts (m)}, after which it then executes the first step and delivers the message to the application.
Lamport’s Logical Clocks Figure 6-10. The positioning of Lamport’s logical clocks in distributed systems.
Example: Totally Ordered Multicasting Figure 6-11. Updating a replicated database and leaving it in an inconsistent state. • Two copies of an account database • Two concurrent updates: • Update 1: depositing money • Update 2: computing interest
Example: Totally Ordered Multicasting • Solution: totally ordered multicasting • All the receivers deliver messages in the same order • Key points: • Logical time • Receive order deliver 1. Sending: multicast an app msg with timestamp, the logical time of sender. 2. Receiving: when a process receives an app. message, put it into a local queue, ordered according to its timestamp. 3. Ack: the receiver multicasts an ack. to all the other processes. (the timestamp of the app. message < the timestamp of ack.) 4. Deliver: a queued msg is delivered to app if: it is at the head of the queue and has been acknowledged by each other process.
Example: Totally Ordered Multicasting $100 1% Why logical time is necessary?
§4.3 Mutual Exclusion • Mutual exclusively access shared resource • Critical section • Correctness properties • Safety: at most one process in CS at any time • Liveness, fairness: each access request will be met • Mutual exclusion in single computer • Many mechanisms • Distributed mutual exclusion • Token vs. Permission • Centralized vs. Distributed
A Centralized Algorithm Figure 6-14. Example requests
A Decentralized Algorithm • Multiple coordinators • To enter CS, send request to all coordinators • Access is granted if a majority of coordinators say OK • Each coordinator can grant one process at any time
Distributed Algorithm • No centralized coordinator • Collaborative operation • Request –> Permission CS
Ricart and Agrawala's Algorithm • Want to enter CS? • Send request to all processes, carrying a timestamp • Timestamp: • Logical time of the request
Ricart and Agrawala's Algorithm Upon receiving a request: • Not in CS, and don’t want it: Send an OK to the requester. • In CS: Queues the request. • Waiting for permission: Compares the timestamps, and: Send an OK if the competitor wins; Queue the request otherwise.
Ricart and Agrawala's Algorithm Upon exiting CS: • Send OK to processes in request queue
Think it: • Ricart and Agrawala's algorithm is very costly in message. Can you improve it by reducing messages? • Is there any other problems in Ricart and Agrawala's algorithm?
A Token Ring Algorithm Figure 6-16. (a) An unordered group of processes on a network. (b) A logical ring constructed in software.
§4.4 Leader Election • Having a leader is necessary and helpful • Leader playing a special role • E.g. coordinator in mutual exclusion
The Bully Algorithm Figure 6-20. The bully election algorithm. (a) Process 4 holds an election. (b) Processes 5 and 6 respond, telling 4 to stop. (c) Now 5 and 6 each hold an election.
The Bully Algorithm Figure 6-20. The bully election algorithm. (d) Process 6 tells 5 to stop. (e) Process 6 wins and tells everyone.
A Ring Algorithm Figure 6-21. Election algorithm using a ring.
Elections in Wireless Environments Figure 6-22. Election algorithm in a wireless network, with node a as the source. (a) Initial network. (b)–(e) The build-tree phase
Elections in Wireless Environments • Figure 6-22. Election algorithm in a wireless network, with node a as the source. (a) Initial network. (b)–(e) The build-tree phase
Elections in Wireless Environments Figure 6-22. (e) The build-tree phase. (f) Reporting of best node to source.
A Summary • Concept of synchronization • Clock synchronization • Logical clocks • Mutual exclusion • Centralized vs. distributed • Token vs. permission • Leader election