Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Chapter 7 - Resource Access Protocols (Critical Sections) Protocols: No Preemptions During Critical Sections Once a job enters a critical section, it cannot.
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.
Synchronization with Eventcounts and Sequencers
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Synchronization and Deadlocks
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.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
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.
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.
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Monitors and Blocking Synchronization By Tal Walter.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
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.
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.
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.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
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.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Kernel Locking Techniques by Robert Love presented by Scott Price.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
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.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
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.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Chapter 6. Semaphores are a simple, but successful and widely used, construct.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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
Background on the need for Synchronization
Multithreading Chapter 23.
Threading And Parallel Programming Constructs
Lecture 2 Part 2 Process Synchronization
Critical section problem
Chapter 7: Synchronization Examples
Semaphores Chapter 6.
Concurrency: Mutual Exclusion and Process Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
CSE 451 Section 1/27/2000.
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8

Content 1. Introduction 2. Monitor Locks and Conditions 3. Readers-Writers Locks 4. Semaphores 5. Locks and Conditions – Example of Usage in Code

1. Introduction

Monitors Class Data Methods Monitor Data Methods Synchronization

Why do we need monitors? Thread AThread B Hey, are you full? Yes

Why do we need monitors? Thread AThread B Hey, are you empty? No

Why do we need monitors? Thread AThread B Hey, are you full? No

Why do we need monitors? Oskar doesn’t like questions!

Why do we need monitors? Thread AThread B

Why do we need monitors? Thread AThread B

Why do we need monitors? Thread AThread B

Why do we need monitors? No questions asked! Or: the internal state of the object was not exposed

2. Monitor Locks and Conditions

Monitor Locks A lock is a basic mechanism of ensuring mutual exclusion. Only one thread at a time can hold a lock. A thread acquires a lock when it first starts to hold the lock. A thread releases a lock when it stops holding the lock A monitor exports a collection of methods, each of which acquires the lock when it is called, and releases it when it returns.

Spin and Block If a thread can not immediately acquire a lock, it has two options: Spin – repeatedly testing whether the desired event had happened. Makes sense on a multiprocessor if we expect to wait for a short time (e.g. a thread waiting for another thread to release a lock). Keeps the processor busy without doing any work. Block – giving up the processor for a while to allow another thread to run. Makes sense if we expect to wait for a long or unpredicted time (e.g. dequeue an item from an empty buffer). Requires an expensive call to the operating system. A combination of the two is also possible

Conditions While a thread is waiting for something to happen to an object, it is a very good idea to release the lock on the object. After the waiting thread releases the lock, it needs a way to be notified when to reacquire the lock and try again. In Java concurrency package, the ability to release a lock temporarily is provided by a Condition object associated with a lock. In the example above: “The garbage can is empty”

The Lost-Wakeup Problem Instead of signaling “The garbage can is not empty” each time we throw an item to it, would it not be more efficient to signal the condition only when the can actually transmits from empty to non empty?

The Lost-Wakeup Problem Signal: I’m not empty!

The Lost-Wakeup Problem A situation where one or more threads wait forever without realizing that the condition had become true. Two simple programming practices that will minimize vulnerability: Always signal all processes waiting on a condition, not only one Specify a timeout when waiting.

Condition in Java newCondition() – creates a condition associated with a lock await() – releases the lock, gives up the processor, and later awaken and reacquire the lock signal() – awakes one waiting thread signalAll() – awakes all waiting threads

3. Readers-Writers Locks

Readers and Writers Readers – method calls which return information about the object’s state without modifying it Writers – method calls which actually modify the object. Many times there are much more readers than writers How can we use this property for optimization?

Example

Simple Readers-Writers Locks The two lock object satisfy following safety properties: No thread can acquire the write lock while any thread holds either the write lock or the read lock. No thread can acquire the read lock while any thread holds the write lock. Multiple threads may hold the read lock at the same time

Implementation A counter is used to keep track of the number of readers that have acquired the lock A Boolean field indicating whether a writer has acquired the lock

Simple Readers-Writers Locks What problem do we have?

Fair Readers-Writers Lock There are many possible ways to give the writers priority Example: FifoReadWriteLock This class ensures that once a writer calls the write lock’s lock() method, then no more readers will be able to acquire the read lock until the writer has acquired and released the write lock.

Implementation The class holds the following elements: readAcquires – counts the total number of read lock acquisitions. readReleases – counts the total number of read lock releases. A condition A private lock Each reader acquires the private lock: before incrementing readAcquires, and releasing it after it. While releasing the read lock, before it calls the associated condition’s signal() method if all readers have released their locks, and releasing it after it. Each writer reader acquires the private lock from the time they try to acquire the write lock and release after that release the write lock.

Simple Readers-Writers Locks

4. Semaphores

Semaphore - Origin One of the earliest forms of fixed railway signal is the semaphore. These signals display their different indications to train drivers by changing the angle of inclination of pivoted 'arms'. Semaphore signals were patented in the early 1840s.

Semaphores A Semaphore is a generalization of the mutual seclusion locks we describe earlier. Each Semaphore has a capacity c. Instead of allowing only one thread at a time into the critical section, it allows at most c threads.

Semaphore - Implementation The Semaphore itself is just a counter that keeps track on the number of thread that have been granted permission to enter the critical section. Provides two methods: A thread calls: acquire() to request permission to enter the critical section. If the call is about to exceed c, the calling thread is suspended until there is room. release() to announce that it is leaving the critical section. The call notifies a waiting thread that there is now room.

5. Locks and Conditions – Example of Usage in Code

Reentrant Lock In simple locks, a thread that attempts to reacquire a lock (during nested calls) it already holds will deadlock with itself. A lock is reentrant if it can be acquired multiple times by the same thread. We will implement a reentrant lock from a non-reentrant lock. In practice, the java.utils.concurrent.locks package provides reentrant lock classes.

Fields Definitions and C’tor The Owner field holds the ID of the last thread to acquire the lock The holdCount field is incremented each time the lock is acquired, and decremented each time it is released (the lock is free when the value is zero)

The Lock Method Acquires the internal lock. If the current thread is already the owner, it increments the hold count and returns. Otherwise, if the hold counter is not zero, the caller releases the lock and waits until the condition is signaled. When the caller awakens, it must still check that the hold count is zero. When the hold count is established to be zero, the calling thread makes itself the owner and sets the hold count to 1. Releases the internal lock.

The Unlock Method Acquires the internal lock. It throws an exception if either the lock is free, or the caller is not the owner. Otherwise, it decrements the hold counter. If the hold counter is zero, the caller signals the condition to wake up a waiting thread.