Concurrency 3 – Concurrent Execution Alexandre David adavid@cs.aau.dk Credits for the slides: Claus Brabrand Jeff Magee & Jeff Kramer 1
Repetition ➢ Concepts: We adopt a model-based approach for the design and construction of concurrent programs. ➢ Models: We use finite state models to represent concurrent behaviour (Finite State Processes and Labelled Transition Systems). ➢ Practice: We use Java for constructing concurrent programs (and later C). 2
Repetition Model = simplified representation of the real world. ➢ Based on Labelled Transition Systems (LTS) Focuses on concurrency aspects (of the program) - everything else abstracted away engineOn speed 0 1 Aka. Finite State engineOff Machine (FSM ) ➢ Described textually as Finite State Processes (FSP) EngineOff = (engineOn-> EngineOn), EngineOn = (engineOff-> EngineOff |speed->EngineOn). 3
Repetition ➢ Finite State Processes (FSP): P : STOP // termination | (x -> P) // action prefix | (when (…) x -> P) // guard | P | P’ // choice | P + { … } // alphabet extension | X // process variable ♦ action indexing x[i:1..N]->P or x[i]->P ♦ process parameters P[i:1..N] = … ♦ constant definitions const N = 3 ♦ range definitions range R = 0..N 4
Repetition ➢ Subclassing java.lang.Thread: class MyThread extends Thread { public void run() { // ... } Thread t = new MyThread(); } t.start(); // ... ➢ Implementing java.lang.Runnable: class MyRun implements Runnable { public void run() { // ... } Thread t = new Thread(new MyRun()); } t.start(); // ... 5
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: multi-threaded Java programs 6
Definition: Parallelism ➔ Parallelism (aka. “Real” Concurrent Execution) ● Physically simultaneous processing ● Involves multiple processing elements (PEs) and/or independent device operations A B C Time 7
Definition: Concurrency ➔ Concurrency (aka. Pseudo-Concurrent Execution) ● Logically simultaneous processing ● Does not imply multiple processing elements (PEs) ● Requires interleaved execution on a single PE A B C Time 8
Parallelism vs. Concurrency Parallelism Concurrency A A B B C C Time Time 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). 9
Modeling Concurrency ➢ How do we model concurrency? x y Possible execution sequences? • x ; y Asynchronous • y ; x model of execution • x || y ➢ Arbitrary relative order of actions from different processes – interleaving but preservation of each process order. 10
Modeling Concurrency ➢ How should we model process execution speed? a x b y ➢ We abstract away time: arbitrary speed! -: we can say nothing of real-time properties +: independent of architecture, processor speed, scheduling policies, … 11
Parallel Composition – 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). • scratch -> think -> talk Possible traces as a result • think -> scratch -> talk of action interleaving? • think -> talk -> scratch 12
Parallel Composition – Action Interleaving Parallel composition = ITCH CONVERSE think talk scratch 1 0 0 1 2 2 states 3 states Cartesian product = scratch scratch think talk scratch talk think 3 4 5 0 1 2 (0,0) (0,1) (0,2) (1,2) (1,1) (1,0) 2 x 3 states from ITCH from CONVERSE 13
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? 14
Modeling Interaction – Shared Action MAKE1 = (make->ready->STOP). MAKE1 USE1 = (ready->use->STOP). synchronizes with USE1 when ||MAKE1_USE1 = (MAKE1 || USE1). ready . LTS? Traces? Number of states? Shared Action: 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. 15
Modeling Interaction - Example MAKE1 = (make-> ready ->STOP). 3 states USE1 = ( ready ->use->STOP). 3 states ||MAKE1_USE1 = (MAKE1 || USE1). make ready ready ready ready ready make 3 x 3 states? use use use make ready 16
Modeling Interaction - Example MAKE1 = (make-> ready ->STOP). 3 states USE1 = ( ready ->use->STOP). 3 states ||MAKE1_USE1 = (MAKE1 || USE1). make ready ready ready ready ready make 3 x 3 states? use use use No…! make ready 17
Modeling Interaction - Example MAKE1 = (make-> ready ->STOP). 3 states USE1 = ( ready ->use->STOP). 3 states ||MAKE1_USE1 = (MAKE1 || USE1). make ready r e 4 states! a d ready ready ready y Interaction constrains ready make the overall behaviour. use use use make ready 18
Modeling Interaction - Example MAKER = ( make ->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make 0 1 19
Modeling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make ready 0 1 2 20
Modeling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make ready make 0 1 2 3 use 21
Modeling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make ready make 0 1 2 3 use use 22
Modeling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make ready r e a d ready ready ready y ready make make make use use use make ready 23
Modeling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make y d a ready ready e r make use use 24
Modeling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). make 0 1 y d a e use use r make 2 3 25
Modeling Interaction - Handshake A handshake is an action acknowledged by another: MAKERv2 = (make->ready->used->MAKERv2). USERv2 = (ready->use->used->USERv2). ||MAKER_USERv2 = (MAKERv2 || USERv2). make ready use 0 1 2 3 used 26
Modeling Interaction – Multiple Processes Multi-party synchronization: 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). makeA makeB 5 makeB makeA ready assemble 0 1 2 3 4 used 27
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). substitution of def’n of MAKERS ||FACTORY = ((MAKE_A || MAKE_B)|| ASSEMBLE). 28
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). substitution of def’n of MAKERS ||FACTORY = ((MAKE_A || MAKE_B)|| ASSEMBLE). associativity! Further simplification? ||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE). 29
Process Labeling a:P prefixes each action label in the alphabet of P with a. Two instances of a switch process: SWITCH = (on->off->SWITCH). ||TWO_SWITCH = (a:SWITCH || b:SWITCH). LTS? (a:SWITCH) 30
Process Labeling a:P prefixes each action label in the alphabet of P with a. Two instances of a switch process: SWITCH = (on->off->SWITCH). ||TWO_SWITCH = (a:SWITCH || b:SWITCH). a.on a.on a:SWITCH a b 0 1 b.off b.on a.on 0: off off a.off 1: off on 0 1 2 3 2: on on b.on b.off b.on a.off 3: on off b:SWITCH 0 1 a.off b.off 31
Process Labeling a:P prefixes each action label in the alphabet of P with a. Two instances of a switch process: SWITCH = (on->off->SWITCH). ||TWO_SWITCH = (a:SWITCH || b:SWITCH). An array of instances of the switch process: ||SWITCHES(N=3) = (forall[i:1..N] s[i]:SWITCH). ||SWITCHES(N=3) = (s[i:1..N]:SWITCH). 32
Recommend
More recommend