Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.

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

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
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.
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
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
Chapter 3 The Critical Section Problem
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Monitors and Blocking Synchronization By Tal Walter.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems 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,
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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:
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Programming Language Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
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.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
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.
Deadlock and Starvation
Multithreading / Concurrency
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Concurrent Objects Companion slides for
An object-based synchronization mechanism.
Multithreading.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Multithreading.
Semaphores Chapter 6.
CS333 Intro to Operating Systems
CSE 451 Section 1/27/2000.
Synchronization and liveness
Presentation transcript:

Monitors & Blocking Synchronization 1

Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t do it simultaneously. 2

1. share two objects: - unsynchronized queue - lock to protect the queue. The producer looks like : Sounds right?? 1. Sharing Everything (1) ©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 3

1. Sharing Everything (2) what if the queue is bounded ? The decision whether to block the call or let it proceed depends on the queue’s internal state. Everybody must keep track of both the lock and the queue objects. The app correctness depends on every thread following the same locking conventions. 4

2. Data Structure Manages it’s own synchronization (1) The queue has it’s internal lock, acquired by each method when it is called and released when it returns. If a thread tries to enqueue an item to a full queue - the enq() can detect the problem, suspend the caller, and resume when the queue has room. 5

We want to synchronize the actions while keeping the correctness of the structure (!!) While a thread is waiting for another thread to place an item in an empty queue, it needs to release the lock. Afterwards it needs a way to be notified when to reacquire the lock(when there is a chance that the queue in not empty). 2. Data Structure Manages it’s own synchronization (2) 6

Condition Objects The ability to release a lock temporarily is provided by the Condition object, associated with a lock. Behind the scenes: - A thread acquires a lock. - If the condition hasn’t been met– the thread releases the lock and waits. - When the condition has been met – all the waiting threads are awakened. - When it is awakened – tries to win again the lock(maybe competing with other threads). 7

Example: ©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 8

[ Capacity ]; ©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 9

10

Optimization There is no need to signal() every time after enq() – only when the queue actually transitions from empty to non- empty. 11

©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 12

Weak spot – Lost Wakeups As locks vulnerable to deadlocks, Condition objects vulnerable to lost wakeups, in which one or more threads wait forever without realizing that the condition has become true. Solutions (either works here): 1. always signal all processes waiting on a condition, not just one. 2.Specify a time out when waiting 13

A combination of methods, mutual exclusion locks and condition objects is called a MONITOR 14

Readers & Writers 15

Readers Writers Problem Many shared objects have the property that most method calls return information about the object without modifying it (READERS), while only a small number of calls actually modify the object (WRITERS). There is no need for readers to synchronize with one another. Writers on the other hand, must lock out readers as well as other writers. 16

Implementation interface: No thread can acquire the write lock while any thread holds wither the write lock or the read lock No thread can acquire the read lock while any thread holds the write lock 17

©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 18

©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 19

> 0 || writer ) { ©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 20

Starvation!!! The code works – but there is: Starvation of the writers!!! If readers are much more frequent than writers, as is usually the case, then writers could be locked out for a long time by continual stream of readers. 21

Giving writers priority One way to give writers priority is to ensure that once a writer calls the writer’s lock method, then no more readers will be able to acquire the read lock until the writer has done with his writing. Eventually, the readers holding the read lock will drain out without letting any more readers in, and the writer will acquire the write lock. 22

} ©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 23

(writer) { while ( readers > 0 ) { condition.await(); } ©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 24

Reentrant Lock Thread that attempts to reacquire a lock it already holds will deadlock with itself. When do you think this situation can happen? As we said before, a thread that is holding the reentrant lock can acquire it again without blocking. There is an importance to thread’s identity. An example of implementation reentrant lock with a non-reentrant lock. 25

©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 26

©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit 27

Before: After: Hope you enjoyed the lesson! 28