1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer September 7th, 2010 The University of Georgia.

Slides:



Advertisements
Similar presentations
Ch 7 B.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
CSC321 §6 Modelling Processes using FSP 1 Section 6 Modelling Processes using FSP.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
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.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Concurrency: concurrent execution ©Magee/Kramer Claus Brabrand University of Aarhus Concurrent Execution Concurrency.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading.
Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Internet Software Development More stuff on Threads Paul Krause.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Concurrency: processes & threads1 ©Magee/Kramer Chapter 2 Processes & Threads.
Definitions & Scenarios. Acknowledgements  This tutorial is based in part on Concurrency: State Models & Java Programming by Jeff Magee and Jeff Kramer.
CSC321 Concurrent Programming: §7 Modelling Concurrency using FSP 1 Section 7 Modelling Concurrency using FSP.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Concurrency: dynamic systems1 ©Magee/Kramer 2 nd Edition Chapter 9 Dynamic Systems.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
2015 Concurrency: processes & threads 1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.
CSC321 §6 Modelling Processes using FSP 1 Chapter 6 Modelling Processes using FSP.
1 Web Based Programming Section 8 James King 12 August 2003.
Concurrency: concurrent execution1 ©Magee/Kramer Chapter 3 Concurrent Execution.
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: §7 Modelling Concurrency using FSP 1 Chapter 7 Modelling Concurrency using FSP.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
2015 Concurrency: concurrent execution 1 ©Magee/Kramer 2 nd Edition Chapter 3 Concurrent Execution.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
CMSC 330: Organization of Programming Languages Threads.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading and Garbage Collection Session 16.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency: processes & threads1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.
Java Thread Programming
Shared Objects & Mutual Exclusion
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Multithreading.
Multithreaded Programming in Java
Chapter 19 Java Never Ends
Monitors Chapter 7.
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Monitors Chapter 7.
Concurrency: Mutual Exclusion and Process Synchronization
Monitors Chapter 7.
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer September 7th, 2010 The University of Georgia

2 Outline  Review: FSP and LTSA to represent concurrent processes (Ch.3)  Structure diagrams (Ch. 3)  UML and Java code for simple threaded programs (Ch. 3)  Modeling the Ornamental Garden (Ch. 4)  Testing a model (Ch.4)

3 Parallel Composition If P and Q are processes then (P||Q) represents the concurrent execution of P and Q. The operator || is the parallel composition operator. ITCH = (scratch->STOP). CONVERSE = (think->talk->STOP). ||CONVERSE_ITCH = (ITCH || CONVERSE). Disjoint alphabets

4 Parallel composition - algebraic laws Commutative: (P||Q) = (Q||P) Associative: (P||(Q||R))= ((P||Q)||R) = (P||Q||R). Clock radio example: CLOCK = (tick->CLOCK). RADIO = (on->off->RADIO). ||CLOCK_RADIO = (CLOCK || RADIO).

5 Modeling interaction - shared actions MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). MAKER synchronizes with USER when ready. Non-disjoint alphabets Actions with the same name are shared. They synchronize. Actions with different names are interleaved.

6 process instances and labeling a:P prefixes each action label in the alphabet of P with a. SWITCH = (on->off->SWITCH). ||TWO_SWITCH = (a:SWITCH || b:SWITCH). Two instances of a switch process: ||SWITCHES(N=3) = (forall[i:1..N] s[i]:SWITCH). ||SWITCHES(N=3) = (s[i:1..N]:SWITCH). An array of instances of the switch process:

7 process labeling by a set of prefix labels {a1,..,ax}::P replaces every action label n in the alphabet of P with the labels a1.n,…,ax.n. Further, every transition (n->X) in the definition of P is replaced with the transitions ({a1.n,…,ax.n} ->X). Process prefixing is useful for modeling shared resources: ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE). RESOURCE = (acquire->release->RESOURCE). USER = (acquire->use->release->USER).

8 process prefix labels for shared resources

9 action relabeling Relabeling to ensure that composed processes synchronize on particular actions. Relabeling functions are applied to processes to change the names of action labels. The general form of the relabeling function is: /{newlabel_1/oldlabel_1,… newlabel_n/oldlabel_n}. CLIENT = (call->wait->continue->CLIENT). SERVER = (request->service->reply->SERVER). Note that both newlabel and oldlabel can be sets of labels.

10 action relabeling ||CLIENT_SERVER = (CLIENT || SERVER) /{call/request, reply/wait}.

11 action relabeling - prefix labels SERVERv2 = (accept.request ->service->accept.reply->SERVERv2). CLIENTv2 = (call.request ->call.reply->continue->CLIENTv2). ||CLIENT_SERVERv2 = (CLIENTv2 || SERVERv2) /{call/accept}. An alternative formulation of the client server system is described below using qualified or prefixed labels:

12 action hiding - abstraction to reduce complexity When applied to a process P, the hiding operator \{a1..ax} removes the action names a1..ax from the alphabet of P and makes these concealed actions "silent". These silent actions are labeled tau. Silent actions in different processes are not shared. When applied to a process P, the interface hides all actions in the alphabet of P not labeled in the set a1..ax. Sometimes it is more convenient to specify the set of labels to be exposed....

13 action hiding USER = (acquire->use->release->USER) \{use}. USER = The following definitions are equivalent: Minimization removes hidden tau actions to produce an LTS with equivalent observable behavior.

14 structure diagrams – systems as interacting processes P a b Process P with alphabet {a,b}. P a b Q m Parallel Composition (P||Q) / {m/a,m/b,c/d} P Q a c d c x x x S y x Composite process ||S = {x,y}

15 structure diagrams We use structure diagrams to capture the structure of a model expressed by the static combinators: parallel composition, relabeling and hiding. range T = 0..3 BUFF = (in[i:T]->out[i]->BUFF). ||TWOBUF = ? a:BUFFb:BUFF a.out TWOBUFF out in out in out

16 Structure diagrams Structure diagram for CLIENT_SERVER Structure diagram for CLIENT_SERVERv2 CLIENTv2 call accept SERVERv2 call servicecontinue CLIENT call request SERVER call replywait reply servicecontinue

17 structure diagrams - resource sharing a:USER printer b:USER printer printer: RESOURCE acquire release PRINTER_SHARE RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use ->printer.release->USER)\{use}. ||PRINTER_SHARE = (a:USER||b:USER||{a,b}::printer:RESOURCE). Indicates sharing

Multi-threaded Programs in Java Concurrency in Java occurs when more than one thread is alive. ThreadDemo has two threads which rotate displays.

19 ThreadDemo model Interpret run, pause, interrupt as inputs, rotate as an output. ROTATOR = PAUSED, PAUSED = (run->RUN | pause->PAUSED |interrupt->STOP), RUN = (pause->PAUSED |{run,rotate}->RUN |interrupt->STOP). ||THREAD_DEMO = (a:ROTATOR || b:ROTATOR) /{stop/{a,b}.interrupt}.

20 ThreadDemo implementation in Java - class diagram ThreadDemo creates two ThreadPanel displays when initialized. ThreadPanel manages the display and control buttons, and delegates calls to rotate() to DisplayThread. Rotator implements the runnable interface.

21 Rotator class class Rotator implements Runnable { public void run() { try { while(true) ThreadPanel.rotate(); } catch(InterruptedException e) {} } Rotator implements the runnable interface, calling ThreadPanel.rotate() to move the display. run() finishes if an exception is raised by Thread.interrupt().

22 ThreadPanel class public class ThreadPanel extends Panel { // construct display with title and segment color c public ThreadPanel(String title, Color c) {…} // rotate display of currently running thread 6 degrees // return value not used in this example public static boolean rotate() throws InterruptedException {…} // create a new thread with target r and start it running public void start(Runnable r) { thread = new DisplayThread(canvas,r,…); thread.start(); } // stop the thread using Thread.interrupt() public void stop() {thread.interrupt();} } ThreadPanel manages the display and control buttons for a thread. Calls to rotate() are delegated to DisplayThread. Threads are created by the start() method, and terminated by the stop() method.

23 Chapter 4 Shared Objects & Mutual Exclusion

24 Shared Objects & Mutual Exclusion Concepts : process interference. mutual exclusion. Models :model checking for interference modeling mutual exclusion Practice :thread interference in shared Java objects mutual exclusion in Java (synchronized objects/methods).

Interference People enter an ornamental garden through either of two turnstiles. Management wish to know how many are in the garden at any time. The concurrent program consists of two concurrent threads and a shared counter object. Ornamental garden problem:

26 ornamental garden Program - 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.

27 ornamental garden program private void go() { counter = new Counter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); } The Counter object and Turnstile threads are created by the go() method of the Garden applet: Note that counterD, westD and eastD are objects of NumberCanvas used in chapter 2.

28 Turnstile class class Turnstile extends Thread { NumberCanvas display; Counter people; Turnstile(NumberCanvas n,Counter c) { display = n; people = c; } public void run() { try{ display.setvalue(0); for (int i=1;i<=Garden.MAX;i++){ Thread.sleep(500); //0.5 second between arrivals display.setvalue(i); people.increment(); } } catch (InterruptedException e) {} } The run() method exits and the thread terminates after Garden.MAX visitors have entered.

29 Counter class class Counter { int value=0; NumberCanvas display; Counter(NumberCanvas n) { display=n; display.setvalue(value); } void increment() { int temp = value; //read value Simulate.HWinterrupt(); value=temp+1; //write value display.setvalue(value); } Hardware interrupts can occur at arbitrary times. The counter simulates a hardware interrupt during an increment(), between reading and writing to the shared counter value. Interrupt randomly calls Thread.sleep() to force a thread switch.

30 ornamental garden program - display 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?

31 concurrent method activation Java method activations are not atomic - thread objects east and west may be executing the code for the increment method at the same time. east west increment: read value write value + 1 program counter program counter PC shared code

32 ornamental garden Model Process VAR models read and write access to the shared counter value. Increment is modeled inside TURNSTILE since Java method activations are not atomic i.e. thread objects east and west may interleave their read and write actions.

33 ornamental garden model const N = 4 range T = 0..N set VarAlpha = { value.{read[T],write[T]} } 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 )+VarAlpha. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || { east,west,display}::value:VAR) /{ go /{ east,west}.go, end/{ east,west}.end}. The alphabet of shared process VAR is declared explicitly as a set constant, VarAlpha. The TURNSTILE alphabet is extended with VarAlpha to ensure no unintended free (autonomous) actions in VAR eg. value.write[0]. All actions in the shared VAR must be controlled (shared) by a TURNSTILE.

34 checking for errors - animation Scenario checking - use animation to produce a trace. Is this trace correct?

35 checking for errors - exhaustive analysis TEST = TEST[0], TEST[v:T] = (when (v 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 ) )+{display.VarAlpha}. Exhaustive checking - compose the model with a TEST process which sums the arrivals and checks against the display value: Like STOP, ERROR is a predefined FSP local process (state), numbered -1 in the equivalent LTS.

36 ornamental garden model - checking for errors ||TESTGARDEN = (GARDEN || TEST). Use LTSA to perform an exhaustive search for ERROR. Trace to property violation in TEST: go 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 wrong LTSA produces the shortest path to reach ERROR.

37 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 to give methods mutually exclusive access to shared objects. Mutual exclusion can be modeled as atomic actions.

Mutual exclusion in Java class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) {super(n);} synchronized void increment() { super.increment(); } We correct COUNTER class by deriving a class from it and making the increment method synchronized: Concurrent activations of a method in Java can be made mutually exclusive by prefixing the method with the keyword synchronized, which uses a lock on the object. acquire lock release lock

39 mutual exclusion - the ornamental garden Java associates a lock with every object. The Java compiler inserts code to acquire the lock before executing the body of the synchronized method and code to release the lock before the method returns. Concurrent threads are blocked until the lock is released.

40 Java synchronized statement Access to an object may also be made mutually exclusive by using the synchronized statement: synchronized (object) { statements } A less elegant way to correct the example would be to modify the Turnstile.run() method: synchronized(people) {people.increment();} Why is this “less elegant”? To ensure mutually exclusive access to an object, all object methods should be synchronized.

41 To add locking to our model, define a LOCK, compose it with the shared VAR in the garden, and modify the alphabet set : 4.3 Modeling mutual exclusion LOCK = (acquire->release->LOCK). ||LOCKVAR = (LOCK || VAR). set VarAlpha = {value.{read[T],write[T], acquire, release}} TURNSTILE = (go -> RUN), RUN = (arrive-> INCREMENT |end -> TURNSTILE), INCREMENT = (value.acquire -> value.read[x:T]->value.write[x+1] -> value.release->RUN )+VarAlpha. Modify TURNSTILE to acquire and release the lock:

42 Revised ornamental garden model - checking for errors Use TEST and LTSA to perform an exhaustive check. Is TEST satisfied? go east.arrive east.value.acquire east.value.read.0 east.value.write.1 east.value.release west.arrive west.value.acquire west.value.read.1 west.value.write.2 west.value.release end display.value.read.2 right A sample animation execution trace

43 COUNTER: Abstraction using action hiding To model shared objects directly in terms of their synchronized methods, we can abstract the details by hiding. For SynchronizedCounter we hide read, write, acquire, release actions. const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = ( read[u]->VAR[u] | write[v:T]->VAR[v]). LOCK = (acquire->release->LOCK). INCREMENT = (acquire->read[x:T] -> (when (x<N) write[x+1] ->release->increment->INCREMENT ) )+{read[T],write[T]}. ||COUNTER =

44 COUNTER: Abstraction using action hiding Minimized LTS: We can give a more abstract, simpler description of a COUNTER which generates the same LTS: This therefore exhibits “equivalent” behavior i.e. has the same observable behavior. COUNTER = COUNTER[0] COUNTER[v:T] = (when (v COUNTER[v+1]).

45 Summary  Concepts process interference mutual exclusion  Models model checking for interference modeling mutual exclusion  Practice thread interference in shared Java objects mutual exclusion in Java (synchronized objects/methods).