U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
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.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Homework-6 Questions : 2,10,15,22.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
CS703 - Advanced Operating Systems
Monitors, Condition Variables, and Readers-Writers
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Kernel Synchronization II
Anthony D. Joseph and Ion Stoica
Semaphore Originally called P() and V() wait (S) { while S <= 0
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Synchronization Hank Levy 1.
CS703 - Advanced Operating Systems
Critical section problem
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitor Giving credit where it is due:
Implementing Mutual Exclusion
Kernel Synchronization II
Homework 4.
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 2019
, Part II Process Synchronization
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
CSE 542: Operating Systems
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture 9: Synchronization III

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 2 Last Time: Locks & Semaphores More on hardware support Implementing locks Test & Set Busy waiting Semaphores Generalization of locks

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 This Time: More Synch Primitives Reader-Writer Locks Monitors Condition Variables

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 Reader/Writers Problem Suppose one object shared among many threads Each thread is either a reader or a writer Readers – only read data, never modify Writers – read & modify data How should we control access to this object? Which synchronization primitive? RWRWR

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 5 Single Lock thread A Lock.acquire() Read data Lock.release() thread B Lock.acquire() Modify data Lock.release() thread C Lock.acquire() Read data Lock.release() thread D Lock.acquire() Read data Lock.release() thread E Lock.acquire() Read data Lock.release() thread F Lock.acquire() Modify data Lock.release() Drawbacks of this solution?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Readers/Writers Optimization Single lock: safe, but limits concurrency Only one thread at a time, but… Safe to have simultaneous readers! But only one writer at a time Must guarantee mutual exclusion for writers

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 7 Readers/Writers: Example thread A Lock.acquire() Read data Lock.release() thread B Lock.acquire() Modify data Lock.release() thread C Lock.acquire() Read data Lock.release() thread D Lock.acquire() Read data Lock.release() thread E Lock.acquire() Read data Lock.release() thread F Lock.acquire() Modify data Lock.release() Maximizes concurrency Great! But how do we implement this?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 8 Reader-Writer Locks New synchronization operator: reader-writer lock Multiple readers, just one writer Can be built with standard synch primitives E.g., semaphores

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 9 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 11 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 12 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 13 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 14 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 15 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 16 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader writer

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 17 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader writer

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 18 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader writer

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 19 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in reader writer

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 20 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in writer

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 21 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in writer

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 22 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in writer reader

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 23 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in writer reader What happens next?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 24 Starvation Problem Two possible policies: 1. No reader waits unless writer is already in 2. Waiting writer always gets served first Both variants may lead to starvation First: writers may starve Second: readers may starve

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 25 Readers/Writers Algorithm As long as there are no writers Let readers in If no readers or writers Let one writer in writer reader variant 2variant 1 How to avoid starvation?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 26 Implementing R/W Locks Can implement with two semaphores “Mutex”: protect number of readers “Queue”: control scheduling of writers Control access to a “database” int getValue() void setValue (int n)

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 27 Implementing R/W Locks class RWDatabase { private Database db; private int readers; private Semaphore mutex; private Semaphore writersSemaphore; RWInt() { readers = 0; mutex = new Semaphore (1); queue = new Semaphore (1); } int read() { … } void write (int n) { … } };

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 28 Write value Implementing R/W Locks void RWDatabase::write (int v) { writersSemaphore.wait(); db.setValue(v); // Write the value writersSemaphore.signal(); };

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 29 Read, remove reader Add a reader Implementing R/W Locks int RWDatabase::read() { int v; mutex.wait(); readers++; if (readers == 1) { // I’m first reader writersSemaphore.wait(); } mutex.signal(); v = db.getValue(); mutex.wait(); readers--; if (readers == 0) { // I’m last reader writersSemaphore.signal(); } mutex.signal(); return v; }; Who can starve?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 30 Problems with Semaphores & Locks Much better than load/store Still many drawbacks Serve two purposes Mutual exclusion Scheduling constraints Effectively shared global variables Access to semaphores may be anywhere Not structured Not tied to data they control access to

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 31 Monitors Invented by C.A.R. “Tony” Hoare for Mesa Also invented quicksort, “Hoare triples” {a > 16} a = sqrt(a); { a > 4 } monitor = Java class with: All data private All methods synchronized

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 32 Implementing Monitors in Java class QueueMonitor { private queue q; public void synchronized add (Object item) { q.add (item); } public Object synchronized remove() { if (q.notEmpty()) { Object o = q.remove(); return o; } else { // what should we do here? } };

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 33 Remove Options Options for remove when queue empty: Return special error value (e.g., NULL) Throw an exception Wait for something to appear in the queue Wait = sleep() But sleep inside synchronized… Holds lock Goes to sleep Never wakes up!

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 34 Condition Variables Queue of threads waiting in critical section Thread must hold lock when performing operations: wait(Lock l) Atomically releases lock, goes to sleep Reacquires lock when awakened notify() Wakes up one waiting thread, if any notifyAll() Wakes up all waiting threads

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 35 Implementing Monitors in Java class QueueMonitor { private queue q; public void synchronized add (Object item) { q.add (item); notify(); } public Object synchronized remove() { while (q.empty()) { wait(); } return q.remove(); } };

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 36 R/W Locks in Java class RWDatabase { private Database db; private int readers; private int writers; RWInt() { readers = 0; } public synchronized int read() { … } public synchronized void write (int n) { … } };

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 37 R/W Locks in Java public int RWDatabase::read() { preRead(); int v = db.getValue(); postRead(); return v; }; private void synchronized preRead() { while (writers > 0) wait(); readers++; } private void synchronized postRead() { readers--; if (readers == 0) notify(); }

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 38 R/W Locks in Java public synchronized void RWDatabase::write (int v) { preWrite(); db.setValue(v); postWrite(); } private void preWrite() { writers++; while (readers > 0) wait(); } private void postWrite() { writers--; notify(); }

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 39 Summary Reader-Writer Locks Permit concurrent reads Implementable with semaphores Monitors Classes: tie data, methods with synchronization Condition Variables Release lock temporarily Waiting inside critical sections

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 40 Next Time Deadlock