Monitors & Condition Synchronization

Similar documents
Monitors & Condition Synchronisation

CSCI 5828: Foundations of Software Engineering

Monitors & Condition Synchronization

Lecture 10: Introduction to Semaphores

Monitors & Condition Synchronization

C340 Concurrency: Semaphores and Monitors. Goals

Lecture 9: Introduction to Monitors

Chapter 6. Deadlock. DM519 Concurrent Programming

COMP30112: Concurrency Topics 5.2: Properties

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

CONCURRENT PROGRAMMING - EXAM

Communications Software Engineering Condition Synchronization and Liveness

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Safety & Liveness Properties

Faculty of Computers & Information Computer Science Department

Monitors; Software Transactional Memory

Monitors; Software Transactional Memory

Part IV Other Systems: I Java Threads

$ Program Verification

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

EECS 482 Introduction to Operating Systems

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Threads and Parallelism in Java

Concurrency: State Models & Design Patterns

CS11 Java. Fall Lecture 7

G52CON: Concepts of Concurrency

Models of concurrency & synchronization algorithms

Java Threads. COMP 585 Noteset #2 1

Shared Objects & Mutual Exclusion

Problems with Concurrency. February 19, 2014

Concurrency 6 - Deadlock

Advanced Concepts of Programming

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

What's wrong with Semaphores?

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Concurrent Programming using Threads

Multiple Inheritance. Computer object can be viewed as

Answer the first question and two further questions. i. action prefix ( -> ) [3 marks]

Lecture 8: September 30

Java Threads. Introduction to Java Threads

Two Types of Semaphores

Introduction to OS Synchronization MOS 2.3

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

COMP346 Winter Tutorial 4 Synchronization Semaphores

Lecture 21: Concurrency in Other Environments (Part 2)

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

1 Process Coordination

Question Points Score Total 100

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

Introduction to Java Threads

Resource management. Real-Time Systems. Resource management. Resource management

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

CS 159: Parallel Processing

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Outline. Monitors. Barrier synchronization The sleeping barber problem Readers and Writers One-way tunnel. o Monitors in Java

Performance Throughput Utilization of system resources

Animation Part 2: MoveableShape interface & Multithreading

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

CS3502 OPERATING SYSTEMS

Synchronization in Java

Programming Language Concepts: Lecture 11

Question Points Score Total 100

Programming in Parallel COMP755

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Process Management And Synchronization

Lecture 6 (cont.): Semaphores and Monitors

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Shared-Memory and Multithread Programming

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Synchronization COMPSCI 386

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Synchronization 1. Synchronization

Only one thread can own a specific monitor

Threads Chate Patanothai

EECS 482 Introduction to Operating Systems

Lecture 29: Java s synchronized statement

Component-Based Software Engineering

Synchronization Lecture 24 Fall 2018

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

CS193k, Stanford Handout #8. Threads 3

Computer Networks. Process Cooperation Multithreaded Programming in Java. Laszlo Böszörmenyi Computer Networks Processes - 1

Model Requirements and JAVA Programs MVP 2 1

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Module - 4 Multi-Threaded Programming

RACE CONDITIONS AND SYNCHRONIZATION

Exercises and Labs. Part I. Exercises

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Reminder from last time

3C03 Concurrency: Starvation and Deadlocks

CSE 153 Design of Operating Systems

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Race Conditions & Synchronization

Transcription:

Chapter 5 Monitors & Condition Synchronization controller 1

Monitors & Condition Synchronization Concepts: monitors (and controllers): encapsulated data + access procedures + mutual exclusion + condition synchronization + single access procedure active in the monitor nested monitors ( nested monitor problem ) Models: guarded actions Practice: private data and synchronized methods (exclusion). wait(), notify() and notifyall() for condition synch. single thread active in the monitor at a time 2

Condition Synchronization 3

5.1 Condition Synchronization (Car Park) A controller is required to ensure: cars can only enter when not full cars can only leave when not empty (duh!) 4

Car Park Model (Actions and Processes) Actions of interest: arrive depart Identify processes: Arrivals Departures env Carpark (Control) 5

Car Park Model (Structure Diagram) Actions of interest: arrive depart Identify processes: Arrivals Departures env Carpark (Control) CARPARK ARRIVALS arrive CARPARK (CONTROL) depart DEPARTURES 6

Car Park Model (FSP) CARPARK ARRIVALS = (arrive -> ARRIVALS). ARRIVALS arrive CARPARK (CONTROL) depart DEPARTURES DEPARTURES = (depart -> DEPARTURES). CONTROL(CAPACITY=4) = SPACES[CAPACITY], SPACES[spaces:0..N] = (when(spaces>0) arrive -> SPACES[spaces-1] when(spaces<capacity) depart -> SPACES[spaces+1]). CARPARK = (ARRIVALS DEPARTURES CONTROL(4)). Guarded actions are used to control arrive and depart LTS? What if we remove ARRIVALS and DEPARTURES? 7

Car Park Program Model: all entities are processes interacting via shared actions Implementation: we need to identify threads and monitors: thread - active entity which initiates (output) actions monitor - passive entity which responds to (input) actions. Arrivals: Departures: Control: For the carpark? active => thread active => thread passive => monitor 8

Car Park Program (Interesting part of Class Diagram) Runnable Active (thread) Arrivals Departures Active (thread) carpark carpark Control arrive() depart() Passive (monitor) 9

Car Park Program - Main The main() method creates: Control monitor Arrival thread Departures thread Arrivals Runnable public static void main(string[] args) { Control c = new Control(CAPACITY); arrivals = new Thread(new Arrivals(c)); departures = new Thread(new Departures(c)); arrivals.start(); departures.start(); Departures carpark carpark Control arrive() depart() The Control is shared by Arrival and Departures threads 10

Car Park Program - Arrivals ARRIVALS = (arrive -> ARRIVALS). class Arrivals implements Runnable { Control carpark; Arrivals(Control c) { carpark = c; public void run() { try { while(true) { Would like to Thread.sleep(...); carpark.arrive(); somehow block Arrival thread catch (InterruptedException _) here { similarly for Departures (calling carpark.depart()) Where should we do the blocking? How do we implement the Carpark Controller s control? 11

Control Monitor CONTROL(CAPACITY=4) = SPACES[CAPACITY], SPACES[spaces:0..CAPACITY] = (when(spaces>0) arrive -> SPACES[spaces-1] when(spaces<capacity) depart -> SPACES[spaces+1]). class Control { Encapsulation protected static final int CAPACITY; int spaces; ~ protected protected Control(int n) { CAPACITY = spaces = n; synchronized void arrive() {... --spaces;... synchronized void depart() {... ++spaces;... Mutual exclusion ~ synchronized Condition synchronization: Block, if full? (spaces>0) Block, if empty? (spaces<capacity) 12

Condition Synchronization in Java Java provides a thread wait queue per object (not per class). Object has methods: public final void wait() throws InterruptedException; Waits to be notified ; Releases the synchronization lock associated with the obj. When notified, the thread must reacquire the synchr. lock. public final void notify(); public final void notifyall(); Wakes up (notifies) thread(s) waiting on the object s queue. 13

Condition Synchronization in Java (enter/exit) A thread: Enters a monitor when a thread acquires the lock associated with the monitor; Exits a monitor when it releases the lock. Wait() causes the thread to exit the monitor, permitting other threads to enter the monitor Thread A notify() Monitor data wait() Thread B 14

Condition Synchronization in FSP and Java FSP: when (cond) action -> NEWSTATE synchronized void action() throws Int Exc { while if (!cond) wait(); // modify monitor data notifyall(); The while loop is necessary to re-test the condition cond to ensure that cond is indeed satisfied when it re-enters the monitor. notifyall() is necessary to awaken other thread(s) that may be waiting to enter the monitor now that the monitor data has been changed. 15

CarParkControl - Condition Synchronization CONTROL(CAPACITY=4) = SPACES[CAPACITY], SPACES[spaces:0..CAPACITY] = (when(spaces>0) arrive -> SPACES[spaces-1] when(spaces<capacity) depart -> SPACES[spaces+1]). class Control { protected static final int CAPACITY; protected int spaces; synchronized void arrive() throws Int Exc { while (!(spaces>0)) wait(); --spaces; notifyall(); synchronized void depart() throws Int Exc { while (!(spaces<capacity)) wait(); ++spaces; notifyall(); Would it be sensible here to use notify() rather than notifyall ()? 16

More about Object.notify() and Object.notifyAll() notify() can be used instead of notifyall() only when both of these conditions hold: Uniform waiters. Only one condition predicate and each thread executes the same logic upon returning from wait(); and One-in, one-out. A notification enables at most one thread to proceed. Prevailing wisdom: use notifyall() in preference to single notify() when you are not sure. 17

Models to Monitors - Guidelines Active entities (that initiate actions) are implemented as threads. Passive entities (that respond to actions) are implemented as monitors. Each guarded action in the model of a monitor is implemented as a synchronized method which uses a while loop and wait() to implement the guard. The while loop condition is the negation of the model guard condition. Changes in the state of the monitor are signalled to waiting threads using notifyall() (or notify()). 18

Semaphores 19

5.2 Semaphores Semaphores are widely used for dealing with inter-process synchronization in operating systems. Semaphore s : integer var that can take only non-neg. values. s.down(): when s>0 do decrement(s); Aka. P ~ Passern s.up(): increment(s); Aka. V ~ Vrijgeven Usually implemented as blocking wait: s.down(): if (s>0) then decrement(s); else block execution of calling process s.up(): if (processes blocked on s) then awake one of them else increment(s); 20

Modelling Semaphores To ensure analyzability, we only model semaphores that take a finite range of values. If this range is exceeded then we regard this as an ERROR. const Max = 3 range Int = 0..Max SEMAPHORE(N=0) = SEMA[N], // N initial value SEMA[v:Int] = (up->sema[v+1] when(v>0) down->sema[v-1]), SEMA[Max+1] = ERROR. LTS? What if we omit the last line above? 21

Modelling Semaphores Action down is only accepted when value (v) of the semaphore is greater than 0. Action up is not guarded. Trace to a violation: up up up up 22

Semaphore Demo - Model SEMAPHORE(N=0) = SEMA[N], // N initial value SEMA[v:Int] = (up->sema[v+1] when(v>0) down->sema[v-1]), Three processes p[1..3] use a shared semaphore mutex to ensure mutually exclusive access (action critical ) to some resource. LOOP = (mutex.down->critical->mutex.up->loop). SEMADEMO = (p[1..3]:loop {p[1..3]::mutex:semaphore(1)). For mutual exclusion, the semaphore initial value is 1. Why? Is the ERROR state reachable for SEMADEMO? Is a binary semaphore sufficient (i.e. Max=1)? LTS? 23

Semaphore Demo - Model 24

Semaphores in Java SEMA[v:Int] = (when(v>0) down->sema[v-1] up->sema[v+1]), public class Semaphore { protected int value; public Semaphore (int n) { value = n; synchronized public void down() throws Int Exc { while (!(value > 0)) wait(); --value; notifyall(); Do we need notifyall() here? synchronized public void up() { ++value; notifyall(); what about here? 25

SEMADEMO Display 26

SEMADEMO Program - MutexLoop LOOP = (mutex.down->critical->mutex.up->loop). class MutexLoop implements Runnable { Semaphore mutex; // shared semaphore MutexLoop (Semaphore sem) { mutex=sem; public void run() { try { while(true) { // non-critical action mutex.down(); // critical action mutex.up(); catch(interruptedexception _) { // acquire // release However (in practice), semaphore is a low-level mechanism often used in implementing higher-level monitor constructs. 27

Producer / Consumer 28

5.3 Producer / Consumer A bounded buffer consists of a fixed number of slots. Items are put into the buffer by a producer process and removed by a consumer process: Car Park Example! 29

Producer / Consumer - a Data-Independent Model BOUNDEDBUFFER PRODUCER put BUFFER get CONSUMER The behaviour of BOUNDEDBUFFER is independent of the actual data values, and so can be modelled in a data-independent manner (i.e., we abstract away the letters). LTS? 30

Producer / Consumer PRODUCER = (put->producer). CONSUMER = (get->consumer). BOUNDEDBUFFER PRODUCER put BUFFER get CONSUMER BUFFER(SIZE=5) = COUNT[0], COUNT[count:0..SIZE] = (when (count<size) put -> COUNT[count+1] when (count>0) get -> COUNT[count-1]). BOUNDEDBUFFER = (PRODUCER BUFFER CONSUMER). 31

Bounded Buffer Program - Buffer Monitor BUFFER(SIZE=5) = COUNT[0], COUNT[count:0..SIZE] = (when (count<size) put -> COUNT[count+1] when (count>0) get -> COUNT[count-1]). public interface Buffer { public void put(object o) throws InterruptedException; public Object get() throws InterruptedException; class BufferImpl implements Buffer { protected Object[] queue; protected int in, out, count, SIZE; synchronized void put(object o) throws Int Exc { while (!(count<size)) wait(); queue[in] = o; count++; in = (in+1) % SIZE; notifyall(); if(count++ == 0) Can we use notify()? 32

Similarly for get() BUFFER(SIZE=5) = COUNT[0], COUNT[count:0..SIZE] = (when (count<size) put -> COUNT[count+1] when (count>0) get -> COUNT[count-1]). public interface Buffer { public void put(object o) throws InterruptedException; public Object get() throws InterruptedException; synchronized Object get() throws Int Exc { 1. while (!(count>0)) wait(); 2. Object obj = queue[out]; < 2½. queue[out] = null; // WHY(?) 3. count--; 4. out = (out+1) % SIZE; 5. notifyall(); 6. return obj; if(count-- == queue.length) 33

Producer Process PRODUCER = (put->producer). class Producer implements Runnable { Buffer buf; String alpha = "abcdefghijklmnopqrstuvwxyz"; Producer(Buffer b) { buf = b; public void run() { try { int i = 0; while(true) { Thread.sleep(...); buf.put(new Character(alpha.charAt(i))); i=(i+1) % alpha.length(); catch (InterruptedException _) { Similarly Consumer which calls buf.get () 34

The Nested Monitor Problem 35

5.4 Nested Monitors (Semaphores) Suppose that, instead of using the count variable and condition synchronization, we instead use 2 semaphores full and empty to reflect the state of the buffer: class SemaBuffer implements Buffer { protected Object queue[]; protected int in, out, count, SIZE; Semaphore empty; // block get appropriately Semaphore full; // block put appropriately SemaBuffer(int s) { size = s; in = out = count = 0; queue = new Object[SIZE]; empty = new Semaphore(0); full = new Semaphore(SIZE); 36

Nested Monitors Program synchronized public void put(object o) throws Int Exc { full.down(); full is decremented during a put, queue[in] = o; which is blocked if full is zero. count++; in = (in+1) % SIZE; empty.up(); synchronized public Object get() throws Int Exc { empty.down(); Object o = queue[out]; empty is decremented by a get, queue[out] = null; count--; which is blocked if empty is zero. out = (out+1) % SIZE; full.up(); return o; Does this behave as desired? 37

Nested Monitors Model PRODUCER = (put -> PRODUCER). CONSUMER = (get -> CONSUMER). SEMAPHORE(N=0) = SEMA[N], SEMA[v:Int] = (when(v>0) down->sema[v-1] up->sema[v+1]). BUFFER = (put -> full.down -> p -> empty.up -> BUFFER get -> empty.down -> g -> full.up -> BUFFER). BOUNDEDBUFFER = ( PRODUCER BUFFER CONSUMER full:semaphore(5) empty:semaphore(0) ). Does this behave as desired? synchronized public void put(object o) throws Int Exc { full.down(); buf[in] = o; count++; in = (in+1) % size; empty.up(); 38

Nested Monitors LTSA analysis predicts a DEADLOCK: synchronized public Object get() throws Int Exc { empty.down(); Object o = queue[out]; queue[out] = null; count--; out = (out+1) % size; full.up(); return o; Composing potential DEADLOCK States Composed: 28 Transitions: 32 in 60ms Trace to DEADLOCK: get 1) Initially Consumer calls SemaBuffer.get(), acquiring a synchr lock (on the buffer); 2) Semaphore.down() - acquires another synchronization lock (on the Semaphore); 3) Semaphore.down() - releases its lock using wait() (but only this lock)! This situation is known as the nested monitor problem! 39

Nested Monitors - Revised Bounded Buffer Program The only way to avoid it in Java is by careful design : synchronized public void put(object o) throws Int Exc { full.down(); queue[in] = o; count++; in = (in+1) % SIZE; empty.up(); public void put(object o) throws Int Exc { full.down(); synchronized (this) { queue[in] = o; count++; in = (in+1) % SIZE; empty.up(); In this example, the deadlock can be removed by ensuring that the monitor lock for the buffer is not acquired until after semaphores are decremented. 40

Nested Monitors - Revised Bounded Buffer Model The semaphore actions have been moved outside the monitor (i.e., conceptually, to the producer and consumer): BUFFER = (put -> BUFFER get -> BUFFER). PRODUCER = (full.down -> put -> empty.up -> PRODUCER). CONSUMER = (empty.down -> get -> full.up -> CONSUMER). Does this behave as desired? No deadlocks/errors 41

5.5 Monitor invariants An invariant for a monitor is an assertion concerning the variables it encapsulates. This assertion must hold whenever there is no thread executing inside the monitor i.e. on thread entry to and exit from a monitor. INV(CarParkControl): INV(Semaphore): INV(Buffer): 0 spaces CAPACITY 0 value 0 count SIZE and 0 in < SIZE and 0 out < SIZE and in = (out + count) % SIZE Like normal invariants, but must also hold when lock is released (wait)! 42

Summary Concepts: monitors: encapsulated data + access procedures + mutual exclusion + condition synchronization + single access procedure active in the monitor nested monitors Models: guarded actions Practice: private data and synchronized methods (exclusion). wait(), notify() and notifyall() for condition synch. single thread active in the monitor at a time 43