More on Semaphores Andy Wang Operating Systems COP
- Slides: 61
More on Semaphores Andy Wang Operating Systems COP 4610 / CGS 5765
The Pigeon Network Scenario l Pigeons are good message carriers l Reasonably reliable l Relatively fast for less developed rural areas l Can sense magnetic field lines
Here is the Story… l There are two towns—Mars and Venus l Mars has all male pigeons l Venus has all female pigeons l Each town delivers messages to the other l By sending a pigeon through the shared flying path l And waiting for the same pigeon to fly back as an acknowledgement
Here is the Story… l Based on experience l Whenever both town send messages simultaneously, the reliability drops significantly l Pigeons of opposite genders decide to take discursions l Goals of a pigeon network: l Efficiency l Fairness
Developing the Solution Can we map it to some solved problems? l Standard synchronization problems: l l Bounded buffer (producers and consumers) l Fairness (readers and writers) l Resource allocation (dining philosopher) l Pigeon network is under the reader-writer category
Step 1: Visualization l Identify l Shared resources l Scope of shared resources flying path Mars Venus
Step 1: Visualization l Identify l Shared resource: flying path l Scope of the shared resource: global flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path something you don’t see everyday… Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); flying path Mars Venus
Simplest Implementation + Simple + Fair - Not efficient l Only one pigeon can fly at a time l Venusians love to fly in groups…
Allowing Multiple Venusians in Transit l Resources and scopes: flying path (global), a counter for Venusians in transit (Venus) flying path Mars Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit semaphore flying. Path = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); Sure. Date? ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } Venus
Allowing Multiple Venusians in Transit Revised semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } V(Venusian. Lock); Venus
Allowing Multiple Venusians in Transit Revised semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } V(Venusian. Lock); Venus
Allowing Multiple Venusians in Transit Revised semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } V(Venusian. Lock); Venus
Allowing Multiple Venusians in Transit Revised semaphore flying. Path = 1; P(flying. Path); // send the message V(flying. Path); Wait! semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit == 1) { P(flying. Path); } // send the message flying path Mars --Venusians. In. Transit; if (Venusians. In. Transit == 0) { V(flying. Path); } V(Venusian. Lock); Venus
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again semaphore flying. Path = 1; semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(flying. Path); // send the message V(flying. Path); P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); flying path Mars Venus == 1) { == 0) {
Allowing Multiple Venusians in Transit Revised Again Efficient for Venusians l Not fair for Marsians l
Allowing Multiple Venusians and Martians in Transit semaphore flying. Path = 1; semaphore Martian. Lock = 1; int Martians. In. Transit = 0; P(Martian. Lock); ++Martians. In. Transit; if (Martians. In. Transit == 1) { P(flying. Path); } V(Martian. Lock); // send the message P(Martian. Lock); --Martians. In. Transit; if (Martians. In. Transit == 0) { V(flying. Path); } V(Martian. Lock); semaphore Venusian. Lock = 1; int Venusians. In. Transit = 0; P(Venusian. Lock); ++Venusians. In. Transit; if (Venusians. In. Transit P(flying. Path); } V(Venusian. Lock); // send the message P(Venusian. Lock); --Venusians. In. Transit; if (Venusians. In. Transit V(flying. Path); } V(Venusian. Lock); == 1) { == 0) {
Allowing Multiple Venusians and Martians in Transit Both sides can transmit efficiently l One side can completely block off the traffic from the other side l Fair, since both sides are equally selfish… l Fortunately, pigeons do sleep l Daily cycles will break the starvation… l
Proving the Correctness l Safety (mutual exclusion): l At most one thread is in the critical section at any time l All necessary locks are acquired at the entrance of critical section l All shared resources are protected
Proving the Correctness l Liveness (progress): l If more than one thread is interested in executing the critical section, some processes will eventually do so (deadlock free) l Fairness (bounded waiting): l Every thread that wants to execute the critical section will eventually do so (no starvation)
- More more more i want more more more more we praise you
- More more more i want more more more more we praise you
- Good cop bad cop interrogation
- Cop 1 cop 2
- Andy wang fsu
- Andy wang fsu
- Mutex puzzle
- Monitors and semaphores
- Semaphores in os
- Semaphore provide a primitive yet powerful and flexible
- Sleeping barber problem using semaphores in c
- Types of semaphore
- Test and set instruction in os
- Semaphores
- Semaphores and monitors
- 5 apples in a basket riddle
- The more you study the more you learn
- Aspire not to
- Law of inertia definition
- Knowing more remembering more
- The more i give to thee the more i have
- More choices more chances
- Human history becomes more and more a race
- Early operating systems
- Evolution of operating systems
- Components of an operating system
- User view and system view in os
- Wsn operating systems
- 3 easy pieces
- Operating systems lab
- Introduction to operating systems
- Andrew tanenbaum modern operating systems
- Components of file
- Distributed design issues
- Early operating systems
- Real-time operating systems
- Can we make operating systems reliable and secure
- Alternative operating systems
- Operating systems mit
- Operating system internals and design principles
- Evolution of operating systems
- Examples of network operating system
- Msdn operating systems subscription
- Menuet os
- Real time characteristics of embedded operating systems
- Operating systems
- Types of operating system
- Virtualization technology in modern operating systems
- Computer divided into two parts
- Structures of operating system
- Components of operating systems
- Architecture of distributed operating system
- Module 4 operating systems and file management
- Modern operating systems 3rd edition
- Uc berkeley operating systems
- Operating systems
- Improving the reliability of commodity operating systems
- Dual mode in os
- Operating systems
- Godmar back
- Tanenbaum structured computer organization
- What are 5 operating systems