Semaphores CSCI 444/544 Operating Systems Fall 2008.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
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.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Monitors 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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
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.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CSE 451: Operating Systems Winter 2014 Module 8 Semaphores, Condition Variables, and Monitors Mark Zbikowski Allen Center 476 ©
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CSE 451: Operating Systems Spring 2006 Module 8 Semaphores and Monitors John Zahorjan Allen Center 534.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
CS703 – Advanced Operating Systems
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Operating Systems CMPSC 473
CSE 120 Principles of Operating
CSE451 Basic Synchronization Spring 2001
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Autumn Module 8 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Presentation transcript:

Semaphores CSCI 444/544 Operating Systems Fall 2008

Agenda What is semaphore? –Two basic operations –Two types of semaphores Usage of semaphores Semaphore implementation Producer/consumer problem

Background Semaphore = a synchronization primitive higher level of abstraction than locks is like a generalized lock invented by Dijkstra in 1968, as part of the THE operating system Locks only provide mutual exclusion Ensure only one thread is in critical section at a time Semaphores have two purposes Mutex: Ensure threads don’t access critical section at same time Scheduling constraints: Ensure threads execute in specific order

Semaphore A semaphore is: an integer variable that is manipulated through two operations, P and V (Dutch for “wait” and “signal”) –P(sem) (wait/down) block until sem > 0, then subtract 1 from sem and proceed –V(sem) (signal/up) add 1 to sem Do these operations atomically

Two Types of Semphores Binary semaphore (aka mutex semaphore) sem is initialized to 1 guarantees mutually exclusive access to resource (e.g., a critical section of code) only one thread/process allowed entry at a time Counting semaphore sem is initialized to N –N = number of units available represents resources with many (identical) units available allows threads to enter as long as more units are available

Usage of Semaphores To achieve mutual exclusion - ensure that only 1 thread (or more generally, less than N threads) is in critical section - initial value of semaphore is set to 1 (or more generally N) wait() signal() Like lock/unlock, but more general

Usage of Semaphores (con’d) To achieve scheduling constraints - used when a thread should wait for some event (not just another thread leaving critical section) - initial value of semaphore is set to 0 (usually but not always) Thread A:Thread B: wait()Do task continue execution signal()

Implementing Semaphores (I) The function wait(): wait(S) { while (S <= 0); // no-op S--; } The function signal(): signal(S) { S++; }

Implementing Semaphores (II) typedef struct { int value; queue tlist; } semaphore; wait (semaphore *S) { // Must be executed atomically S->value--; if (S->value < 0) { add this process to S->tlist; block(); } signal (semaphore *S) {// Must be executed atomically S->value++; if (S->value <= 0) { remove thread t from S->tlist; wakeup(t); }

Queue in Semaphores Each semaphore has an associated queue of threads when P (sem) is called by a thread, –if sem was “available” (>0), decrement sem and let thread continue –if sem was “unavailable” (<=0), block thread and place it on associated queue; run some other thread when V (sem) is called by a thread –if thread(s) are waiting on the associated queue, unblock one place it on the ready queue might as well let the “V-ing” thread continue execution –otherwise (when no threads are waiting on the sem), increment sem the signal is “remembered” for next time P(sem) is called

Simple Observations Sem value is negative --> Number of waiters on queue Sem value is positive --> Number of threads that can be in critical section at same time

Semaphores vs Locks Threads that are blocked at the level of program logic are placed on queues, rather than busy-waiting Busy-waiting may be used for the “real” mutual exclusion required to implement P and V but these are very short critical sections – totally independent of program logic

Producer/Consumer Problem Also known as bounded-buffer problem - N buffers, each can hold one item - Three semaphores: one binary, two counting - Binary Semaphore mutex initialized to the value 1 - Counting semaphore full initialized to the value 0 - Counting semaphore empty initialized to the value N.

Producer Thread The structure of the producer do { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (true);

Consumer Thread The structure of the consumer do { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item } while (true);

Problems with Semaphores They can be used to solve any of the traditional synchronization problems, but: semaphores are essentially shared global variables –can be accessed from anywhere (bad software engineering) there is no connection between the semaphore and the data being controlled by it used for both critical sections (mutual exclusion) and for coordination (scheduling) no control over their use, no guarantee of proper usage Thus, they are prone to bugs another (better?) approach: use programming language support –monitor