Monitors and Blocking Synchronization By Tal Walter.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
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.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
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)
Interprocess Communication
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Art of Multiprocessor Programming1 Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified.
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.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
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.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
1 Concurrency: Deadlock and Starvation Chapter 6.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Nachos Phase 1 Code -Hints and Comments
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.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Kernel Locking Techniques by Robert Love presented by Scott Price.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Homework-6 Questions : 2,10,15,22.
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.
Interprocess Communication Race Conditions
CS703 - Advanced Operating Systems
Monitors and Blocking Synchronization
COT 4600 Operating Systems Fall 2009
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
January 29, 2004 Adrienne Noble
CS533 Concepts of Operating Systems Class 3
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Semaphore Originally called P() and V() wait (S) { while S <= 0
Multithreading.
Synchronization Hank Levy 1.
Concurrency: Mutual Exclusion and Process Synchronization
CS533 Concepts of Operating Systems Class 3
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
“The Little Book on Semaphores” Allen B. Downey
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Monitors and Blocking Synchronization By Tal Walter

Outline Monitors Conditions Readers-Writers Locks Reentrant locks / semaphores implementations

Motivation for Monitors Programming with locks often gets complicated and cumbersome In order to program something that uses multiple cores, you have to use and understand locks. Monitors encapsulate a class with it’s synchronization needs Provides another layer of abstraction User doesn’t have to be aware of locks at all.

What’s a Monitor? Monitors are a structured way of combining synchronization and data. A class encapsulates both data and methods in the same way that a monitor combines data, methods, and synchronization in a single modular package.

An Example mutex.lock(); try { queue.enq(x) } finally { mutex.unlock(); } Suppose an application has two threads, a producer and a consumer, that communicate through a shared FIFO queue. We could have the threads share two objects: an unsynchronized queue, and a lock to protect the queue. The producer would be:

An Example Suppose the queue is bounded. What to do on queue.enq(x)? Would it pass? What if the queue is full? Would it block? Depends on internal state mutex.lock(); try { queue.enq(x) } finally { mutex.unlock(); }

An Example The user of the queue will have to figure out a cumbersome synchronization protocol. The queue should manage its own synchronization. A queue that also has the synchronization logic encapsulated is a synchronized queue, a Monitor.

Spinning/Blocking-a reminder If a thread cannot immediately acquire a lock, it can either spin, repeatedly testing whether the desired event has happened, or it can block, giving up the processor for a while to allow another thread to run.

Conditions – Why? Back to the queue, a thread that waits to deq(x) on an empty queue needs to be blocked After the waiting thread went to sleep, it needs a way to be awakened, to reacquire the lock and try again. That’s where conditions come in:

Conditions – What? A condition is an object that’s associated with a lock, and is created by calling that lock’s newCondition() method. If the thread holding that lock calls the associated condition’s await() method, it releases that lock and suspends itself, giving another thread the opportunity to acquire the lock.

Conditions – What? When that condition is signal()ed by some other thread, the noble thread awakens (or not) and reacquires the lock, perhaps competing with other threads in the process.

12 A Monitor Lock Critical Section waiting room Lock() unLock()

13 Unsuccessful Deq Critical Section waiting room Lock() await() Deq() Oh no, Empty!

14 Another One Critical Section waiting room Lock() await() Deq() Oh no, Empty!

15 Enqueur to the Rescue Critical Section waiting room Lock() signalAll() Enq( ) unLock() Yawn!

16 Yawn! Monitor Signalling Critical Section waiting room Yawn! Awakend thread might still lose lock to outside contender…

17 Dequeurs Signalled Critical Section waiting room Found it Yawn!

18 Yawn! Dequeurs Signalled Critical Section waiting room Still empty!

19 Dollar Short + Day Late Critical Section waiting room

20 Lost Wake-Up Critical Section waiting room Lock() signal () Enq( ) unLock() Yawn!

21 Lost Wake-Up Critical Section waiting room Lock() Enq( ) unLock() Yawn!

22 Lost Wake-Up Critical Section waiting room Yawn!

23 Lost Wake-Up Critical Section waiting room Found it

24 What’s Wrong Here? Critical Section waiting room zzzz….!

A Queue Example An example of a queue with conditions who are Signal()ed everytime instead of using SignalAll() when transitioning:

A Queue Example

Readers-Writers Locks A lot of times we can split a data structure’s methods into “readers” that return information about the object’s state without modifying the object, and “writers” who actually modify the object. There can be many readers at once, or a writer, but only one. A readers–writers lock allows multiple readers or a single writer to enter the critical section concurrently. In practice, we’ll call readLock().lock() and writeLock().lock() accordingly, instead of calling lock.lock().

Readers-Writers Locks We’ll have a look at 2 Readers–Writers Lock implementations. First one is pretty straight-forward, The SimpleReadWriteLock

SimpleReadWriteLock

ReadLock

WriteLock OR

Introducing fairness into the scheme If readers are much more frequent than writers, as is usually the case, then writers could be locked out for a long time by a continual stream of readers. The FifoReadWriteLock class, that will be shown next, shows a way to give writers priority.

FifoReadWriteLock – The Idea FifoReadWriteLock ensures that once a writer calls the write lock’s lock() method, no more readers will be able to acquire the read lock until the writer has acquired and released the write lock. Eventually, the readers holding the read lock will drain out without letting any more readers in, and the writer will acquire the write lock.

FifoReadWriteLock

ReadLock

WriteLock

The Reentrant Lock It’s simply a lock that can be obtained multiple times by the same thread that originally acquired it. Let’s see how do we implement a reentrant lock using a non reentrant one and a condition:

The Reentrant Lock – implementation

Another use for conditions - Semaphores We all know what a semaphore is, let’s see the implementation (don’t worry it’s pretty short)

Semaphore implementation

Summary Monitors Conditions Readers-Writers Locks Reentrant locks / semaphores implementations

That’s it!