Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Synchronization Principles Gordon College Stephen Brinton.
Classical Problems of Concurrency
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
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
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.
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.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
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.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
Synchronization II: CPE Operating Systems
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
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.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
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, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
CSE Operating System Principles Synchronization.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Producer-Consumer Problem David Monismith cs550 Operating Systems.
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.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Deadlock and Starvation
Chapter 6: Process Synchronization
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Process Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Classical Synchronization Problems
Deadlock and Starvation
Operating Systems CMPSC 473
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Chapter 7: Synchronization Examples
Presentation transcript:

Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar

Mid-semester feedback On Angel, format similar to SRTEs Please submit by end of the week Based on feedback so far: will do a revision of mutual exclusion related topics

Agenda Some classic synchronization problems using semaphores Last class –Producer/Consumer problem –Readers/Writers problem Solution was free of deadlocks Starvation problem Today –Readers/Writers problem Consider more carefully the definition of starvation –Dining Philosophers problem

Readers-Writers Problem A data set shared among a number of concurrent processes –Readers – only read the data set; they do not perform any updates –Writers – can both read and write Problem – allow multiple readers to read at the same time. Only one writer may access the shared data at a given time Shared Data –Data set –Semaphore mutex initialized to 1 –Semaphore wrt initialized to 1 –Integer readcount initialized to 0

Readers-Writers Problem (Cont.) The structure of a writer process do { wait (wrt) ; // writing is performed signal (wrt) ; } while (TRUE); mutex = 1 wrt = 1 readcount = 0 Allow only one writer at a time to write

Readers-Writers Problem (Cont.) The structure of a reader process do { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) ; // reading wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; } while (TRUE); mutex = 1 wrt = 1 readcount = 0 Proceed only if no writer is writing; disallow writers once we proceed Signal a writer only when there are no more active readers

Readers/Writers Note that starvation is said to occur only when a process, despite getting CPU, does not get to make use of it –Only writers are starved, not readers Can we eliminate starvation for writers?

Dining-Philosophers Problem Shared data –Bowl of rice (data set) –Semaphore chopstick [5] initialized to 1

Dining-Philosophers Problem (Cont.) The structure of Philosopher i: do { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); Deadlock? Starvation? What happens if all pick their left chopsticks?

Dining-Philosophers Problem (Cont.) Idea #1: Make the two wait operations occur together as an atomic unit (and the two signal operations) –Use another binary semaphore for this –Any problems with this?

Dining-Philosophers Problem (Cont.) Idea #2: Does it help if one of the neighbors picks their left chopstick first and the other picks their right chopstick first? What is the most # phils. that can eat simultaneously?

Bounded Buffer/ Producer Consumer Buffer has max capacity N Producer can only add if buffer has room (i.e., count < N) Consumer can only remove if buffer has item (i.e., count > 0) Producer Consumer N = 4 2 empty slots 2 occupied slots

Producer Consumer: Mutex Locks mutex_lock m; int count = 0; Produce() { while (count == N); mutex_lock (m); … ADD TO BUFFER, count++ … mutex_unlock (m); } Consume() { while (count ==0); mutex_lock (m); … REMOVE FROM BUFFER, count-- … mutex_unlock (m); } Downside: Busy waiting Liveness guarantee depends on how lock is implemented –E.g., –Peterson’s => Mutex and bounded wait –Test&set => Only mutex

Binary Semaphores == Mutex Locks semaphore m = 1; int count = 0; Produce() { while (count == N); wait (m); … ADD TO BUFFER, count++ … signal (m); } Consume() { while (count ==0); wait (m); … REMOVE FROM BUFFER, count-- … signal (m); }

Use three semaphores Note important difference from condition variables –Signals are not lost sem protects critical sections full forces a consumer to wait till there is at least one item in buffer empty ensures at most N items are produced between empty buffer and execution of a consumer Producer Consumer: Semaphores semaphore sem = 1; semaphore full = 0; semaphore empty = N; Produce() { wait (empty); wait (sem); … ADD TO BUFFER, count++ … signal (sem); signal (full); } Consume() { wait (full); wait (sem); … REMOVE FROM BUFFER, count-- … signal (sem); signal (empty); }

Producer Consumer: Semaphores semaphore sem = 1; int count = 0; Produce() { if (count == N) wait (sem); … ADD TO BUFFER, count++ … signal (sem); } Consume() { if (count == 0) wait (sem); … REMOVE FROM BUFFER, count-- … signal (sem); } A non-working example –Identify the race condition here –That is, can a producer and consumer be in their critical sections simultaneously?

A non-working example What happens if a consumer acquires the lock before an item is added to the buffer? –Deadlock! Producer Consumer: Semaphores semaphore sem = 1; int count = 0; mutex_lock m; Produce() { mutex_lock (m); if (count == N) wait (sem); … ADD TO BUFFER, count++ … signal (sem); mutex_unlock (m); } Consume() { mutex_lock (m); if (count == 0) wait (sem); … REMOVE FROM BUFFER, count-- … signal (sem); mutex_unlock (m); }