Exam Questions & Revision DM519 Concurrent Programming 1 1 - - PowerPoint PPT Presentation

exam questions revision
SMART_READER_LITE
LIVE PREVIEW

Exam Questions & Revision DM519 Concurrent Programming 1 1 - - PowerPoint PPT Presentation

Exam Questions & Revision DM519 Concurrent Programming 1 1 The Main Aims Of The Course Construct models from specifications of concurrency problems DM519 Concurrent Programming 2 2 The Main Aims Of The Course Construct models from


slide-1
SLIDE 1

DM519 Concurrent Programming

Exam Questions & Revision

1 1

slide-2
SLIDE 2

DM519 Concurrent Programming

The Main Aims Of The Course

Construct models from specifications of concurrency problems

2 2

slide-3
SLIDE 3

DM519 Concurrent Programming

The Main Aims Of The Course

Construct models from specifications of concurrency problems Test, analyse, and compare models’ behaviour

2 2

slide-4
SLIDE 4

DM519 Concurrent Programming

The Main Aims Of The Course

Construct models from specifications of concurrency problems Test, analyse, and compare models’ behaviour Define and verify models’ safety & liveness properties

2 2

slide-5
SLIDE 5

DM519 Concurrent Programming

The Main Aims Of The Course

Construct models from specifications of concurrency problems Test, analyse, and compare models’ behaviour Define and verify models’ safety & liveness properties Implement models in Java

2 2

slide-6
SLIDE 6

DM519 Concurrent Programming

The Main Aims Of The Course

Construct models from specifications of concurrency problems Test, analyse, and compare models’ behaviour Define and verify models’ safety & liveness properties Implement models in Java Relate models and implementations

2 2

slide-7
SLIDE 7

DM519 Concurrent Programming 3

CSP

The following is a sample of some of the covered topics

Revision

3

slide-8
SLIDE 8

DM519 Concurrent Programming

Outline Of Covered Chapters

4

  • 2. Processes and Threads
  • 3. Concurrent Execution
  • 4. Shared Objects & Interference
  • 5. Monitors & Condition Synchronisation
  • 6. Deadlock
  • 7. Safety and Liveness Properties
  • 8. Model-based Design
  • 9. Dynamic systems

10.Message Passing

Concepts Models Practice

The main basic

Advanced topics …

4

slide-9
SLIDE 9

DM519 Concurrent Programming

Models: FSP & LTS

Model = simplified representation of the real world u Based on Labelled Transition Systems (LTS ): u Described textually as Finite State Processes (FSP):

EngineOff = (engineOn -> EngineOn), EngineOn = (engineOff -> EngineOff |speed -> EngineOn). Focuses on concurrency aspects (of the program)

  • everything else abstracted away

5 5

slide-10
SLIDE 10

DM519 Concurrent Programming

Finite State Processes (FSP)

FSPs can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

const N = 3

// constant definitions

range R = 0..N

// range definitions

set S = {a,b,c}

// set definitions

6 6

slide-11
SLIDE 11

DM519 Concurrent Programming

Finite State Processes (FSP)

FSPs can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

const N = 3

// constant definitions

range R = 0..N

// range definitions

set S = {a,b,c}

// set definitions

6

range T = 0..3 BUFF = (in[i:T]->out[i]->BUFF).

6

slide-12
SLIDE 12

DM519 Concurrent Programming

Finite State Processes (FSP)

FSPs can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

const N = 3

// constant definitions

range R = 0..N

// range definitions

set S = {a,b,c}

// set definitions

6

range T = 0..3 BUFF = (in[i:T]->out[i]->BUFF).

6

slide-13
SLIDE 13

DM519 Concurrent Programming

Finite State Processes (FSP)

7 7

slide-14
SLIDE 14

DM519 Concurrent Programming

Finite State Processes (FSP)

FSP: – P || Q // parallel composition – a:P // process labelling (1 process/prefix) – {…}::P // process sharing (1 process w/all prefixes) – P / {x/y} // action relabelling – P \ {…} // hiding – P @ {…} // keeping (hide complement)

7 7

slide-15
SLIDE 15

DM519 Concurrent Programming

Finite State Processes (FSP)

FSP: – P || Q // parallel composition – a:P // process labelling (1 process/prefix) – {…}::P // process sharing (1 process w/all prefixes) – P / {x/y} // action relabelling – P \ {…} // hiding – P @ {…} // keeping (hide complement)

7

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

  • ut/b.out}

@{in,out}.

7

slide-16
SLIDE 16

DM519 Concurrent Programming

Finite State Processes (FSP)

FSP: – P || Q // parallel composition – a:P // process labelling (1 process/prefix) – {…}::P // process sharing (1 process w/all prefixes) – P / {x/y} // action relabelling – P \ {…} // hiding – P @ {…} // keeping (hide complement)

7

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

  • ut/b.out}

@{in,out}.

7

slide-17
SLIDE 17

DM519 Concurrent Programming

Finite State Processes (FSP)

FSP: – P || Q // parallel composition – a:P // process labelling (1 process/prefix) – {…}::P // process sharing (1 process w/all prefixes) – P / {x/y} // action relabelling – P \ {…} // hiding – P @ {…} // keeping (hide complement) Structure Diagrams:

a:BUFF b:BUFF a.out TWOBUFF

  • ut

in in

  • ut

in

  • ut

7

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

  • ut/b.out}

@{in,out}.

7

slide-18
SLIDE 18

DM519 Concurrent Programming

Structure Diagrams - Resource Sharing

RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use->printer.release->USER).

8 8

slide-19
SLIDE 19

DM519 Concurrent Programming

Structure Diagrams - Resource Sharing

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

8 8

slide-20
SLIDE 20

DM519 Concurrent Programming

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). ||PRINTER_SHARE = (a:USER || b:USER || {a,b}::printer:RESOURCE).

8 8

slide-21
SLIDE 21

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

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

9

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. make ready make make ready ready ready ready ready use use use 0,0 0,1 0,2 1,0 1,2 1,1 2,0 2,2 2,1

9

slide-22
SLIDE 22

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

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

9

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. make ready make make ready ready ready ready ready use use use 0,0 0,1 0,2 1,0 1,2 1,1 2,0 2,2 2,1

9

slide-23
SLIDE 23

DM519 Concurrent Programming

Chapter 4: Shared Objects & Mutual Exclusion

u Concepts:

l Process interference l Mutual exclusion

u Models:

l Model-checking for interference l Modelling mutual exclusion

u Practice:

l Thread interference in shared objects in Java l Mutual exclusion in Java l Synchronised objects, methods, and statements

10 10

slide-24
SLIDE 24

DM519 Concurrent Programming

Condition Synchronisation In FSP And Java

FSP: when (cond) action -> NEWSTATE

synchronized void action() throws Int’Exc’ { while (!cond) wait(); // modify monitor data notifyAll(); }

The while loop is necessary to re-test the condition cond to ensure that cond is indeed satisfied when it re-enters the monitor. notifyAll() is necessary to awaken other thread(s) that may be waiting to enter the monitor now that the monitor data has been changed.

11 11

slide-25
SLIDE 25

DM519 Concurrent Programming

Condition Synchronisation (in Java)

class CarParkControl { protected int spaces, capacity; synchronized void arrive() throws Int’Exc’ { while (!(spaces>0)) wait();

  • -spaces;

notifyAll(); } synchronized void depart() throws Int’Exc’ { while (!(spaces<capacity)) wait(); ++spaces; notifyAll(); } }

CONTROL(CAPACITY=4) = SPACES[CAPACITY], SPACES[spaces:0..CAPACITY] = (when(spaces>0) arrive -> SPACES[spaces-1] |when(spaces<CAPACITY) depart -> SPACES[spaces+1]).

12 12

slide-26
SLIDE 26

DM519 Concurrent Programming

Condition Synchronisation (in Java)

class CarParkControl { protected int spaces, capacity; synchronized void arrive() throws Int’Exc’ { while (!(spaces>0)) wait();

  • -spaces;

notifyAll(); } synchronized void depart() throws Int’Exc’ { while (!(spaces<capacity)) wait(); ++spaces; notifyAll(); } }

CONTROL(CAPACITY=4) = SPACES[CAPACITY], SPACES[spaces:0..CAPACITY] = (when(spaces>0) arrive -> SPACES[spaces-1] |when(spaces<CAPACITY) depart -> SPACES[spaces+1]).

12

notify() instead of notifyAll() ?

  • 1. Uniform waiters - everybody

waits on the same condition

  • 2. One-in, one-out

What goes wrong with notify and 8xDepartures, 5xArrivals?

12

slide-27
SLIDE 27

DM519 Concurrent Programming

Semaphores

Semaphores are widely used for dealing with inter-process synchronisation in operating systems. Semaphore s : integer var that can take only non-neg. values. sem.down(); // decrement (block if counter = 0) sem.up(); // increment counter (allowing one blocked thread to pass)

13 13

slide-28
SLIDE 28

DM519 Concurrent Programming

full empty

synchronized public E get() throws InterruptedException{ full.down(); // if no items, block! ... }

buffer

14

Nested Monitor Problem

14

slide-29
SLIDE 29

DM519 Concurrent Programming

full empty

synchronized public E get() throws InterruptedException{ full.down(); // if no items, block! ... }

get down wait full full put buffer

14

Nested Monitor Problem

14

slide-30
SLIDE 30

DM519 Concurrent Programming

Deadlock: 4 Necessary And Sufficient Conditions

  • 1. Mutual exclusion condition (aka. “Serially reusable resources”):

the processes involved share resources which they use under mutual exclusion.

  • 2. Hold-and-wait condition (aka. “Incremental acquisition”):

processes hold on to resources already allocated to them while waiting to acquire additional resources.

  • 3. No preemption condition:
  • nce acquired by a process, resources cannot be “pre-empted” (forcibly

withdrawn) but are only released voluntarily.

  • 4. Circular-wait condition (aka. “Wait-for cycle”):

a circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire.

15 15

slide-31
SLIDE 31

DM519 Concurrent Programming

Deadlock: 4 Necessary And Sufficient Conditions

  • 1. Mutual exclusion condition (aka. “Serially reusable resources”):

the processes involved share resources which they use under mutual exclusion.

  • 2. Hold-and-wait condition (aka. “Incremental acquisition”):

processes hold on to resources already allocated to them while waiting to acquire additional resources.

  • 3. No preemption condition:
  • nce acquired by a process, resources cannot be “pre-empted” (forcibly

withdrawn) but are only released voluntarily.

  • 4. Circular-wait condition (aka. “Wait-for cycle”):

a circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire.

15

Deadlock avoidance: “Break at least one of the deadlock conditions”.

15

slide-32
SLIDE 32

DM519 Concurrent Programming

♦ STOP or deadlocked state (no outgoing transitions) ♦ ERROR process (-1) to detect erroneous behaviour

7.1 Safety Properties

RESOURCE =(acquire -> ACQUIRED), ACQUIRED =(release -> RESOURCE |acquire -> ERROR).

Trace to property violation in RESOURCE: acquire acquire ♦ Analysis using LTSA: (shortest trace) A safety property asserts that nothing bad happens.

16 16

slide-33
SLIDE 33

DM519 Concurrent Programming

Safety - Property Specification

♦ERROR conditions state what is not required (~ exceptions). ♦ In complex systems, it is usually better to specify safety properties by stating directly what is required. property SAFE_RESOURCE = (acquire -> release -> SAFE_RESOURCE). RESOURCE = (acquire -> (release -> RESOURCE |acquire -> ERROR) |release -> ERROR).

17

is equivalent to

17

slide-34
SLIDE 34

DM519 Concurrent Programming

7.3 Liveness Properties

A safety property asserts that nothing bad happens. A liveness property asserts that something good eventually happens. E.g., does every car eventually get an opportunity to cross the bridge, i.e., make progress? A progress property asserts that it is always the case that an action is eventually executed. Progress is the opposite of starvation (= the name given to a concurrent programming situation in which an action is never executed).

18 18

slide-35
SLIDE 35

DM519 Concurrent Programming

Progress Properties

progress HEADS = {heads} ? progress TAILS = {tails} ? LTSA check progress: No progress violations detected progress P = {a1, a2, …, an} This defines a progress property, P, which asserts that in an infinite execution, at least one of the actions a1, a2, …, an will be executed infinitely often. COIN = (toss->heads->COIN | toss->tails->COIN).

J J

19 19

slide-36
SLIDE 36

DM519 Concurrent Programming

Dynamic Systems Concepts: dynamic creation and deletion of processes

Resource allocation example – varying number of users and resources. master-slave interaction

Models: static - fixed populations with cyclic behavior

interaction

Practice: dynamic creation and deletion of threads

(# active threads varies during execution) Resource allocation algorithms Java join() method

20 20

slide-37
SLIDE 37

DM519 Concurrent Programming

♦ send(e,c) - send e to channel c. The sender is blocked until the message is received from the channel.

10.1 Synchronous Message Passing - Channel Channel c Sender

send(e,c)

Receiver

v=receive(c) ♦ v = receive(c) - receive a value into local variable v from channel c. The calling process is blocked until a message is sent to the channel. Channel has no buffering

  • ne-to-one

21

Corresponds to “v = e”

21

slide-38
SLIDE 38

DM519 Concurrent Programming

Selective Receive Channels c1 c2 cn

How should we deal with multiple channels?

Sender

send(e,c)

Sender

send(e,c)

Sender[n]

send(en,cn) select when G1 and v1=receive(chan1) => S1;

  • r

when G2 and v2=receive(chan2) => S2;

  • r

  • r

when Gn and vn=receive(chann) => Sn; end Select statement...

22 22

slide-39
SLIDE 39

DM519 Concurrent Programming

♦ send(e,p) - send e to port p. The calling process is not blocked. The message is queued at the port if the receiver is not waiting.

10.2 Asynchronous Message Passing - Port Port p Receiver

v=receive(p) ♦ v = receive(p) - receive a value into local variable v from port p. The calling process is blocked if no messages queued to the port.

Sender

send(e,c)

Sender

send(e,c)

Sender[n]

send(en,p) many-to-one

23 23

slide-40
SLIDE 40

DM519 Concurrent Programming

10.3 Rendezvous - Entry Client Server

req=accept(entry) res=call(entry,req) reply(entry,res)

Request message Reply message

suspended perform service

Rendezvous is a form of request-reply to support client server

  • communication. Many clients may request service, but only one is

serviced at a time.

24 24

slide-41
SLIDE 41

DM519 Concurrent Programming

The Main Aims Of The Course (Repetition)

Construct models from specifications of concurrency problems Test, analyse, and compare models’ behaviour Define and verify models’ safety & liveness properties Implement models in Java Relate models and implementations

25 25