Shared Objects & Mutual Exclusion

Similar documents
Shared Objects & Mutual Exclusion

CSCI 5828: Foundations of Software Engineering

Q1. From post-it note. Question asked after Prev. Lecture. thread life-cycle in Java. Q2. Concurrency and parallelism. Definitions

Introduction to modelling in FSP (Finite State Processes) and its application for analysis of Java Mutex problems

Shared Objects & Mutual Exclusion

Lecture 9: Introduction to Monitors

Shared Mutable State SWEN-220

CSCI 5828: Foundations of Software Engineering

Concurrent Execution

CSCI 5828: Foundations of Software Engineering

7: Interprocess Communication

Concurrent processes. Processes and Threads. Processes and threads. Going back to Concurrency. Modelling Processes. Modeling processes

Processes & Threads. Concepts: processes - units of sequential execution.

Chapter 2. Processes & Threads. Concurrency: processes & threads 1. Magee/Kramer

CSCI 5828: Foundations of Software Engineering

Verifying a Compiler for Java Threads

G52CON: Concepts of Concurrency

Communications Software Engineering Condition Synchronization and Liveness

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

G52CON: Concepts of Concurrency

Monitors & Condition Synchronization

Concurrent processes. Processes and Threads. A question asked by your friend. Warm-up exercises at beginning of Lecture 3. My question for you

LTSA User Manual.

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

Model Requirements and JAVA Programs MVP 2 1

Monitors & Condition Synchronisation

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

Monitors & Condition Synchronization

CSCI 5828: Foundations of Software Engineering

Monitors & Condition Synchronization

Chapter 6. Deadlock. DM519 Concurrent Programming

Lecture 17: Sharing Objects in Java

Threads Synchronization

Lecture 10: Introduction to Semaphores

Space Exploration EECS /25

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

FSP Language Specification

Faculty of Computers & Information Computer Science Department

Threads Chate Patanothai

C340 Concurrency: Semaphores and Monitors. Goals

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Concurrency: Mutual Exclusion and Synchronization. Concurrency

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

COMP30112: Concurrency Topics 5.2: Properties

Problems with Concurrency. February 19, 2014

Programming Language Concepts: Lecture 11

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

CS 351 Design of Large Programs Threads and Concurrency

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Synchronization Spinlocks - Semaphores

Concurrency in Java Prof. Stephen A. Edwards

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Java s Implementation of Concurrency, and how to use it in our applications.

Static Program Analysis

Atropos User s manual

Lecture 32: Volatile variables, Java memory model

Synchronization SPL/2010 SPL/20 1

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

THE QUEEN S UNIVERSITY OF BELFAST

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

$ Program Verification

CMSC 132: Object-Oriented Programming II. Threads in Java

F. Tip and M. Weintraub CONCURRENCY

Let s make some Marc R. Hoffmann Eclipse Summit Europe

IV. Process Synchronisation

What is a thread anyway?

Monitors; Software Transactional Memory

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock. INF2140 Parallel Programming: Lecture 6. March 07, INF2140 Parallel Programming: Lecture 6 Deadlock

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Contents. 6-1 Copyright (c) N. Afshartous

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

CS 159: Parallel Processing

Concurrent Programming using Threads

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

Parallel Programming Languages COMP360

Introduction to OS Synchronization MOS 2.3

Formal Specification and Verification

Concurrent Programming. Introduction

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

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

Systems Programming & Scripting

CS370 Operating Systems

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

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

Deadlock. Concepts: Models: Practice: Aim: deadlock avoidance - to design systems where deadlock cannot occur. Chapter 6. Deadlock

Lecture 2: Modeling Introduction

Process Management And Synchronization

Lecture 9 Threading, Synchronization, Interruption. Manu Kumar

Java Threads Vs Processes. CS Concurrent Programming. Java Threads. What can a thread do? Java Concurrency

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

4/6/2011. Model Checking. Encoding test specifications. Model Checking. Encoding test specifications. Model Checking CS 4271

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Process Synchronization

Generating Code from Event-B Using an Intermediate Specification Notation

Software Engineering using Formal Methods

Transcription:

Chapter 4 Shared Objects & Mutual Exclusion 1

Repetition (Finite State Processes; Fsp) Finite State Processes (FSP) can be defined using: P = x -> Q // action Q // other process variable STOP // termination Q R // choice when (...) x -> Q // guard... + {write[0..3] // alphabet extension X[i:0..N] =x[n-i] -> P // process & action index BUFF(N=3) // process parameter const N = 3 range R = 0..N set S = {a,b,c // constant definitions // range definitions // set definitions range T = 0..3 BUFF = (in[i:t]->out[i]->buff). 2

Repetition (Fsp) FSP: P Q // parallel composition a:p // process labelling (1 process/prefix) { ::P // process sharing (1 process w/all prefixes) P / {x/y // action relabelling P \ { // hiding P @ { // keeping (hide complement) TWOBUF = (a:buff b:buff) /{in/a.in, a.out/b.in, out/b.out @{in,out. Structure Diagrams: in a:buff in out TWOBUFF a.out b:buff in out out 3

Structure Diagrams - Resource Sharing RESOURCE = (acquire->release->resource). USER = (printer.acquire->use->printer.release->user). PRINTER_SHARE = (a:user b:user {a,b::printer:resource). a:user printer b:user printer PRINTER_SHARE printer: RESOURCE acquire release 4

How To Create The Parallel Composed Lts MAKE1 = (make->ready->stop). USE1 = (ready->use->stop). MAKE1_USE1 = (MAKE1 USE1). For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. make ready 0,0 1,0 2,0 ready ready ready make ready 0,1 1,1 2,1 use use use 0,2 make 1,2 ready 2,2 5

How To Create The Parallel Composed Lts WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). DAY = (WORKDAY ALSORUN). For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. 0,0 1,1 1,0 0,1 6

Chapter 4: Shared Objects & Mutual Exclusion u Concepts: l Process interference l Mutual exclusion u Models: l Model-checking for interference l Modelling mutual exclusion u Practice: l Thread interference in shared objects in Java l Mutual exclusion in Java l Synchronised objects, methods, and statements 7

4.1 Interference The Ornamental Garden Problem : People enter an ornamental garden through either of two turnstiles. Management wishes to know how many are in the garden at any time. (Nobody can exit). 012 Counter 8

4.1 Ornamental Garden Problem (Cont d) Java implementation: Counter The concurrent program consists of: two concurrent threads (west & east); and a shared counter object 2 9

Class Diagram 20 40 20 counter 10

Ornamental Garden Program The go() method of the Garden applet class Garden extends Applet { NumberCanvas counterd, westd, eastd; Turnstile east, west;... private void go() { counter = new Counter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); creates the shared Counter object & the Turnstile threads. 11

The Turnstile Class class Turnstile extends Thread { NumberCanvas display; Counter counter; The Turnstile thread simulates periodic arrival of visitors by invoking the counter object s increment() method every second public void run() { try { display.setvalue(0); for (int i=1; i<=garden.max; i++) { Thread.sleep(1000); display.setvalue(i); counter.increment(); catch (InterruptedException _) { 12

The Shared Counter Class The increment() method of the Counter class increments its internal value and updates the display. class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); 13

Running The Applet 20 39 20 After the East and West turnstile threads each have incremented the counter 20 times, the garden people counter is not always the sum of the counts displayed. Why? 14

The Shared Counter Class (Cont d) class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); javac Counter.java javap -c Counter > Counter.bc Thread switch? aload_0 getfield #2 iconst_1 iadd putfield #2 // push this onto stack // get value of this.value // push 1 onto stack // add two top stack elements // put result into this.value 15

Concurrent Method Activation Java method activation is not atomic Thus, threads east and west may be executing the code for the increment method at the same time. west PC program counter Shared code: Counter.class: aload_0 // this getfield #2 // x iconst_1 iadd putfield #2 // x east PC program counter 16

Pedagogification; The Counter Class (Cont d) class Counter { void increment() { value = value + 1; display.setvalue(value); 17

Pedagogification; The Counter Class (Cont d) class Counter { void increment() { int temp = value; // read Simulate.HWinterrupt(); value = temp + 1; // write display.setvalue(value); The counter simulates a hardware interrupt during an increment(), between reading and writing to the shared counter value. class Simulate { // randomly force thread switch public static void HWinterrupt() { if (random()<0.5) Thread.yield(); 18

Running The Applet Now the erroneous behaviour occurs almost all the time 19

Garden Model (Structure Diagram) GARDEN VAR: models read and write access to the shared counter value. TURNSTILE: Increment is modelled inside TURNSTILE, since Java method activation is not atomic (i.e., thread objects east and west may interleave their read and write actions). 20

Ornamental Garden Model (Fsp) const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] write[v:t] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT end -> TURNSTILE), INCREMENT = (value.read[x:t] -> value.write[x+1] -> RUN) +{value.write[0]. DISPLAY =(value.read[t]->display)+{value.write[t]. GARDEN = (east:turnstile west:turnstile display:display {east,west,display::value:var) /{ go / {east,west.go, end / {east,west.end. α(var)? α(value:var)? α({east,west,display::value:var)? α(turnstile)? α(east:turnstile)? α(display:display)? 21

Checking For Errors - Animation Scenario checking - use animation to produce a trace. Is the model correct? Never send a human to do a machine s job - Agent Smith (1999) 22

Checking For Errors - Compose With Error Detector Exhaustive checking - compose the model with a TEST process which sums the arrivals and checks against the display value: TEST = TEST[0], TEST[v:T] = (when (v<n) west.arrive->test[v+1] when (v<n) east.arrive->test[v+1] end -> CHECK[v]), CHECK[v:T] = (display.value.read[u:t] -> (when (u==v) right -> TEST[v] when (u=v) wrong -> ERROR)). 23

Checking For Errors - Exhaustive Analysis TESTGARDEN = (GARDEN TEST). Use LTSA to perform an exhaustive search for ERROR: Trace to property violation in TEST: go wrong east.arrive east.value.read.0 west.arrive west.value.read.0 east.value.write.1 west.value.write.1 end display.value.read.1 LTSA produces the shortest path to reach the ERROR state. 24

Interference And Mutual Exclusion Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference. Interference bugs are extremely difficult to locate. The general solution is: Give methods mutually exclusive access to shared objects. Mutual exclusion can be modelled as atomic actions. 25

4.2 Mutual Exclusion In Java Concurrent activations of a method in Java can be made mutually exclusive by prefixing the method with the keyword synchronized. We correct the Counter class by deriving a class from it and making its increment method synchronized: class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) { super(n); synchronized void increment() { super.increment(); 26

The Garden Class (Revisited) If the fixit checkbox is ticked, the go() method creates a SynchronizedCounter: class Garden extends Applet { private void go() { if (fixit.getstate()) counter = new Counter(counterD); else counter = new SynchCounter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); 27

Mutual Exclusion - The Ornamental Garden Java associates a lock with every object. The Java compiler inserts code to: acquire the lock before executing a synchronized method release the lock after the synchronized method returns. Concurrent threads are blocked until the lock is released. 28

Java Synchronized Statement Synchronized methods: synchronized void increment() { super.increment(); synchronized void decrement() { super.decrement(); Variant - the synchronized statement : class Turnstile{... public void run() {... synchronized(counter) { counter.increment();... object reference Use synch methods whenever possible. 29

Java -> Java Bytecode 1 class X { 2 int x; 3 void m() { 4 synchronized(this) { 5 x++; 6 7 8 compile Method void m() >> max_stack=3, max_locals=3 << 0 aload_0 1 dup 2 astore_1 3 monitorenter 4 aload_0 5 dup 6 getfield #2 <Field X.x:int> 9 iconst_1 10 iadd 11 putfield #2 <Field X.x:int> 14 aload_1 15 monitorexit 16 goto 24 19 astore_2 20 aload_1 21 monitorexit 22 aload_2 23 athrow 24 return Exception table: from to target type 4 16 19 any 19 22 19 any 30

4.3 Modelling Mutual Exclusion GARDEN = (east:turnstile west:turnstile {east,west,display::value:lockvar) Define a mutual exclusion LOCK process: LOCK = (acq -> rel -> LOCK). and compose it with the shared VAR in the Garden: LOCKVAR = (LOCK VAR). Modify TURNSTILE to acquire and release the lock: TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT end -> TURNSTILE), INCREMENT = (value.acq -> value.read[x:t] -> value.write[x+1] -> value.rel->run )+{value.write[0]. 31

Revised Ornamental Garden Model - Checking For Errors A sample trace: go east.arrive east.value.acq east.value.read.0 east.value.write.1 east.value.rel west.arrive west.value.acq west.value.read.1 west.value.write.2 west.value.rel end display.value.read.2 right Use LTSA to perform an exhaustive check: is TEST satisfied? 32

Counter: Abstraction Using Action Hiding We can abstract the details by hiding. For SynchronizedCounter we hide const N = 4 range T = 0..N read, write, acquire, release actions. VAR = VAR[0], VAR[u:T] = ( read[u]->var[u] write[v:t]->var[v]). LOCK = (acquire->release->lock). INCREMENT = (acquire->read[x:t] -> write[x+1] -> release->increment->increment) +{read[t],write[t]. COUNTER = (INCREMENT LOCK VAR)@{increment. 33

Counter: Abstraction Using Action Hiding Minimised LTS: We can give a more abstract, simpler description of a COUNTER which generates the same LTS: COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<n) increment -> COUNTER[v+1]). This therefore exhibits equivalent behaviour, i.e., has the same observable behaviour. 34

Active & Passive Processes Comparing FSP and Java active processes : threads, e.g., TURNSTILE passive processes: shared objects, e.g., COUNTER const N = 4 range T = 0..N set VarAlpha = {value.{read[t],write[t],acquire,release VAR = VAR[0], VAR[u:T] = (read[u]->var[u] write[v:t]->var[v]). LOCK = (acquire->release->lock). LOCKVAR = (LOCK VAR). TURNSTILE = (go -> RUN), RUN = (arrive-> INCREMENT end -> TURNSTILE), INCREMENT = (value.acquire -> value.read[x:t]->value.write[x+1] ->value.release->run)+varalpha. DISPLAY =(value.read[t]->display)+{value.{write[t],acquire,release. GARDEN = (east:turnstile west:turnstile display:display {east,west,display::value:lockvar) /{go /{east,west.go, end/{east,west.end. 35

Java Memory Model public class NoVisibility { private static boolean ready; private static int number; private static class ReaderThread extends Thread { public void run() { while (ready) { yield(); System.out.println(number); public static void main(string[] args) { new ReaderThread().start(); number = 42; ready = true; 36

Synchronisation In Java Is Not Just Mutual Exclusion; It s Also About Memory Visibility Thread A y=1 lock M x=1 unlock M Everything before the unlock on M Must be the same lock is visible to everything after the lock on M Thread B lock M i=x unlock M Without synchronisation, there is no such guarantee. j=y 37

Summary u Concepts l process interference l mutual exclusion u Models l model checking for interference l modelling mutual exclusion u Practice l thread interference in shared Java objects l mutual exclusion in Java (synchronized objects/methods). 38