Chapter 3 Concurrent Execution DM519 Concurrent Programming � 1
Repetition (Concepts, Models, and Practice) u Concepts: l We adopt a model-based approach for the design and construction of concurrent programs u Safe model => safe program u Models: l We use finite state models to represent concurrent behaviour ( Finite State Processes and Labelled Transition Systems ) � u Practice: l We use Java for constructing concurrent programs DM519 Concurrent Programming � 2
Repetition (Models; Lts, Fsp) Model = simplified representation of the real world u Based on Labelled Transition Systems ( LTS ): Focuses on concurrency aspects (of the program) - everything else abstracted away � � � u Described textually as Finite State Processes EngineOff = (engineOn -> EngineOn), ( FSP ): EngineOn = (engineOff -> EngineOff |speed -> EngineOn). DM519 Concurrent Programming � 3
Repetition (Finite State Processes; Fsp) 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(N=3) = … ♦ constant definitions const N = 3 ♦ range definitions range R = 0..N Which constructions do not add expressive power? (and are thus only "syntactic sugar"). DM519 Concurrent Programming � 4
Repetition (Java Threads) 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(); // ... DM519 Concurrent Programming � 5
Chapter 3: 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 DM519 Concurrent Programming � 6
Definition: Parallelism u Parallelism (aka. Real/True Concurrent Execution) � l Physically simultaneous processing u Involves multiple processing elements (PEs) and/or independent device operations A B C Time DM519 Concurrent Programming � 7
Definition: Concurrency u Concurrency (aka. Pseudo-Concurrent Execution) � l Logically simultaneous processing u Does not imply multiple processing elements (PEs) u Requires interleaved execution on a single PE A B C Time DM519 Concurrent Programming � 8
Parallelism vs Concurrency u Parallelism u 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). � Also, creating software independent of the physical setup, makes us capable of deploying it on any platform. DM519 Concurrent Programming � 9
3.1 Modelling Concurrency u How do we model concurrency? � x y � � � Possible execution sequences? � • x ; y � � • y ; x Asynchronous � • x || y � model of execution � l Arbitrary relative order of actions from different processes ( interleaving but preservation of each process order) DM519 Concurrent Programming � 10
3.1 Modelling Concurrency u How should we model process execution speed? � a x � � b y � l We choose to abstract away time: u Arbitrary speed! -: we can say nothing of real-time properties +: independent of architecture, processor speed, scheduling policies, … DM519 Concurrent Programming � 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 • think à scratch à talk a result of action • think à talk à scratch interleaving? DM519 Concurrent Programming � 12
Parallel Composition - Action Interleaving 2 states 3 states Cartesian product? (1,1) (1,0) (0,0) (0,1) (0,2) (1,2) from ITCH from CONVERSE 2 x 3 states DM519 Concurrent Programming � 13
Parallel Composition - Algebraic Laws Commutative: (P||Q) = (Q||P) Associative: (P||(Q||R)) = ((P||Q)||R) = (P||Q||R). Small example: MALTHE = (climbTree->fall->MALTHE). OSKAR = (run->jump->OSKAR). � ||MALTHE_OSKAR = (MALTHE || OSKAR). LTS? Traces? Number of states? DM519 Concurrent Programming � 14
Modelling Interaction - Shared Actions MAKE1 = (make->ready->STOP). MAKE1 USE1 = (ready->use->STOP). synchronises � with USE1 when ||MAKE1_USE1 = (MAKE1 || USE1). ready . LTS? Traces? Number of states? u Shared Actions: 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 modelled. 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. DM519 Concurrent Programming � 15
Modelling 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 DM519 Concurrent Programming � 16
Modelling Interaction - Example MAKE1 = (make-> ready ->STOP). 3 states USE1 = ( ready ->use->STOP). 3 states � ||MAKE1_USE1 = (MAKE1 || USE1). make ready ready ready ready ready 4 states! ready make Interaction may constrain use use use the overall behaviour ! make ready DM519 Concurrent Programming � 17
Example 2 states P = (x -> y -> P). Q = (y -> x -> Q). 2 states � ||R = (P || Q). LTS? Traces? Number of states? P = (a -> P | b -> P). Q = (c -> Q) + {a}. ||PQ = (P || Q). LTS? Traces? DM519 Concurrent Programming � 18
Modelling Interaction - Example MAKER = (make->ready->MAKER). USER = (ready->use->USER). � ||MAKER_USER = (MAKER || USER). LTS? Traces? Can we make sure the MAKER does not “get ahead of” the USER ( i.e. never make before use); and if so, how? DM519 Concurrent Programming � 19
Modelling Interaction - Handshake A handshake is an action acknowledged by another process: MAKERv2 = (make->ready->used->MAKERv2). USERv2 = (ready->use->used->USERv2). � ||MAKER_USERv2 = (MAKERv2 || USERv2). DM519 Concurrent Programming � 20
Modelling Interaction - Multiple Processes Multi-party synchronisation: 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). DM519 Concurrent Programming � 21
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). DM519 Concurrent Programming � 22
Process Labelling a:P prefixes each action label in the alphabet of P with a. Two instances of a switch process: LTS? (a:SWITCH) SWITCH = (on->off->SWITCH). ||TWO_SWITCH = (a:SWITCH || b:SWITCH). DM519 Concurrent Programming � 23
Process Labelling 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). Create 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). DM519 Concurrent Programming � 24
Process Labelling By A Set Of Prefix Labels {a 1 ,..,a n }::P replaces every action label x in the alphabet of P with the labels a 1 .x,…,a n .x. Further, every transition (x -> X) in the definition of P is replaced with the transitions ({a 1 .x,…,a n .x} -> X). Process prefixing is useful for modelling shared resources: USER = (acquire->use->release->USER). RESOURCE = (acquire->release->RESOURCE). ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE). DM519 Concurrent Programming � 25
Process Prefix Labels For Shared Resources RESOURCE = (acquire->release->RESOURCE). USER = (acquire->use->release->USER). ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE). How does the model ensure that the user that acquires the resource is the one to release it? DM519 Concurrent Programming � 26
Recommend
More recommend