Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Classic Synchronization Problems
Synchronization Principles Gordon College Stephen Brinton.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
Monitors CSCI 444/544 Operating Systems Fall 2008.
What we will cover… Process Synchronization Basic Concepts
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Synchronization II: CPE Operating Systems
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Chapter 6: Process Synchronization
1 Concurrency: Mutual Exclusion and Synchronization Chapter 4.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS307 Operating Systems Process Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
CSE Operating System Principles Synchronization.
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Semaphores Chapter 6. Semaphores are a simple, but successful and widely used, construct.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Deadlock and Starvation
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization – Part 3
Deadlock and Starvation
Chapter 6-7: Process Synchronization
Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Chapter 6: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
CMPT 300: Operating Systems I
Critical section problem
Chapter 7: Synchronization Examples
Presentation transcript:

Semaphores Ref: William Stallings G.Anuradha

Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process can be forced to stop at a specified place until it has received a specific signal For signaling semaphores are used – For sending a signal semSignal (s) is used – For receiving a signal semWait(s) is used First defined by Dijkstra Semaphore is a variable – A semaphore may be initialized to a nonnegative integer value. – The semWait operation decrements the semaphore value. – The semSignal operation increments the semaphore value.

Definition of semaphore

Binary Semaphore Can only take on values 0, 1

Operations on Binary Semaphore

Types of Semaphores Binary semaphore:-takes value 0 and 1 Mutex:-similar to binary semaphore but the key difference is that the mutex is locked and unlocked by the same process Strong semaphore:- Process who is blocked the longest is released from queue first Weak semaphore:- Semaphore whose order is not specified.

Animation

Classical Synchronization problems Producer consumer problem Reader-writer problem Barber shop Dining philosopher

9 I. Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt

10 Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs!

11 Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs!

12 Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine a customer picking items off the conveyor belt

13 Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine many such customers picking items off the conveyor belt!

14 Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine many such customers picking items off the conveyor belt!

15 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

16 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

17 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

18 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

19 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

20 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

21 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

22 Producer-Consumer insertPtr removePtr BUFFER FULL: Producer must be blocked! Chef = Producer Customer = Consumer

23 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

24 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

25 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

26 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

27 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

28 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

29 Producer-Consumer insertPtr removePtr Chef = Producer Customer = Consumer

30 Producer-Consumer insertPtr removePtr BUFFER EMPTY: Consumer must be blocked! Chef = Producer Customer = Consumer

Producer Consumer problem Infinite buffer

Possible scenario

Correct solution

Finite circular buffer for the Producer/consumer problem

Summing up One process produces some type of data The other process consumes that data – Data stored in a shared buffer (infinite size) – Require mutual exclusion to access buffer Producer do forever produce item wait(s) append to queue n++ if n = 1 then signal(delay) signal(s) Consumer wait(delay) do forever wait(s) remove from queue n-- m = n signal(s) if m = 0 then wait(delay)

Barbershop Problem 3 barbers, each with a barber chair – Haircuts may take varying amounts of time Sofa can hold 4 customers, max of 20 in shop – Customers wait outside if necessary When a chair is empty: – Customer sitting longest on sofa is served – Customer standing the longest sits down After haircut, go to cashier for payment – Only one cash register – Algorithm has a separate cashier, but often barbers also take payment This is also a critical section

Fair Barbershop program barbershop2; varmax_capacity: semaphore (:=20); sofa: semaphore (:=4); barber_chair, coord: semaphore (:=3); mutex1, mutex2: semaphore (:=1); cust_ready, leave_b_chair, payment, receipt: semaphore (:=0) finished: array [1..50] of semaphore (:=0); count: integer; procedure customer; procedure barber;procedure cashier; var custnr: integer; var b_cust: integerbegin begin begin repeat wait (max_capacity ); repeat wait( payment ); enter shop; wait( cust_ready ); wait( coord ); wait( mutex1 ); wait( mutex2 ); accept payment; count := count + 1; dequeue1( b_cust ); signal( coord ); custnr := count; signal( mutex2 ); signal( receipt ); signal( mutex1 ); wait( coord ); forever wait( sofa ); cut hair;end; sit on sofa; signal( coord ); wait( barber_chair ); signal( finsihed[b_cust] ); get up from sofa; wait( leave_b_chair ); signal( sofa ); signal( barber_chair ); sit in barber chair;forever wait( mutex2 ); end; enqueue1( custnr ); signal( cust_ready ); signal( mutex2 ); wait( finished[custnr] ); leave barber chair; signal( leave_b_chair ); pay; signal( payment ); wait( receipt ); exit shop; signal( max_capacity ); end;

39 II. Reader-Writer Problem A reader: read data A writer: write data Rules: – Multiple readers may read the data simultaneously – Only one writer can write the data at any time – A reader and a writer cannot access data simultaneously Locking table: whether any two can be in the critical section simultaneously ReaderWriter Reader OKNo WriterNo

40 III. Dining Philosophers: an intellectual game 1

Problem Statement Five philosophers eat then think forever – They never sleep nor relieve themselves! – They do not behave altruistically They eat at a communal table – It has a single bowl of tangled spaghetti – Five plates each with a single fork to the left of their plate – To eat a philosopher must have two forks, their own and that of their neighbour’s to the right – If a philosopher is unable to eat they resume thinking

Ramifications Deadlock – All philosophers decide to eat at same time – They all pick up one fork – None of them can eat hence the system comes to a halt Starvation – Some philosophers think for such a short time and contrive to put their forks down in such a way that no other philosophers have the opportunity to pick up the two forks they require to eat

Deadlock - Pictorially

Starvation - Pictorially

Naïve Solution Unconstrained picking up and putting down of forks causes problems – So put down in the reverse order of picking up Manage the entry and exit of philosophers to the table by means of a Butler process that entry and exit is orderly Each fork is a process – Accessed either from left or right hand

Deadlocks Each philosopher has their left fork BUT Cannot get a right fork Butler did nothing – Needs to control access to table so that there is always one empty seat – Ensures one philosopher can eat

Butler Controls No more than 4 philosopher can sit at the table (enter) Once 4 philosophers seated then Butler only lets people exit (get down) from the table

Problems with Semaphores Incorrect usage of semaphores leads to timing errors Timing errors occur only if some particular execution sequence takes place.(rare) InCorrect use of semaphore operations – signal (mutex) …. wait (mutex) (multiple CS) – wait (mutex) … wait (mutex) (deadlock) – Omitting of wait (mutex) or signal (mutex) (or both)

Monitors A high-level abstraction (ADT) that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time Has one or more procedure, initialization sequence, local data Local data variables are accessible only by the monitor’s procedures and not by any external procedure monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } … }

Schematic view of a Monitor

Condition Variables Synchronization is supported by the use of condition variables that are contained and accessible only within the monitor condition x, y; Two operations on a condition variable: – x.wait () – Suspends execution of the calling process on condition x. monitor is available for use by another process – x.signal () – resumes execution of some process suspended after a wait on the same condion. one of processes (if any) that invoked

Monitor with Condition Variables

Monitors for Producer consumer problem Monitor module used to control the buffer used to store and retrieve characters. Condition variables – Not full :- TRUE when there is room to add atleast one character to the buffer – Not empty :- TRUE when there is atleast one character in the buffer

Monitors for Producer consumer Bounded Buffer problem

Solution to Dining Philosophers monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5];’/* hungry but is unable to obtain forks*/ /* A philosopher can set state[i]=eating only if state (i+4)%5 and (i+1)%5 are not eating */ void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); }

Solution to Dining Philosophers (cont) void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; }

Solution to Dining Philosophers (cont) Each philosopher I invokes the operations pickup() and putdown() in the following sequence: dp.pickup (i) EAT dp.putdown (i) No two neighbours eat simultaneously and no deadlocks will occur. But a philosopher can starve to death…………………………