Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Operating Systems Part III: Process Management (Process Synchronization)
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Homework 3-4 Sarah Diesburg Operating Systems COP 4610.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Process Synchronization
Process Synchronization
1 July 2015 Charles Reiss CS162: Operating Systems and Systems Programming Lecture 7: Synchronization: Lock Implementation,
Monitors, Condition Variables, and Readers-Writers
Chapter 5: Process Synchronization
Lecture 13: Producer-Consumer and Semaphores
CSCI 511 Operating Systems Chapter 5 (Part B) Mutual Exclusion, Semaphores Dr. Frank Li.
Critical Section and Critical Resources
Sarah Diesburg Operating Systems COP 4610
Critical Section and Critical Resources
Semaphores and Bounded Buffer
Anthony D. Joseph and Ion Stoica
Process Synchronization
Anthony D. Joseph and John Canny
Critical section problem
Implementing Mutual Exclusion
Chapter 6: Process Synchronization
February 6, 2013 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Implementing Mutual Exclusion
Chapter 6 Synchronization Principles
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Lecture 13: Producer-Consumer and Semaphores
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
September 19, 2018 Prof. Ion Stoica
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Semaphores and Bounded Buffer

Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives used in UNIX –Consist of a positive integer value –Two operations  P(): an atomic operation that waits for semaphore to become positive, then decrement it by 1  V(): an atomic operation that increments semaphore by 1 and wakes up a waiting thread at P(), if any.

Semaphores vs. Integers  No negative values  Only operations are P() and V() –Cannot read or write semaphore values –Except at the initialization times  Operations are atomic –Two P() calls cannot decrement the value below zero –A sleeping thread at P() cannot miss a wakeup from V()

Binary Semaphores  A binary semaphore is initialized to 1  P() waits until the value is 1 –Then set it to 0  V() sets the value to 1 –Wakes up a thread waiting at P(), if any

Two Uses of Semaphores 1. Mutual exclusion –Lock was designed to do this lock->acquire(); // critical section lock->release();

Two Uses of Semaphores 1. Mutual exclusion 1.The lock function can be realized with a binary semaphore: semaphore subsumes lock.  Semaphore has an initial value of 1  P() is called before a critical section  V() is called after the critical section semaphore litter_box = 1; P(litter_box); // critical section V(litter_box);

Two Uses of Semaphores 1. Mutual exclusion –Semaphore has an initial value of 1 –P() is called before a critical section –V() is called after the critical section semaphore litter_box = 1; P(litter_box); // critical section V(litter_box); litter_box = 1

Two Uses of Semaphores 1. Mutual exclusion –Semaphore has an initial value of 1 –P() is called before a critical section –V() is called after the critical section semaphore litter_box = 1; P(litter_box); // purrr… // critical section V(litter_box); litter_box = 1  0

Two Uses of Semaphores 1. Mutual exclusion –Semaphore has an initial value of 1 –P() is called before a critical section –V() is called after the critical section semaphore litter_box = 1; P(litter_box); // critical section V(litter_box); litter_box = 0

Two Uses of Semaphores 1. Mutual exclusion –Semaphore has an initial value of 1 –P() is called before a critical section –V() is called after the critical section semaphore litter_box = 1; P(litter_box); // meow… // critical section V(litter_box); litter_box = 0

Two Uses of Semaphores 1. Mutual exclusion –Semaphore has an initial value of 1 –P() is called before a critical section –V() is called after the critical section semaphore litter_box = 1; P(litter_box); // critical section V(litter_box); litter_box = 0  1

Two Uses of Semaphores 2. Synchronization: Enforcing some order between threads T1 T2 T1 T2 do X do X wait for X wait for X do Y do Y wait for Y wait for Y do Z do Z wait for Z wait for Z …… ……

Two Uses of Semaphores 2. Synchronization –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }}

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0 wait

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0  1 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 1  0 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0 wait

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0  1

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 1  0

Two Uses of Semaphores 2. Scheduling –Semaphore usually has an initial value of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() {Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); }} wait_left = 0 wait_right = 0

Two Uses of Semaphores 2. Synchronization –Semaphore usually has an initial value of 0 semaphore s1 = 0; semaphore s2 = 0; A() {B() { write(x); P(s1); V(s1); read(x); P(s2); write(y); read(y); V(s2); }}

Producer-Consumer with a Bounded Buffer  A classic problem  A producer put things into a shared buffer  A consumer takes them out

Problem Constraints  The solution involves both synchronization and mutual exclusion  Constraints –The consumer must wait if buffers are empty (synchronization constraint) –The producer must wait if buffers are full (synchronization constraint) –Only one thread can manipulate the buffer at a time (mutual exclusion)

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0;

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0; Producer() { P(mutex); // put 1 item in the buffer V(mutex);} Consumer() { P(mutex); // take 1 item from the // buffer V(mutex);}

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);}

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);}

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 2 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 2 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 2 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 2  1 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1  0 nFreeBuffers = 1 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 1 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 1 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 1  0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0  1 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1  0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0  1

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 1  0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0  1 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1  0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0  1 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 0  1 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1 nFreeBuffers = 1  0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 1  0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Developing the Solution  Each constraint needs a semaphore semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers);P(mutex); // put 1 item in the buffer V(mutex);V(nLoadedBuffers);} Consumer() { P(nLoadedBuffers);P(mutex); // take 1 item from the // buffer V(mutex);V(nFreeBuffers);} mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Implementing Semaphore  How to implement semaphore? –Almost exactly like lock. –Using spinlock or queue  What hardware support is needed? –Interrupt disable –Test-and-set

Implementing Semaphore class semaphore { int value; } semaphore::semaphore(int i) { value = i; value = i;} semaphore::p() { // disable interrupts while (value == 0) { // enable interrupts // disable interrupts } value --; // enable interrupts } semaphore::v() { // disable interrupts value ++; // enable interrupts }

Implementing Semaphore with test and set class semaphore { int value; } semaphore::semaphore(int i) { value = i; value = i;} semaphore::p() { while (test_and_set(guard)); while (value == 0) { // queue the thread // guard = 0 and sleep } value --; guard = 0; } semaphore::v() { while (test_and_set(guard)); if (anyone waiting) { if (anyone waiting) { // wake up one thread // wake up one thread // put in on ready queue // put in on ready queue } else { } else { value ++; value ++; } guard = 0; guard = 0;}

Semaphore in UNIX  Managing concurrent access to shared memory.  Semaphore system calls –Creation: semget( … ) –Incr/Decr/set : semop(…) –Deletion: semctl(semid, 0, IPC_RMID, 0);  See examples: seminit.c, sema.c semb.c