Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
More Synchronization, Semaphores Vivek Pai / Kai Li Princeton University.
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.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
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.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
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.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Exam Review Andy Wang Operating Systems COP 4610 / CGS 5765.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore.
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.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
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
CPS110: Reader-writer locks
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Process Synchronization
Process Synchronization
Monitors, Condition Variables, and Readers-Writers
Lecture 13: Producer-Consumer and Semaphores
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
Threading And Parallel Programming Constructs
Process Synchronization
Anthony D. Joseph and John Canny
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
February 6, 2013 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
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 451: Operating Systems Winter Module 7 Semaphores and Monitors
Chapter 7: Synchronization Examples
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CIS 720 Lecture 6.
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Semaphores  Semaphore is a type of generalized lock –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.

Origin of Semaphores  Defined by Dijkstra in the last 60s  Main synchronization primitives used in UNIX  The P operation is an abbreviation for proberen (Dutch), meaning “to test”  The V operation stands for verhogen, meaning “to increment”

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  Mutual exclusion –Semaphore has an initial value of 1 –P() is called before a critical section –V() is called after the critical section semaphore s = 1; P(s); // critical section V(s);

Two Uses of Semaphores  Scheduling –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 scheduling and mutual exclusion  Constraints –The consumer must wait if buffers are empty (scheduling constraint) –The producer must wait if buffers are full (scheduling 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);}