1 / 66

More on Semaphores

More on Semaphores. Andy Wang Operating Systems COP 4610 / CGS 5765. The Pigeon Network Scenario. Pigeons are good message carriers Reasonably reliable Relatively fast for less developed rural areas Can sense magnetic field lines. Here is the Story…. There are two towns—Mars and Venus

inez
Download Presentation

More on Semaphores

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. More on Semaphores Andy Wang Operating Systems COP 4610 / CGS 5765

  2. The Pigeon Network Scenario • Pigeons are good message carriers • Reasonably reliable • Relatively fast for less developed rural areas • Can sense magnetic field lines

  3. Here is the Story… • There are two towns—Mars and Venus • Mars has all male pigeons • Venus has all female pigeons • Each town delivers messages to the other • By sending a pigeon through the shared flying path • And waiting for the same pigeon to fly back as an acknowledgement

  4. Here is the Story… • Based on experience • Whenever both towns send messages simultaneously, the reliability drops significantly • Pigeons of opposite genders decide to take excursions • Goals of a pigeon network: • Efficiency • Fairness

  5. Developing the Solution • Can we map it to already solved problems? • Standard synchronization problems: • Bounded buffer (producers and consumers) • Fairness (readers and writers) • Resource allocation (dining philosophers) • Pigeon network is under the reader-writer category

  6. flying path Venus Mars Step 1: Visualization • Identify • Shared resources • Scope of shared resources

  7. flying path Venus Mars Step 1: Visualization • Identify • Shared resource: flying path • Scope of the shared resource: global

  8. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  9. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  10. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path something you don’t see everyday… Venus Mars Simplest Implementation

  11. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  12. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  13. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  14. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  15. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  16. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); P(flyingPath); // send the message V(flyingPath); flying path Venus Mars Simplest Implementation

  17. Simplest Implementation + Simple + Fair - Not efficient • Only one pigeon can fly at a time • Venusians love to fly in groups…

  18. flying path Venus Mars Allowing Multiple Venusians in Transit • Resources and scopes: flying path (global), a counter for Venusians in transit (Venus)

  19. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  20. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; // == 1 if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  21. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; // == 2 if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  22. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; // == 2 if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  23. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; // == 2 if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  24. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  25. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit flying path Venus Mars

  26. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); int VenusiansInTransit = 0; ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } Allowing Multiple Venusians in Transit Sure. Date? flying path Venus Mars

  27. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised flying path Venus Mars

  28. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised flying path Venus Mars

  29. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised flying path Venus Mars

  30. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Wait! flying path Venus Mars

  31. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  32. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  33. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  34. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  35. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  36. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  37. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  38. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  39. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  40. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  41. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  42. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  43. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  44. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  45. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  46. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  47. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  48. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  49. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

  50. semaphore flyingPath = 1; P(flyingPath); // send the message V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message P(VenusianLock); --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); } V(VenusianLock); Allowing Multiple Venusians in Transit Revised Again flying path Venus Mars

More Related