5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Operating Systems Unit 3: – Concurrent execution mutual exclusion – Concurrent programming semaphore monitor Operating Systems.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CSE 451: Operating Systems Section 5: Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
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.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
CSE 451: Operating Systems Section 5 Synchronization.
Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
CSE 451 Section 4. 2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var.
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
CS703 – Advanced Operating Systems
Process Synchronization
PARALLEL PROGRAM CHALLENGES
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Midterm review: closed book multiple choice chapters 1 to 9
Synchronization and Semaphores
Lecture 2 Part 2 Process Synchronization
Critical section problem
Implementing Mutual Exclusion
Conditions for Deadlock
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Presentation transcript:

5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait and signal commands Also called P and V operations, respectively

5.6.1 Mutual Exclusion with Semaphores Binary semaphore: allow only one thread in its critical section at once –Wait operation If no threads are waiting, allow thread into its critical section Decrement protected variable (to 0 in this case) Otherwise place in waiting queue –Signal operation Indicate that thread is outside its critical section Increment protected variable (from 0 to 1) A waiting thread (if there is one) may now enter

Figure 5.15 Mutual exclusion with semaphores Mutual Exclusion with Semaphores

Group Discussion 4 (2/14/2008) Questions 1-3 are related to the code of figure If Occupied is 1, can the calling thread go into its critical section? 2.What happens if the initial value of the semaphore is set to 0? 3.If a thread did not call P before V, what happens? 4.Is there any advantage for using semaphores vs. TestAndSet instruction? 5.Does TestAndSet provide mutual exclusion?

5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer enters its critical section to produce value Consumer is blocked until producer finishes Consumer enters its critical section to read value Producer cannot update value until it is consumed –Semaphores offer a clear, easy-to-implement solution to this problem

Figure 5.16 Producer/consumer relationship implemented with semaphores. (1 of 2) Thread Synchronization with Semaphores

Figure 5.16 Producer/consumer relationship implemented with semaphores. (2 of 2) Thread Synchronization with Semaphores

5.6.3 Counting Semaphores Counting semaphores –Initialized with values greater than one –Can be used to control access to a pool of identical resources Decrement the semaphore’s counter when taking resource from pool Increment the semaphore’s counter when returning it to pool If no resources are available, thread is blocked until a resource becomes available

5.6.4 Implementing Semaphores Semaphores can be implemented at application or kernel level –Application level: typically implemented by busy waiting Inefficient –Kernel implementations can avoid busy waiting Block waiting threads until they are ready –Kernel implementations can disable interrupts Guarantee exclusive semaphore access Must be careful to avoid poor performance and deadlock Implementations for multiprocessor systems must use a more sophisticated approach

Group Discussion 5, 2/19/08, Implementing Semaphores- disabling interrupts class semaphore { private int count; public semaphore (int init) { count = init; } public void P () { while (1) { Disable interrupts; if (count > 0) { count--; Enable interrupts; } else { Enable interrupts; } public void V () { Disable interrupts; count++; Enable interrupts; }

1. Add what is missing according to your understanding of semaphores. Group Discussion 5, 2/19/08, Implementing Semaphores 2. Explain how a binary semaphore and its operations can be implemented in kernel. What does the kernel have to provide to implement it. 3. Why do we say that if a semaphore is implemented in user space, busy waiting has to be used?