1 / 33

第 4 讲 进程同步

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

koto
Download Presentation

第 4 讲 进程同步

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第4讲 进程同步 §4.1 时钟同步 Clock Synchronization §4.2 逻辑时钟 Logical Clocks §4.3 互斥 Mutual Exclusion §4.4 选举 Leader Election

  2. Synchronizing Processes • From communication to synchronization • Time, order, etc. • No global/universal information

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

  4. Clock Synchronization Algorithms Figure 6-5. The relation between clock time and UTC when clocks tick at different rates.

  5. Network Time Protocol (NTP) Figure 6-6. Getting the current time from a time server.

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

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

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

  9. Lamport’s Logical Clocks Figure 6-9. (a) Three processes with clocks at different rates. (b) Lamport’s algorithm corrects the clocks.

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

  11. Lamport’s Logical Clocks Figure 6-10. The positioning of Lamport’s logical clocks in distributed systems.

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

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

  14. Example: Totally Ordered Multicasting $100 1% Why logical time is necessary?

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

  16. A Centralized Algorithm Figure 6-14. Example requests

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

  18. Distributed Algorithm • No centralized coordinator • Collaborative operation • Request –> Permission  CS

  19. Ricart and Agrawala's Algorithm • Want to enter CS? • Send request to all processes, carrying a timestamp • Timestamp: • Logical time of the request

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

  21. Ricart and Agrawala's Algorithm Upon exiting CS: • Send OK to processes in request queue

  22. 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?

  23. A Token Ring Algorithm Figure 6-16. (a) An unordered group of processes on a network. (b) A logical ring constructed in software.

  24. A Comparison of the Four Algorithms

  25. §4.4 Leader Election • Having a leader is necessary and helpful • Leader playing a special role • E.g. coordinator in mutual exclusion

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

  27. The Bully Algorithm Figure 6-20. The bully election algorithm. (d) Process 6 tells 5 to stop. (e) Process 6 wins and tells everyone.

  28. A Ring Algorithm Figure 6-21. Election algorithm using a ring.

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

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

  31. Elections in Wireless Environments Figure 6-22. (e) The build-tree phase. (f) Reporting of best node to source.

  32. A Summary • Concept of synchronization • Clock synchronization • Logical clocks • Mutual exclusion • Centralized vs. distributed • Token vs. permission • Leader election

  33. Homework Questions

More Related