Concurrency 3 Concurrent Execution Alexandre David - - PowerPoint PPT Presentation

concurrency 3 concurrent execution
SMART_READER_LITE
LIVE PREVIEW

Concurrency 3 Concurrent Execution Alexandre David - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

1

Concurrency 3 – Concurrent Execution

Alexandre David

adavid@cs.aau.dk Credits for the slides: Claus Brabrand Jeff Magee & Jeff Kramer

slide-2
SLIDE 2

2

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).

slide-3
SLIDE 3

3

Repetition

Model = simplified representation of the real world.

➢ Based on Labelled Transition Systems (LTS) ➢ Described textually as Finite State Processes

(FSP) Focuses on concurrency aspects (of the program)

  • everything else abstracted away

EngineOff = (engineOn-> EngineOn), EngineOn = (engineOff-> EngineOff |speed->EngineOn).

1

speed engineOn engineOff

  • Aka. Finite State

Machine (FSM )

slide-4
SLIDE 4

4

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

slide-5
SLIDE 5

5

Repetition

➢ Subclassing java.lang.Thread: ➢ Implementing java.lang.Runnable:

class MyThread extends Thread { public void run() { // ... } } class MyRun implements Runnable { public void run() { // ... } } Thread t = new MyThread(); t.start(); // ... Thread t = new Thread(new MyRun()); t.start(); // ...

slide-6
SLIDE 6

6

Concurrent Execution

Concepts: processes - concurrent execution and interleaving. process interaction. Models: parallel composition

  • f asynchronous processes - interleaving

interaction

shared actions, process labelling, and action relabelling and hiding

structure diagrams

Practice: multi-threaded Java programs

slide-7
SLIDE 7

7

Definition: Parallelism

➔ Parallelism (aka. “Real” Concurrent Execution)

  • Physically simultaneous processing
  • Involves multiple processing elements (PEs)

and/or independent device operations

A

Time

B C

slide-8
SLIDE 8

8

Definition: Concurrency

A

Time

B C

➔ Concurrency (aka. Pseudo-Concurrent Execution)

  • Logically simultaneous processing
  • Does not imply multiple processing elements (PEs)
  • Requires interleaved execution on a single PE
slide-9
SLIDE 9

9

Parallelism vs. Concurrency

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). Parallelism Concurrency

A

Time

B C A

Time

B C

slide-10
SLIDE 10

10

Modeling Concurrency

➢ How do we model concurrency? ➢ Arbitrary relative order of actions from

different processes – interleaving but preservation of each process order.

x

Possible execution sequences?

y

Asynchronous model of execution

  • x ; y
  • y ; x
  • x || y
slide-11
SLIDE 11

11

Modeling Concurrency

➢ How should we model process execution speed? ➢ We abstract away time: arbitrary speed! a b x y

+: independent of architecture, processor speed, scheduling policies, …

  • : we can say nothing of real-time properties
slide-12
SLIDE 12

12

Parallel Composition – Action Interleaving

  • scratch->think->talk
  • think->scratch->talk
  • think->talk->scratch

Possible traces as a result

  • f 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

  • perator.

ITCH = (scratch->STOP). CONVERSE = (think->talk->STOP). ||CONVERSE_ITCH = (ITCH || CONVERSE).

slide-13
SLIDE 13

13

Parallel Composition – Action Interleaving

2 states 3 states (0,0) (0,1) (0,2) (1,2) (1,1) (1,0)

from CONVERSE from ITCH

2 x 3 states

Cartesian product =

1 2 3 4 5

think talk think talk scratch scratch scratch

1

scratch

1 2

think talk

ITCH CONVERSE

Parallel composition =

slide-14
SLIDE 14

14

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?

slide-15
SLIDE 15

15

Modeling Interaction – 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.

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1). MAKE1

synchronizes with

USE1 when ready. LTS? Traces? Number of states?

Shared Action:

slide-16
SLIDE 16

16

Modeling Interaction - Example

3 states 3 states 3 x 3 states?

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1).

make ready make make ready ready ready ready ready use use use

slide-17
SLIDE 17

17

No…!

Modeling Interaction - Example

3 states 3 states 3 x 3 states?

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1).

make ready make make ready ready ready ready ready use use use

slide-18
SLIDE 18

18

3 states 3 states

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1). Interaction constrains the overall behaviour.

4 states!

make ready make make ready ready ready ready ready use use use r e a d y

Modeling Interaction - Example

slide-19
SLIDE 19

19

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). 1

make

slide-20
SLIDE 20

20

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). 2 1

make ready

slide-21
SLIDE 21

21

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). 3 2 1

make ready make use

slide-22
SLIDE 22

22

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER). 3 2 1

make ready make use use

slide-23
SLIDE 23

23

make ready make make ready ready ready ready ready use use use r e a d y

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER).

make make

slide-24
SLIDE 24

24

make make ready ready use

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER).

r e a d y use

slide-25
SLIDE 25

25

make

1 2 3

make use

Modeling Interaction - Example

MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||MAKER_USER = (MAKER || USER).

r e a d y use

slide-26
SLIDE 26

26

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). 3 2 1

make ready use used

slide-27
SLIDE 27

27

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). 3 2 1

makeB ready

5 4

makeA assemble makeA used makeB

slide-28
SLIDE 28

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). ||FACTORY = ((MAKE_A || MAKE_B)|| ASSEMBLE).

substitution of def’n of MAKERS

slide-29
SLIDE 29

29

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). ||FACTORY = ((MAKE_A || MAKE_B)|| ASSEMBLE).

substitution of def’n of MAKERS associativity!

||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE).

Further simplification?

slide-30
SLIDE 30

30

Process 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:

LTS? (a:SWITCH)

slide-31
SLIDE 31

31

Process 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:

1

a.on a.off a:SWITCH

1

b.on b.off b:SWITCH

1

b.on b.off

2 3

a.on a.off b.off b.on a.off a.on

a b 0: off off 1: off on 2: on on 3: on off

slide-32
SLIDE 32

32

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:

Process Labeling

slide-33
SLIDE 33

33

{a1,..,an}::P replaces every action label x in the alphabet of P with the labels a1.x,…,an.x. Further, every transition (x->X) in the definition of P is replaced with the transitions ({a1.x,…,an.x} ->X).

RESOURCE = (acquire->release->RESOURCE). USER = (acquire->use->release->USER). ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE).

Process Labeling by a Set of Prefix Labels

Process prefixing is useful for modeling shared resources:

slide-34
SLIDE 34

34

Process Prefix Labels for Shared Resources

How does the model ensure that the user who acquires the resource is the one to release it?

RESOURCE = (acquire->release->RESOURCE). USER = (acquire->use->release->USER). ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE). 2 1

a:USER a.acquire a.use

a.release

2 1

b:USER

b.acquire b.use b.release

1

b.acquire a.acquire b.release a.release

{a,b}::RESOURCE

2 1

b.acquire b.use b.release

4 3

a.use a.acquire a.release RESOURCE_SHARE

slide-35
SLIDE 35

35

Relabeling to ensure that composed processes synchronize

  • n particular actions:

Relabeling functions are applied to processes to change the names of action

  • labels. The general form of the relabeling function is:

/{newlabel1/oldlabel1,… newlabeln/oldlabeln}.

CLIENT = (call->wait->continue->CLIENT). SERVER = (request->service->reply->SERVER).

Action Relabeling

slide-36
SLIDE 36

36

Action Relabeling

||C_S = (C || S). CLIENT = (call->wait->continue->CLIENT). SERVER = (request->service->reply->SERVER).

C S C_S

C = (CLIENT /{reply/wait}). S = (SERVER /{call/request}).

2 1

call wait reply continue

2 1

request call service reply

2 1

call service reply

3

continue

slide-37
SLIDE 37

37

Action Relabeling – Prefix Labels

CLIENTv2 = (call.request

  • >call.reply->continue->CLIENTv2).

SERVERv2 = (accept.request

  • >service->accept.reply->SERVERv2).

||CLIENT_SERVERv2 = (CLIENTv2 || SERVERv2) /{call/accept}.

An alternative formulation of the client server system is described below using qualified or prefixed labels:

slide-38
SLIDE 38

38

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.

USER = (acquire->use->release->USER) \{use}.

2 1

acquire tau release

USER

slide-39
SLIDE 39

39

Action Hiding – Abstraction to Reduce Complexity

When applied to a process P, the interface operator @ {a1..ax} 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....

USER = (acquire->use->release->USER) @{acquire,release}.

2 1

acquire tau release

USER

slide-40
SLIDE 40

40

Action Hiding

USER = (acquire->use->release->USER) \{use}. USER = (acquire->use->release->USER) @{acquire,release}.

The following definitions are equivalent:

Minimization removes hidden tau actions to produce an LTS with equivalent observable behaviour.

2 1

acquire tau release

USER

slide-41
SLIDE 41

41

Action Hiding

USER = (acquire->use->release->USER) \{use}. USER = (acquire->use->release->USER) @{acquire,release}.

The following definitions are equivalent:

Minimization removes hidden tau actions to produce an LTS with equivalent observable behaviour.

2 1

acquire tau release

USER

1

acquire release

USER

slide-42
SLIDE 42

42

T

a b

Process T with alphabet {a,b}.

Structure Diagrams

slide-43
SLIDE 43

43

T

a b

Process T with alphabet {a,b}.

P

a b

Q

m

Parallel Composition (P||Q) / {m/a,m/b,c/d}

c d c x x x

Structure Diagrams

slide-44
SLIDE 44

44

T

a b

Process T with alphabet {a,b}.

P

a b

Q

m

Parallel Composition (P||Q) / {m/a,m/b,c/d}

A B

a c d c x x x

S

y x

Composite process ||S = (A||B) @ {x,y}

Structure Diagrams

slide-45
SLIDE 45

45

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).

a:BUFF b:BUFF

a.out

TWOBUFF

  • ut

in in

  • ut

in

  • ut

||TWOBUF = (a:BUFF || b:BUFF) /{in/a.in, a.out/b.in, out/b.out} @{in,out}.

slide-46
SLIDE 46

46

Structure diagram for CLIENT_SERVER ? Structure diagram for CLIENT_SERVERv2 ?

CLIENTv2

call accept

SERVERv2

call service continue

CLIENT

call request

SERVER

call reply wait reply service continue

Structure Diagrams

slide-47
SLIDE 47

47 a:USER

printer

b:USER

printer

printer: RESOURCE

acquire release

PRINTER_SHARE

RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use->printer.release->USER). ||PRINTER_SHARE = (a:USER || b:USER || {a,b}::printer:RESOURCE).

Structure Diagrams – Resource Sharing

slide-48
SLIDE 48

48

THREAD = OFF, OFF = (toggle->ON |abort->STOP), ON = (toggle->OFF |output->ON |abort->STOP). ||THREAD_DEMO = (a:THREAD || b:THREAD) /{stop/{a,b}.abort}. Interpret:

toggle, abort

as inputs

  • utput

as output a:T b:T

stop a.toggle a.output b.output b.toggle

THREAD_DEMO

ThreadDemo Model

slide-49
SLIDE 49

49

class MyThread extends Thread { private boolean on; MyThread() {

  • n = false;

} void toggle() { on = !on; } void abort() { interrupt(); } private void output() { System.out.println(“output”); } public void run() { try { while (true) { if (on) output(); sleep(1000); } } catch(InterruptedException _) { System.out.println(“Done!”); } } }

ThreadDemo Code: MyThread

slide-50
SLIDE 50

50

class MyThread extends Thread { private boolean on; MyThread() {

  • n = false;

} void toggle() { on = !on; } void abort() { interrupt(); } private void output() { System.out.println(“output”); } public void run() { try { while (true) { if (on) output(); sleep(1000); } } catch(InterruptedException _) { System.out.println(“Done!”); } } }

ThreadDemo Code: MyThread

Interpret:

toggle, abort

as inputs

  • utput

as output

slide-51
SLIDE 51

51

class ThreadDemo { private stop(MyThread a, MyThread b) { a.abort(); b.abort(); } public static void main(String[] args) { MyThread a = new MyThread(); MyThread b = new MyThread(); a.start(); b.start(); while (true) { switch (readChar()) { case ‘a’: a.toogle(); break; case ‘b’: b.toogle(); break; case ‘i’: stop(a,b); return; } } } }

ThreadDemo Code: ThreadDemo

slide-52
SLIDE 52

52

Summary

➢ Concepts: concurrent processes and process

interaction.

➢ Models:

– asynchronous (arbitrary speed) & interleaving (arbitrary

  • rder)

– parallel composition (finite state process with action

interleaving)

– process interaction (shared actions) – process labeling, action relabeling, and hiding – structure diagrams

➢ Practice: multiple threads in Java.