Andy Wang Operating Systems COP 4610 / CGS 5765

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Synchronization with Eventcounts and Sequencers
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Concurrent Programming Problems OS Spring Concurrency pros and cons Concurrency is good for users –One of the reasons for multiprogramming Working.
Chapter 6: Process Synchronization
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Concurrency CS 510: Programming Languages David Walker.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
More on Semaphores Andy Wang Operating Systems COP 4610 / CGS 5765.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Homework-6 Questions : 2,10,15,22.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Operating systems Deadlocks.
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
Process Synchronization
Concurrent Processes.
Classical Synchronization Problems
Deadlock and Starvation
Lecture 16: Readers-Writers Problem and Message Passing
Concurrency: Mutual Exclusion and Synchronization
Monitors Chapter 7.
Andy Wang Operating Systems COP 4610 / CGS 5675
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Liveness And Performance
COMS Prelim 1 Review Session
Operating systems Deadlocks.
Semaphores Chapter 6.
Monitors Chapter 7.
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
Monitors Chapter 7.
Lecture 16: Readers-Writers Problem and Message Passing
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
, Part II Process Synchronization
CSE 153 Design of Operating Systems Winter 2019
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Sarah Diesburg Operating Systems CS 3430
Operating Systems {week 10}
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); flying path Venus Mars

Allowing Multiple Venusians in Transit 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); Sure. Date? flying path Venus Mars

Allowing Multiple Venusians in Transit Revised 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); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised 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); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised 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); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised 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); Wait! flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again 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 --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath); flying path Venus Mars

Allowing Multiple Venusians in Transit Revised Again Efficient for Venusians Not fair for Marsians

Allowing Multiple Venusians and Martians in Transit semaphore flyingPath = 1; semaphore MartianLock = 1; int MartiansInTransit = 0; P(MartianLock); ++MartiansInTransit; if (MartiansInTransit == 1) { P(flyingPath); } V(MartianLock); // send the message --MartiansInTransit; if (MartiansInTransit == 0) { V(flyingPath); semaphore VenusianLock = 1; int VenusiansInTransit = 0; P(VenusianLock); ++VenusiansInTransit; if (VenusiansInTransit == 1) { P(flyingPath); } V(VenusianLock); // send the message --VenusiansInTransit; if (VenusiansInTransit == 0) { V(flyingPath);

Allowing Multiple Venusians and Martians in Transit Both sides can transmit efficiently One side can completely block off the traffic from the other side Fair, since both sides are equally selfish… Fortunately, pigeons do sleep Daily cycles will break the starvation…

Proving the Correctness Safety (mutual exclusion): At most one thread is in the critical section at any time All necessary locks are acquired at the entrance of critical section All shared resources are protected

Proving the Correctness Liveness (progress): If more than one thread is interested in executing the critical section, some processes will eventually do so (deadlock free) Fairness (bounded waiting): Every thread that wants to execute the critical section will eventually do so (no starvation)