3C03 Concurrency: Condition Synchronisation

Similar documents
Monitors & Condition Synchronization

Lecture 6: Introduction to Monitors and Semaphores

Chapter 8 Implementing FSP Models in Java

Outline of this lecture G52CON: Concepts of Concurrency

Parallel Programming

Mutual Exclusion using Monitors

Chapter 6 Concurrent Programming

Resurrecting Ada s Rendez-Vous in Java

Last Class: Semaphores

CS11 Java. Fall Lecture 7

! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization

Monitors, Java, Threads and Processes

Semaphores and Monitors: High-level Synchronization Constructs

Concurrent programming in Java

Lecture 8: Safety and Liveness Properties

Lecture 6: Semaphores and Monitors

Concurrent Programming

4. Monitors. Monitors support data encapsulation and information hiding and are easily adapted to an object-oriented environment.

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

Monitors and Semaphores

Deadlock Victim. dimanche 6 mai 12

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

TESTING JAVA MONITORS BY STATE SPACE EXPLORATION MONICA HERNANDEZ. Presented to the Faculty of the Graduate School of

Threads 1. When writing games you need to do more than one thing at once.

Java Memory Model: Content

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Thread Synchronization and the Java Monitor

Hoare-Style Monitors for Java

Multithreaded Programming

Computer Science 483/580 Concurrent Programming Midterm Exam February 23, 2009

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Priority Inversion Problem and Deadlock Situations

Design Patterns in C++

Advanced Multiprocessor Programming

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

COS 318: Operating Systems. Semaphores, Monitors and Condition Variables

Distributed Systems [Fall 2012]

Concurrent Programming

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

Traditional Software Development. Model Requirements and JAVA Programs. Formal Verification & Validation. What is a state?

Operating Systems and Networks

JAVA - MULTITHREADING

COMMMUNICATING COOPERATING PROCESSES

Real Time Programming: Concepts

Java Concurrency Framework. Sidartha Gracias

Paper 3F6: Software Engineering and Systems Distributed Systems Design Solutions to Examples Paper 3

Lecture 9 verifying temporal logic

KWIC Implemented with Pipe Filter Architectural Style

Threads & Tasks: Executor Framework

SYSTEM ecos Embedded Configurable Operating System

Java Coding Practices for Improved Application Performance

Concepts of Concurrent Computation

Chapter 6, The Operating System Machine Level

Comp 204: Computer Systems and Their Implementation. Lecture 12: Scheduling Algorithms cont d

Overview. CMSC 330: Organization of Programming Languages. Synchronization Example (Java 1.5) Lock Interface (Java 1.5) ReentrantLock Class (Java 1.

Tuple spaces and Object spaces. Distributed Object Systems 12. Tuple spaces and Object spaces. Communication. Tuple space. Mechanisms 2.

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

OBJECT ORIENTED PROGRAMMING LANGUAGE

Java SE 7 Programming

Socket-based Network Communication in J2SE and J2ME

Shared Address Space Computing: Programming

Figure 30.1: A Parent Waiting For Its Child What we would like to see here is the following output:

The ConTract Model. Helmut Wächter, Andreas Reuter. November 9, 1999

Java the UML Way: Integrating Object-Oriented Design and Programming

QoS for (Web) Applications Velocity EU 2011

Getting Started with the Internet Communications Engine

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Distributed Transactions

Java SE 8 Programming

CS Fall 2008 Homework 2 Solution Due September 23, 11:59PM

Atomicity for Concurrent Programs Outsourcing Report. By Khilan Gudka Supervisor: Susan Eisenbach

Green Telnet. Making the Client/Server Model Green

Gildart Haase School of Computer Sciences and Engineering

Java Virtual Machine Locks

Introduction to LabVIEW Design Patterns

Facing the Challenges for Real-Time Software Development on Multi-Cores

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Computer Systems Engineering: Spring Quiz I Solutions

Creating a Simple, Multithreaded Chat System with Java

W4118 Operating Systems. Instructor: Junfeng Yang

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

Java SE 7 Programming

Transcription:

3C03 Concurrency: Condition Synchronisation Mark Handley 1 Goals n Introduce concepts of Condition synchronisation Fairness Starvation n Modelling: Relationship between guarded actions and condition synchronisation? n Implementation: Condition Monitors in Java, Semaphores as Java Monitors 2 1

Thread Waiting Queues in Java n public final void notify() Wakes up a single thread that is waiting on this object s queue n public final void notifyall() Wakes up all threads that are waiting on this object s queue n public final void wait() throws InterruptedException Waits to be notified by another thread. When notified must reacquire monitor. 3 Condition synchronisation in Java n Thread enters monitor when it acquires mutual exclusion lock of monitor n Thread exits monitor when releasing lock n Wait causes thread to exit monitor Monitor data Wait() Notify() 4 2

Semaphore as a Java Monitor class Semaphore { private int value_; Semaphore (int initial) { value_=initial; public synchronised down() { while (value_==0) wait(); --value; public synchronised up() { ++value_; notify(); 5 Condition Synchronisation in Java n FSP Model: when cond act -> NEWSTATE n Java: public synchronized void act() throws InterruptedException { while (! cond) wait(); act notifyall(); n Loop re-tests cond to make sure that it is valid when it re-enters the monitor 6 3

CarParkControl revisited CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive-> SPACES[i-1] when(i<n) depart-> SPACES[i+1] ). ARRIVALS = (arrive-> ARRIVALS). DEPARTURES = (depart-> DEPARTURES). CARPARK = (ARRIVALS CARPARKCONTROL DEPARTURES). 7 CarParkControl revisited class CarParkControl { private int spaces; private int N; synchronized public void arrive() { while (spaces<=0) { try { wait(); catch(interruptedexception e){ --spaces; notify(); 8 4

FSP and Condition Synchronisation For each guarded action in the FSP model of a monitor: n Implement action as a synchronised method That invokes wait() in a while loop before it begins While condition is negation of guard condition n Every change in the monitor is signalled to waiting threads using notify() or notifyall() 9 Example: Producer/Consumer Producer Buffer Consumer put get Demo 10 5

Producer Consumer in FSP PRODUCER = (put -> PRODUCER). CONSUMER = (get -> CONSUMER). BUFFER(SIZE=5) = BUFFER[0], BUFFER[count:0..SIZE] = ( when (count<size) put->buffer[count+1] when (count>0) get -> BUFFER[count-1]). PC=(PRODUCER BUFFER CONSUMER). LTSA 11 Bounded Buffer - Outline class Buffer { private Object[] buf; private int in = 0; //index for put private int out = 0; //index for get private int count = 0; //no of items private int size; Buffer(int size) { this.size = size; buf = new Object[size]; synchronized public void put(object o) { synchronized public Object get() { 12 6

Bounded Buffer - put synchronized public void put(object o) { while (count>=size) { try { wait(); catch(interruptedexception e){ buf[in] = o; ++count; in=(in+1) % size; notifyall(); 13 Bounded Buffer - get synchronized public Object get() { while (count==0) { try { wait(); catch (InterruptedException e){ Object o =buf[out]; buf[out]=null; // for display purposes --count; out=(out+1) % size; notifyall(); // [count < size] return (o); 14 7

Monitor Invariants Monitor invariant is assertion concerning attributes encapsulated by a monitor. n Assertion must hold when no thread is in monitor. Examples: CarParkControl: 0<=spaces<=N Semaphore: 0<=value BoundedBuffer: 0<=count<=size && 0<=in<=size && 0<=out<=size && in=(out+count) % size n Used to reason about correctness of monitors. 15 Nested Monitor Problem SEMAPHORE(I=0) = SEMA[I], SEMA[v:0..3] = ( up->sema[v+1] when (v>0) down -> SEMA[v-1]). BUFFER = ( put -> down -> BUFFER get -> up -> BUFFER ). PRODUCER = (put -> PRODUCER). CONSUMER = (get -> CONSUMER). BOUNDEDBUFFER = (PRODUCER CONSUMER BUFFER SEMAPHORE(3) )@{put,get. 16 8

Nested Monitors Normal sequence of events seems fine: {put {down {get {up {put {down {get {up... But what happens when: {put {down {put {down {put {down {put {down..zzz PRODUCER will now block, waiting for an up. n CONSUMER must do a get to cause an up to happen. n But the PRODUCER thread is still in the BUFFER monitor, so CONSUMER cannot run. n DEADLOCK! This is known as the nested monitors problem. 17 Nested Monitors A Thread that waits in a monitor releases only its lock, not the lock of any monitor from which it was called. n Nested monitors calls need to be used with great care. n The only way to avoid nested monitors in Java is by careful design. Modelling and analysis can aid in careful design. 18 9

Non-nested Monitors. SEMAPHORE(I=0) = SEMA[I], SEMA[v:0..3] = ( up->sema[v+1] when (v>0) down -> SEMA[v-1]). BUFFER = ( put -> BUFFER get -> BUFFER ). PRODUCER = (data.down -> put -> spaces.up -> PRODUCER). CONSUMER = (spaces.down -> get -> data.up -> CONSUMER). BOUNDEDBUFFER = (PRODUCER CONSUMER BUFFER spaces:semaphore(3) data:semaphore(0))@{put,get. 19 Summary n Condition synchronization. n In Java using wait(), notify() and notifyall() n Used to implement Semaphores in Java. n Relationship between an FSP model and its implementation as a Java monitor. n Monitor invariants. n Nested Monitor Problem. 20 10