SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.

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.
– 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.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 6: Process Synchronization
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Monitors and Blocking Synchronization By Tal Walter.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Fundamentals of Python: From First Programs Through Data Structures
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Internet Software Development More stuff on Threads Paul Krause.
Experience with Processes and Monitors in Mesa
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Internet Software Development Controlling Threads Paul J Krause.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
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.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
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.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
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.
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.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Tutorial 2: Homework 1 and Project 1
Multithreading / Concurrency
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Multithreading.
Producer-Consumer Problem
Multithreading.
Lecture 2 Part 2 Process Synchronization
Semaphores Chapter 6.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Lecture 9 Synchronization.
CSE 153 Design of Operating Systems Winter 19
“The Little Book on Semaphores” Allen B. Downey
Software Engineering and Architecture
More concurrency issues
Synchronization and liveness
Presentation transcript:

SPL/2010 Guarded Methods and Waiting 1

SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread computation (pre-condition is not met) 2

SPL/2010 Overview ● Techniques to deal with potential failures ● check all invariants and preconditions before performing action ● refuse to perform actions unless they can ensure that these actions will succeed ● exceptions, exception handling code 3

SPL/2010 4

Policies for failed preconditions/invariants ● Balking. throw exception if precondition fails. try, and if you cannot succeed, fail fast ● Guarded suspension. suspend method invocation (and thread) until precondition becomes true. ● Time-outs. between balking and suspension. Bounded wait for precondition to become true. 5

SPL/2010 Today ● guarded suspension ● general suspension mechanisms in Java RTE 6

SPL/2010 Guarded suspension and time-outs in concurrent software ● abstract queue supports add() get() size() 7

SPL/2010 Guards = special conditionals ● Conditionals in sequential programs: ● if statement check condition on method entry ● if false - no wait - can never become true 8

SPL/2010 Queue impl. Balking 9

SPL/2010 Guarded suspension ● asynchronous state changes can happen ● precondition may hold in the future ● wait until precondition holds ● guard asserts that another thread will make required state changes… ● Time-outs - soft guards - balking policy if wait for too long 10

SPL/

SPL/2010 Java Mechanics for Guarded Suspension ● Wait/Notify constructs - simple, mechanism for suspension in multi-threaded context 1. mechanism semantics 2. higher level constructs to simplify usability 12

SPL/2010 wait() ● threads may wait() until some other thread wakes them up ● each object has a wait set - similar to locks ● entities possessing locks and wait sets are called monitors ● maintained internally by the JVM ● set holds threads blocked by wait on object ● until notifications are invoked or waits released ● threads enter the queue of an object, o, by invoking the wait() method of o. A thread wishing to wakeup threads from the queue of o may call o.notify() (or o.notifyAll()) to wakeup just one thread (resp. all of the threads) waiting in the wait set of o. 13

SPL/2010 notify() ● threads enter wait queue of object o by invoking the wait() of o. ● thread wakeups threads from queue of o by: o.notify()/o.notifyAll() 14

SPL/2010 wait/notify cycle ● o.wait() - wait invocation ● current thread calling wait() is blocked. ● JVM places thread in wait set of o. ● o.notify() - notify invocation ● arbitrarily thread T is removed from o’s wait set ● T is resumed from the point of wait(). ● o.notifyAll() ● like notify except for all threads in o’s wait set 15

SPL/2010 Interrupting Threads ● Special JVM exception to threads: ● thread may need to stop waiting for a notify() (example when program needs to exit) ● InterruptedException – wait() method exits immediately 16

SPL/2010 Handle InterruptedException 17 ?

SPL/2010 Back to queue ● Producer-Consumer design pattern ● Producer produces objects /Consumer needs process ● decouple processes that produce and consume data at different rates ● queue data in producer loop / process in consumer loop at own pace 18

SPL/

SPL/2010 ● video player example: ● Producer read the file from disk and give chunks of data to Consumer – add() ● Consumer decodes and play – take() ● Guard add() / take() methods, such that all preconditions are respected – wait() until the preconditions hold. 20

SPL/2010 Producer-Consumer Queue 21

SPL/

SPL/2010 ● thread trying to get() when queue is empty will wait in wait set of queue ● thread which successfully add()s an object to queue will wake him up (and vice versa) ● What is wrong? ● No synchronization 23

SPL/2010 What is wrong? Guard atomicity ● two Consumer threads and one producer The ● queue is empty at first ● consumers execute take() - block (size=0) ● producer calls add() once, notifyAll() ● both consumers wake up. both execute vec_.remove(0) ● precondition does not hold for one of them 24

SPL/2010 What is wrong? Wait atomicity ● one producer and one consumer ● first runs the consumer, calls take() ● consumer checks condition, and is stopped right after (scheduler) ● producer calls add() successfully – calls notifyAll() - no thread in wait set at that time ● consumer is resumed and calls this.wait() ● queue not empty-consumer miss notification. 25

SPL/2010 Guard Atomicity - while loop 26

SPL/2010 Solution? ● while loop ensures after waking up, checks precondition one more time ● no synchronization ● use notify() instead of notifyAll() ? dangerous... 27

SPL/2010 Wait Atomicity ● ensure no one calls notify() between precondition check and wait. ● make both check and wait atomic with relation to notify mechanism. ● use single lock to protect them (?) 28

SPL/2010 Wait Atomicity ● thread get hold of a lock ● check ● wait() - enter a wait set ● no other thread may get lock and wake us up! 29

SPL/2010 Wait Atomicity ● Java solution: ● call to wait() or notifyAll() - calling thread must hold object's monitor (lock). ● enter wait() - thread (involuntarily) releases lock ● before exit wait() - thread must relock object (why?) 30

SPL/

SPL/2010 Wait/Notify Mechanism ● thread T calls o.wait() ● JVM checks that T holds o's lock (if not, exception IllegalMonitorState). ● T is blocked, and lock of o is released. ● JVM places T in the wait set associated with o. 32

SPL/2010 Wait/Notify Mechanism ● thread T calls o.notify() ● JVM checks that T holds o's lock. if not exception is thrown. ● arbitrarily thread T' is removed from wait set ● T' tries to re-acquire the lock of o (T' blocks until it does). ● T' is resumed from the point of its wait(). 33

SPL/2010 Wait/Notify Mechanism ● o.notifyAll() - steps occur simultaneously for all threads in wait set of o. ● Thundering Herd phenomena – all threads try to acquire the lock competing with each other. ● holding the monitor's lock check is done at runtime and not at compile time. 34

SPL/

SPL/

SPL/2010 Rules of Thumb for Wait and Notify ● Guarded wait - for each condition that needs to be waited on, write a guarded wait loop that causes the current thread to block if the guard condition is false. 37

SPL/2010 Rules of Thumb for Wait and Notify ● Guard atomicity - Condition checks must always be placed in while loops. ● When action is resumed, waiting thread doesn't know if condition is true ● to maintain safety, it must check again. ● thread can wake up without notify, interrupt, or timing out - spurious wakeup. 38

SPL/2010 Rules of Thumb for Wait and Notify ● Multiple guard atomicity If there are several conditions which need to be waited for, place them all in the same loop. 39

SPL/2010 Rules of Thumb for Wait and Notify ● Don't forget to wake up - ensure liveness, classes must wake up waiting threads. ● when value of field in guard changes - waiting threads must be awakened to recheck conditions 40

SPL/2010 Rules of Thumb for Wait and Notify ● notifyAll() - multiple threads wait on same wait set: ● at least two threads waiting for different conditions, ● ensure that no thread misses an event. 41

SPL/2010 notify() Vs. notifyAll(): ● queue which has add() / addTwo() methods ● Thread t1 waits for queue in add() checking room for one object, ● thread t2 waits in addTwo() checking room for two objects. ● get() method calls notify() and thread t2 wakes up ● room for one object in queue, t2 goes back to sleep. ● t1 will not be woken up at all, even though there is space in the queue. 42

SPL/2010 Sophisticated Primitives ● Wait and Notify: ● basis for higher level constructs. ● general constructs ● Specific mechanisms make code easier to understand and demand less details when using them: ● Semaphores ● Readers-Writers locks. 43

SPL/2010 Semaphores ● Object controls bounded number of permits (permit = train ticket): ● Threads ask semaphore for permit (a ticket). ● If semaphore has permits available, one permit is assigned to requesting thread. ● If no permit available, requesting thread is blocked until permit is available – when a thread returns permit received earlier 44

SPL/2010 implementation 45

SPL/2010 Java Semaphore class properties ● not re-entrant – thread calls acquire() twice, must release() twice! ● semaphore can be released by a thread other than owner (unlike lock) – no ownership. ● constructor accepts fairness parameter – if t1 requested before t2 it will receive first ● services as tryAcquire and managing permits. 46

SPL/2010 SimpleQueue vs. Semaphore ● SQ: add(),remove() call notifyAll() call. ● Sem: release() calls notifyAll() method acquire() does not. ● SQ:add(), remove() call wait() ● Sem: method acquire() calls wait() method release() does not. 47

SPL/2010 SimpleQueue vs. Semaphore ● SQ: add(),remove() - non-empty preconditions correspond to 2 distinct states of the Queue: empty or full. ● Sem: acquire() has pre-condition and release() has no pre-condition. ● release() does not need to call wait(). ● acquire() call wait() - symmetric call in release() for notifyAll() (for each wait() - corresponding notifyAll() that unblocks). 48

SPL/2010 Readers/Writers Example ● allow readers and writers to access a shared resource under different policies ● Policy: ● several readers can access resource together ● only one writer can access resource at given time ● Example: – Readers wait if writer is pending – One writer at a time can access the shared resource, if no readers are reading from it 49

SPL/

SPL/