2015 Concurrency: concurrent execution 1 ©Magee/Kramer 2 nd Edition Chapter 3 Concurrent Execution.

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

Chapter 3 Process Description and Control
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Concurrency: introduction1 ©Magee/Kramer 2 nd Edition Concurrency State Models and Java Programs Jeff Magee and Jeff Kramer.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition Chapter 5 Monitors & Condition Synchronization.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Process Description and Control
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency: concurrent execution ©Magee/Kramer Claus Brabrand University of Aarhus Concurrent Execution Concurrency.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fundamentals of Python: From First Programs Through Data Structures
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion.
(C) 2009 J. M. Garrido1 Object Oriented Simulation with Java.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads, Thread management & Resource Management.
Threads some important concepts Simon Lynch
Concurrency: processes & threads1 ©Magee/Kramer Chapter 2 Processes & Threads.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Definitions & Scenarios. Acknowledgements  This tutorial is based in part on Concurrency: State Models & Java Programming by Jeff Magee and Jeff Kramer.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
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 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Concurrency: concurrent execution1 ©Magee/Kramer Chapter 3 Concurrent Execution.
CSC321 Concurrent Programming: §7 Modelling Concurrency using FSP 1 Chapter 7 Modelling Concurrency using FSP.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer September 7th, 2010 The University of Georgia.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
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.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Module 2.0: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
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 in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrency: processes & threads1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.
Chapter 4 – Thread Concepts
Process Management Process Concept Why only the global variables?
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Multithreading Chapter 23.
Chapter 4: Threads.
Threads Chapter 4.
Threaded Programming in Python
Multithreading in java.
Chapter 4: Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

2015 Concurrency: concurrent execution 1 ©Magee/Kramer 2 nd Edition Chapter 3 Concurrent Execution

2015 Concurrency: concurrent execution 2 ©Magee/Kramer 2 nd Edition Concurrent execution Concepts : processes - concurrent execution and interleaving. process interaction. Models :parallel composition of asynchronous processes - interleaving interaction - shared actions process labelling, and action relabelling and hiding structure diagrams Practice : Multithreaded Java programs

2015 Concurrency: concurrent execution 3 ©Magee/Kramer 2 nd Edition Definitions  Concurrency Logically simultaneous processing. Does not imply multiple processing elements (PEs). Requires interleaved execution on a single PE.  Parallelism Physically simultaneous processing. Involves multiple PEs and/or independent device operations. Both concurrency and parallelism require controlled access to shared resources. We use the terms parallel and concurrent interchangeably and generally do not distinguish between real and pseudo-concurrent execution. A Time B C

2015 Concurrency: concurrent execution 4 ©Magee/Kramer 2 nd Edition 3.1 Modeling Concurrency  How should we model process execution speed? arbitrary speed (we abstract away time)  How do we model concurrency? arbitrary relative order of actions from different processes (interleaving but preservation of each process order )  What is the result? provides a general model independent of scheduling (asynchronous model of execution)

2015 Concurrency: concurrent execution 5 ©Magee/Kramer 2 nd Edition parallel composition - action interleaving think  talk  scratch think  scratch  talk scratch  think  talk Possible traces as a result of action interleaving. 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

2015 Concurrency: concurrent execution 6 ©Magee/Kramer 2 nd Edition parallel composition - action interleaving (0,0)(0,1)(0,2)(1,2) (1,1)(1,0) from CONVERSE from ITCH 2 states 3 states 2 x 3 states

2015 Concurrency: concurrent execution 7 ©Magee/Kramer 2 nd Edition 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). LTS? Traces? Number of states?

2015 Concurrency: concurrent execution 8 ©Magee/Kramer 2 nd Edition modelling interaction - shared actions MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). MAKER synchronizes with USER when ready. If processes in a composition have actions in common, these actions are said to be shared. Shared actions are the way that process interaction is modeled. While unshared actions may be arbitrarily interleaved, a shared action must be executed at the same time by all processes that participate in the shared action. LTS? Traces? Number of states? Non-disjoint alphabets

2015 Concurrency: concurrent execution 9 ©Magee/Kramer 2 nd Edition MAKERv2 = (make->ready->used->MAKERv2). USERv2 = (ready->use->used ->USERv2). ||MAKER_USERv2 = (MAKERv2 || USERv2). modelling interaction - handshake A handshake is an action acknowledged by another: Interaction constrains the overall behaviour. 3 states 3 x 3 states? 4 states

2015 Concurrency: concurrent execution 10 ©Magee/Kramer 2 nd Edition modelling interaction - multiple processes MAKE_A = (makeA->ready->used->MAKE_A). MAKE_B = (makeB->ready->used->MAKE_B). ASSEMBLE = (ready->assemble->used->ASSEMBLE). ||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE). Multi-party synchronization:

2015 Concurrency: concurrent execution 11 ©Magee/Kramer 2 nd Edition composite processes A composite process is a parallel composition of primitive processes. These composite processes can be used in the definition of further compositions. ||MAKERS = (MAKE_A || MAKE_B). ||FACTORY = (MAKERS || ASSEMBLE). Substituting the definition for MAKERS in FACTORY and applying the commutative and associative laws for parallel composition results in the original definition for FACTORY in terms of primitive processes. ||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE).

2015 Concurrency: concurrent execution 12 ©Magee/Kramer 2 nd Edition Example: a roller coaster model A roller coaster control system only permits its car to depart when it is full. Passengers arriving at the departure platform are registered by a turnstile. The controller signals the car to depart when there are enough passengers on the platform to fill the car to its maximum capacity of M passengers. The car then goes around the roller coaster track and then waits for another M passengers. A maximum of M passengers may occupy the platform. The roller coaster consists of three interacting processes TURNSTILE, CONTROL and CAR.

2015 Concurrency: concurrent execution 13 ©Magee/Kramer 2 nd Edition Example: an abstract roller coaster model const M = 3 //turnstile simulates passenger arrival TURNSTILE = ( … -> TURNSTILE). //control counts passengers and signals when full CONTROL = CONTROL[0], CONTROL[i:0..M]=( …-> CONTROL[i+1] | … -> CONTROL[0] ). //car departs when signalled CAR = ( … -> CAR). ||ROLLERCOASTER = ( … ).

2015 Concurrency: concurrent execution 14 ©Magee/Kramer 2 nd Edition process instances and labelling a:P creates an instance of process P and 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:

2015 Concurrency: concurrent execution 15 ©Magee/Kramer 2 nd Edition action relabelling Relabelling to ensure that composed processes synchronize on particular actions. Relabelling 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.

2015 Concurrency: concurrent execution 16 ©Magee/Kramer 2 nd Edition action relabelling ||CLIENT_SERVER = (CLIENT || SERVER) /{call/request, reply/wait}.

2015 Concurrency: concurrent execution 17 ©Magee/Kramer 2 nd Edition process labelling 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. Thus, every transition (n->X) in the definition of P is replaced with the transitions ({a1.n,…,ax.n} ->X). Process prefixing is useful for modelling shared resources: ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE). RESOURCE = (acquire->release->RESOURCE). USER = (acquire->use->release->USER).

2015 Concurrency: concurrent execution 18 ©Magee/Kramer 2 nd Edition process prefix labels for shared resources How does the model ensure that the user that acquires the resource is the one to release it? Can this be achieved using relabelling rather than sharing? How?

2015 Concurrency: concurrent execution 19 ©Magee/Kramer 2 nd Edition action relabelling - 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:

2015 Concurrency: concurrent execution 20 ©Magee/Kramer 2 nd Edition 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....

2015 Concurrency: concurrent execution 21 ©Magee/Kramer 2 nd Edition 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 behaviour.

2015 Concurrency: concurrent execution 22 ©Magee/Kramer 2 nd Edition 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}

2015 Concurrency: concurrent execution 23 ©Magee/Kramer 2 nd Edition 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

2015 Concurrency: concurrent execution 24 ©Magee/Kramer 2 nd Edition 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

2015 Concurrency: concurrent execution 25 ©Magee/Kramer 2 nd Edition 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).

2015 Concurrency: concurrent execution 26 ©Magee/Kramer 2 nd Edition 3.2 Multi-threaded Programs in Java Concurrency in Java occurs when more than one thread is alive. ThreadDemo has two threads which rotate displays.

2015 Concurrency: concurrent execution 27 ©Magee/Kramer 2 nd Edition 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}.

2015 Concurrency: concurrent execution 28 ©Magee/Kramer 2 nd Edition 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.

2015 Concurrency: concurrent execution 29 ©Magee/Kramer 2 nd Edition 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().

2015 Concurrency: concurrent execution 30 ©Magee/Kramer 2 nd Edition 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 and started by the start() method, and terminated by the stop() method.

2015 Concurrency: concurrent execution 31 ©Magee/Kramer 2 nd Edition ThreadDemo class public class ThreadDemo extends Applet { ThreadPanel A; ThreadPanel B; public void init() { A = new ThreadPanel("Thread A",Color.blue); B = new ThreadPanel("Thread B",Color.blue); add(A); add(B); } public void start() { A.start(new Rotator()); B.start(new Rotator()); } public void stop() { A.stop(); B.stop(); } ThreadDemo creates two ThreadPanel displays when initialized and two threads when started. ThreadPanel is used extensively in later demonstration programs.

2015 Concurrency: concurrent execution 32 ©Magee/Kramer 2 nd Edition 3.3 Java Concurrency Utilities Package Java SE 5 introduced a package of advanced concurrency utilities in java.util.concurrent (more later). This was extended in Java SE 7 to include additional constructs to separate thread creation and management from the rest of the application using executors, thread pools, and fork/join. Executor interface:replacement for thread creation, usually using existing thread: replace (new Thread(r)).start(); runnable object r with e.execute(r); Executor object e ExecutorService:manage termination; return a Future for tracking thread status Thread Pools: used to minimize the overhead of thread creation /termination ExecutorService newFixedThreadPool(int nThreads) - creates a fixed number with at most nThreads active threads - tasks are allocated from a shared unbounded queue Fork/Join:for recursive decomposition of tasks using thread pools

2015 Concurrency: concurrent execution 33 ©Magee/Kramer 2 nd Edition Summary  Concepts concurrent processes and process interaction  Models Asynchronous (arbitrary speed) & interleaving (arbitrary order). Parallel composition as a finite state process with action interleaving. Process interaction by shared actions. Process labeling and action relabeling and hiding. Structure diagrams  Practice Multiple threads in Java.