INF Models of concurrency

Similar documents
INF Models of concurrency

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Extensibility Patterns: Extension Access

INF 4140: Models of Concurrency Series 3

Operating Systems. VII. Synchronization

INF 4140: Models of Concurrency Series 4

INF Models of concurrency

Improper Nesting Example

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

Real Time Operating Systems

Lecture 4: Process Management

ITI Introduction to Computing II

Lab Course: distributed data analytics

CIS 4930/6930: Principles of Cyber-Physical Systems

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Real Time Operating Systems

ITI Introduction to Computing II

Design Patterns and Refactoring

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

Unit: Blocking Synchronization Clocks, v0.3 Vijay Saraswat

Java Programming. Final Examination on December 13, 2015 Fall 2015

INF Models of concurrency

Latches. October 13, 2003 Latches 1

Shared resources. Sistemi in tempo reale. Giuseppe Lipari. Scuola Superiore Sant Anna Pisa -Italy

CS-206 Concurrency. Lecture 8 Concurrent. Data structures. a b c d. Spring 2015 Prof. Babak Falsafi parsa.epfl.ch/courses/cs206/ remove(c) remove(b)

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Correctness of Concurrent Programs

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

process arrival time CPU burst time priority p1 0ms 25ms 3 p2 1ms 9ms 1 p3 20ms 14ms 4 p4 32ms 4ms 2

Techniques of Java Programming

Lecture 16 More Profiling: gperftools, systemwide tools: oprofile, perf, DTrace, etc.

Solutions to the exam in Real-Time Systems

Multicore Semantics and Programming

Announcements. Project #1 grades were returned on Monday. Midterm #1. Project #2. Requests for re-grades due by Tuesday

Disproof in Jape (a short how-to note)

CS/IT OPERATING SYSTEMS

Tou has been released!

Clock-driven scheduling

Lists, Stacks, and Queues (plus Priority Queues)

Modern Functional Programming and Actors With Scala and Akka

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

Information System Design IT60105

Lecture Note #6: More on Task Scheduling EECS 571 Principles of Real-Time Embedded Systems Kang G. Shin EECS Department University of Michigan

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Actors for Reactive Programming. Actors Origins

CS Java. Introduction to Java. Andy Mroczkowski Department of Computer Science Drexel University

Marwan Burelle. Parallel and Concurrent Programming. Introduction and Foundation

Lecture Notes Each circuit agrees with M on inputs of length equal to its index, i.e. n, x {0, 1} n, C n (x) = M(x).

CS 152 Computer Architecture and Engineering. Lecture 17: Synchronization and Sequential Consistency

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

An introduction to Uppaal and Timed Automata MVP5 1

Deadlock. CSE 2431: Introduction to Operating Systems Reading: Chap. 7, [OSC]

Deadlock (2) Dave Eckhardt Brian Railing Roger Dannenberg

TDDB68 Concurrent programming and operating systems. Lecture: CPU Scheduling II

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Distributed Architectures

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

COMPUTER SCIENCE TRIPOS

Computer Science Introductory Course MSc - Introduction to Java

Distributed Systems Part II Solution to Exercise Sheet 10

Simulation of Process Scheduling Algorithms

INF421, Lecture 2 Queues

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

Exercise 1 (15 points)

Scheduling I. Today Introduction to scheduling Classical algorithms. Next Time Advanced topics on scheduling

Lecture 4: Stacks and Queues

Thread Specific Storage

9. PSPACE 9. PSPACE. PSPACE complexity class quantified satisfiability planning problem PSPACE-complete

9. PSPACE. PSPACE complexity class quantified satisfiability planning problem PSPACE-complete

Compiling Techniques

CSE505, Fall 2012, Final Examination December 10, 2012

Computer Science Introductory Course MSc - Introduction to Java

Solutions. Prelim 2[Solutions]

Finite State Machines CS 64: Computer Organization and Design Logic Lecture #15 Fall 2018

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

CMSC 313 Lecture 15 Good-bye Assembly Language Programming Overview of second half on Digital Logic DigSim Demo

Sequential Logic. Handouts: Lecture Slides Spring /27/01. L06 Sequential Logic 1

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Factory method - Increasing the reusability at the cost of understandability

LTL Model Checking. Wishnu Prasetya.

1 Lamport s Bakery Algorithm

Lecture 13: Sequential Circuits, FSM

Introduction to Computing II (ITI1121) FINAL EXAMINATION

CS 425 / ECE 428 Distributed Systems Fall Indranil Gupta (Indy) Oct. 5, 2017 Lecture 12: Time and Ordering All slides IG

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

CSE 380 Computer Operating Systems

Multi-threading model

INTRODUCTION TO DIFFERENTIATION

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

Dartmouth Computer Science Technical Report TR Efficient Wait-Free Implementation of Multiword LL/SC Variables

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Formal Verification Techniques. Riccardo Sisto, Politecnico di Torino

EECS150 - Digital Design Lecture 23 - FFs revisited, FIFOs, ECCs, LSFRs. Cross-coupled NOR gates

Transcription:

INF4140 - Models of concurrency Fall 2017 October 17, 2017 Abstract This is the handout version of the slides for the lecture (i.e., it s a rendering of the content of the slides in a way that does not waste so much paper when printing out). The material is found in [Andrews, 2000]. Being a handout-version of the slides, some figures and graph overlays may not be rendered in full detail, I remove most of the overlays, especially the long ones, because they don t make sense much on a handout/paper. Scroll through the real slides instead, if one needs the overlays. This handout version also contains more remarks and footnotes, which would clutter the slides, and which typically contains remarks and elaborations, which may be given orally in the lecture. 1 Java concurrency 18. 10. 2017 1.1 Threads in Java Outline 1. Monitors: review 2. Threads in Java: Thread classes and Runnable interfaces Interference and Java threads Synchronized blocks and methods: (atomic regions and monitors) 3. Example: The ornamental garden 4. Thread communication & condition synchronization (wait and signal/notify) 5. Example: Mutual exclusion 6. Example: Readers/writers Short recap of monitors monitor encapsulates data, which can only be observed and modified by the monitor s procedures Contains variables that describe the state variables can be accessed/changed only through the available procedures Implicit mutex: Only one procedure may be active at a time. 2 procedures in the same monitor: never executed concurrently Condition synchronization: block a process until a particular condition holds, achieved through condition variables. Signaling disciplines Signal and wait (SW): the signaller waits, and the signalled process gets to execute immediately Signal and continue (SC): the signaller continues, and the signalled process executes later 1

Java From Wikipedia: 1 "... Java is a general-purpose, concurrent, class-based, object-oriented language..." Threads in Java A thread in Java unit of concurrency 2 originally green threads identity, accessible via static method Thread.CurrentThread() 3 has its own stack / execution context access to shared state shared mutable state: heap structured into objects privacy restrictions possible what are private fields? may be created (and deleted ) dynamically Thread class Thread run() MyThread run() The Thread class executes instructions from its method run(). The actual code executed depends on the implementation provided for run() in a derived class. c l a s s MyThread extends Thread { public void run ( ) { //...... // Creating a thread o b j e c t : Thread a = new MyThread ( ) ; a. start ( ) ; 1 But it s correct nonetheless... 2 as such, roughly corresponding to the concept of processes from previous lecctures. 3 What s the difference to this? 2

Runnable interface no multiple inheritance, often implement the run() method in a class not derived from Thread but from the interface Runnable. target Runnable Thread run() MyRun run() public interface Runnable { public abstract void run(); class MyRun implements Runnable { public void run() { //... // Creating a thread o b j e c t : Runnable b = new MyRun ( ) ; new Thread( b ). start ( ) ; Threads in Java steps to create a thread and get it running: 1. Define class that extends the Java Thread class or implements the Runnable interface 2. define run method inside the new class 4 3. create an instance of the new class. 4. start the thread. Interference and Java threads... c l a s s S t o r e { private int data = 0 ; public void update ( ) { data++;... // in a method : Store s = new Store ( ) ; // t h e t h r e a d s below have a c c e s s to s t1 = new FooThread ( s ) ; t1. start ( ) ; t2 = new FooThread ( s ) ; t2. start ( ) ; t1 and t2 execute s.update() concurrently! Interference between t1 and t2 may lose updates to data. Synchronization avoid interference threads synchronize access to shared data 1. One unique lock for each object o. 2. mutex: at most one thread t can lock o at any time. 5 3. two flavors of synchronization: synchronized block synchronized ( o ) { B 4 overriding, late-binding. 5 but: in a re-entrant manner! 3

synchronized method whole method body of m protected 6 : synchronized Type m (... ) {... Protecting the initialization Solution to earlier problem: lock the Store objects before executing problematic method: c l a s s S t o r e { p r i v a t e int data = 0 ; p u b l i c void update ( ) { synchronized ( t h i s ) { data++; or c l a s s S t o r e { p r i v a t e int data = 0 ; p u b l i c synchronized void update ( ) { data++;... // i n s i d e a method : Store s = new Store ( ) ; Java Examples Book: Concurrency: State Models & Java Programs, 2 nd Edition Jeff Magee & Jeff Kramer Wiley http://www.doc.ic.ac.uk/~jnm/book/ Examples in Java: http://www.doc.ic.ac.uk/~jnm/book/book_applets 6 assuming that other methods play according to the rules as well etc. 4

1.2 Ornamental garden Ornamental garden problem people enter an ornamental garden through either of 2 turnstiles. problem: the number of people present at any time. The concurrent program consists of: 2 threads shared counter object Ornamental garden problem: Class diagram The Turnstile thread simulates the periodic arrival of a visitor to the garden every second by sleeping for a second and then invoking the increment() method of the counter object. Counter c l a s s Counter { int value = 0 ; NumberCanvas d i s p l a y ; Counter ( NumberCanvas n ) { d i s p l a y = n ; d i s p l a y. s e t v a l u e ( value ) ; void increment ( ) { int temp = value ; // read [ v ] Simulate. HWinterrupt ( ) ; value = temp + 1 ; // w r i t e [ v+1] d i s p l a y. s e t v a l u e ( value ) ; Turnstile c l a s s Turnstile extends Thread { NumberCanvas d i s p l a y ; // i n t e r f a c e Counter people ; // shared data Turnstile ( NumberCanvas n, Counter c ) { // c o n s t r u c t o r d i s p l a y = n ; people = c ; public void run ( ) { 5

try { d i s p l a y. s e t v a l u e ( 0 ) ; for ( int i = 1 ; i <= Garden.MAX; i++) { Thread. s l e e p ( 5 0 0 ) ; // 0.5 second d i s p l a y. s e t v a l u e ( i ) ; people. increment ( ) ; // increment t h e counter catch ( I n t e r r u p t e d E x c e p t i o n e ) { Ornamental Garden Program The Counter object and Turnstile threads are created by the go() method of the Garden applet: private void go ( ) { counter = new Counter ( counterd ) ; west = new Turnstile ( westd, counter ) ; e a s t = new Turnstile ( eastd, c o u n t e r ) ; west. s t a r t ( ) ; e a s t. s t a r t ( ) ; Ornamental Garden Program: DEMO DEMO After the East and West turnstile threads have each incremented its counter 20 times, the garden people counter is not the sum of the counts displayed. Counter increments have been lost. Why? http://www.doc.ic.ac.uk/~jnm/book/book_applets/garden.html Avoid interference by synchronization c l a s s SynchronizedCounter extends Counter { SynchronizedCounter ( NumberCanvas n ) { super ( n ) ; synchronized void increment ( ) { super. increment ( ) ; 6

Mutual Exclusion: The Ornamental Garden - DEMO DEMO 1.3 Thread communication, monitors, and signaling Monitors each object has attached to it a unique lock and thus: can act as monitor 3 important monitor operations 7 o.wait(): release lock on o, enter o s wait queue and wait o.notify(): wake up one thread in o s wait queue o.notifyall(): wake up all threads in o s wait queue executable by a thread inside the monitor represented by o executing thread must hold the lock of o/ executed within synchronized portions of code typical use: this.wait() etc. note: notify does not operate on a thread-identity 8 Thread t = new MyThread ( ) ;... t. n o t i f y ( ) ; ; // mostly to be nonsense Condition synchronization, scheduling, and signaling quite simple/weak form of monitors in Java only one (implicit) condition variable per object: availability of the lock. threads that wait on o (o.wait()) are in this queue no built-in support for general-purpose condition variables. ordering of wait queue : implementation-dependent (usually FIFO) signaling discipline: S & C awakened thread: no advantage in competing for the lock to o. note: monitor-protection not enforced (!) private field modifier instance private not all methods need to be synchronized 9 besides that: there s re-entrance! 7 there are more 8 technically, a thread identity is represented by a thread object though. Note also : Thread.suspend() and Thread.resume() are deprecated. 9 remember: find of oblig-1. 7

A semaphore implementation in Java // down ( ) = P operation // up ( ) = V o p e r a t i o n public c l a s s Semaphore { private int value ; public Semaphore ( int i n i t i a l ) { v a l u e = i n i t i a l ; synchronized public void up ( ) { ++value ; notifyall ( ) ; synchronized public void down( ) throws InterruptedException { while ( v a l u e==0)wait ( ) ; // the well known while cond wait pattern v a l u e ; cf. also java.util.concurrency.semaphore (acquire/release + more methods) 1.4 Semaphores Mutual exclusion with sempahores The graphics shows waiting and active phases, plus value of mutex. Note: Light blue for active phase, other colours for waiting. Mutual exclusion with sempahores c l a s s MutexLoop implements Runnable { Semaphore mutex ; MutexLoop (Semaphore sema ) {mutex=sema ; public void run ( ) { try { while ( true ) { while (! ThreadPanel. r o t a t e ( ) ) ; // g e t mutual e x c l u s i o n mutex.down ( ) ; while ( ThreadPanel. r o t a t e ( ) ) ; // c r i t i c a l s e c t i o n // r e l e a s e mutual e x c l u s i o n mutex. up ( ) ; catch ( InterruptedException e ){ DEMO Panel is an (old) AWT class (applet is a subclass). It s the simplest container class. The function rotate returns a boolean. It s a static method of the thread subclass DisplayThhread. http://www.doc.ic.ac.uk/~jnm/book/book_applets/garden.html 8

1.5 Readers and writers Readers and writers problem (again... ) A shared database is accessed by two kinds of processes. Readers execute transactions that examine the database while Writers both examine and update the database. A Writer must have exclusive access to the database; any number of Readers may concurrently access it. Interface R/W interface ReadWrite { public void acquireread ( ) throws InterruptedException ; public void releaseread ( ) ; public void acquirewrite ( ) throws InterruptedException ; public void releasewrite ( ) ; Reader client code c l a s s Reader implements Runnable { ReadWrite monitor_ ; Reader ( ReadWrite monitor ) { monitor_ = monitor ; p u b l i c void run ( ) { t r y { while ( true ) { while (! ThreadPanel. r o t a t e ( ) ) ; // begin c r i t i c a l s e c t i o n monitor_. acquireread ( ) ; while ( ThreadPanel. r o t a t e ( ) ) ; monitor_. r e l e a s e R e a d ( ) ; catch ( I n t e r r u p t e d E x c e p t i o n e ){ Writer client code c l a s s Writer implements Runnable { ReadWrite monitor_ ; Writer ( ReadWrite monitor ) { monitor_ = monitor ; p u b l i c void run ( ) { t r y { while ( true ) { while (! ThreadPanel. r o t a t e ( ) ) ; // begin c r i t i c a l s e c t i o n monitor_. a c q u i r e W r i t e ( ) ; 9

while ( ThreadPanel. r o t a t e ( ) ) ; monitor_. r e l e a s e W r i t e ( ) ; catch ( I n t e r r u p t e d E x c e p t i o n e ){ R/W monitor (regulate readers) c l a s s ReadWriteSafe implements ReadWrite { p r i v a t e int r e a d e r s =0; p r i v a t e boolean w r i t i n g = f a l s e ; p u b l i c s y n c h r o n i z e d void acquireread ( ) throws I n t e r r u p t e d E x c e p t i o n { while ( w r i t i n g ) wait ( ) ; ++r e a d e r s ; p u b l i c s y n c h r o n i z e d void releaseread ( ) { r e a d e r s ; i f ( r e a d e r s ==0) notifyall ( ) ; p u b l i c s y n c h r o n i z e d void a c q u i r e W r i t e ( ) {... p u b l i c s y n c h r o n i z e d void r e l e a s e W r i t e ( ) {... R/W monitor (regulate writers) c l a s s ReadWriteSafe implements ReadWrite { private int r e a d e r s =0; private boolean w r i t i n g = f a l s e ; public synchronized void acquireread ( ) {... public synchronized void releaseread ( ) {... public synchronized void acquirewrite ( ) throws I n t e r r u p t e d E x c e p t i o n { while ( r e a d e r s >0 w r i t i n g ) wait ( ) ; w r i t i n g = true ; public synchronized void releasewrite ( ) { w r i t i n g = f a l s e ; notifyall ( ) ; DEMO Fairness http://www.doc.ic.ac.uk/~jnm/book/book_applets/readwritefair.html 10

Fairness : regulating readers c l a s s ReadWriteFair implements ReadWrite { private int r e a d e r s =0; private boolean w r i t i n g = f a l s e ; private int waitingw = 0 ; // no o f w a i t i n g Writers. private boolean readersturn = f a l s e ; synchronized public void acquireread ( ) throws I n t e r r u p t e d E x c e p t i o n { while ( w r i t i n g ( waitingw>0 &&! readersturn ) ) wait ( ) ; ++r e a d e r s ; synchronized public void releaseread ( ) { r e a d e r s ; readersturn=f a l s e ; i f ( r e a d e r s ==0) notifyall ( ) ; synchronized public void acquirewrite ( ) {... synchronized public void r e l e a s e W r i t e ( ) {... Fairness : regulating writers c l a s s ReadWriteFair implements ReadWrite { private int r e a d e r s =0; private boolean w r i t i n g = f a l s e ; private int waitingw = 0 ; // no o f w a i t i n g Writers. private boolean readersturn = f a l s e ; synchronized public void acquireread ( ) {... synchronized public void releaseread ( ) {... synchronized public void acquirewrite ( ) throws I n t e r r u p t e d E x c e p t i o n { ++waitingw ; while ( r e a d e r s >0 w r i t i n g ) wait ( ) ; waitingw ; w r i t i n g = true ; synchronized public void releasewrite ( ) { w r i t i n g = f a l s e ; readersturn=true ; notifyall ( ) ; Readers and Writers problem DEMO http://www.doc.ic.ac.uk/~jnm/book/book_applets/readerswriters.html Java concurrency there s (much) more to it than what we discussed (synchronization, monitors) (see java.util.concurrency) Java s memory model: since Java 1: loooong, hot debate connections to GUI-programming (swing/awt/events) and to RMI etc. major clean-up/repair since Java 5 better thread management Lock class (allowing new Lock() and non block-structured locking) one simplification here: Java has a (complex!) weak memory model (out-of-order execution, compiler optimization) not discussed here volatile 11

General advice shared, mutable state is more than a bit tricky, 10 watch out! work thread-local if possible make variables immutable if possible keep things local: encapsulate state learn from tried-and-tested concurrent design patterns golden rule never, ever allow (real, unprotected) races unfortunately: no silver bullet for instance: synchronize everything as much as possible : not just inefficient, but mostly nonsense concurrent programmig remains a bit of an art see for instance [?] or [?] References [Andrews, 2000] Andrews, G. R. (2000). Foundations of Multithreaded, Parallel, and Distributed Programming. Addison-Wesley. 10 and pointer aliasing and a weak memory model makes it worse. 12