COMP30112: Concurrency Topics 4.2: Modelling and Analyzing - - PowerPoint PPT Presentation

comp30112 concurrency
SMART_READER_LITE
LIVE PREVIEW

COMP30112: Concurrency Topics 4.2: Modelling and Analyzing - - PowerPoint PPT Presentation

Topic 4.2: Modelling and Analyzing Interference in FSP COMP30112: Concurrency Topics 4.2: Modelling and Analyzing Interference in FSP Howard Barringer Room KB2.20: email: Howard.Barringer@manchester.ac.uk February 2008 Topic 4.2: Modelling and


slide-1
SLIDE 1

Topic 4.2: Modelling and Analyzing Interference in FSP

COMP30112: Concurrency

Topics 4.2: Modelling and Analyzing Interference in FSP

Howard Barringer

Room KB2.20: email: Howard.Barringer@manchester.ac.uk

February 2008

slide-2
SLIDE 2

Topic 4.2: Modelling and Analyzing Interference in FSP

Outline

Topic 4.2: Modelling and Analyzing Interference in FSP Interference A GUI Example FSP Modelling of the Garden Modelling Locks in FSP

slide-3
SLIDE 3

Topic 4.2: Modelling and Analyzing Interference in FSP

Outline

Topic 4.2: Modelling and Analyzing Interference in FSP Interference A GUI Example FSP Modelling of the Garden Modelling Locks in FSP

slide-4
SLIDE 4

Topic 4.2: Modelling and Analyzing Interference in FSP

Interference - summary

  • When 2 or more threads have access to the same object, there

is potential for interference between the actions on the object

slide-5
SLIDE 5

Topic 4.2: Modelling and Analyzing Interference in FSP

Interference - summary

  • When 2 or more threads have access to the same object, there

is potential for interference between the actions on the object

  • Destructive update caused by arbitrary interleaving of read

and write actions is interference

slide-6
SLIDE 6

Topic 4.2: Modelling and Analyzing Interference in FSP

Interference - summary

  • When 2 or more threads have access to the same object, there

is potential for interference between the actions on the object

  • Destructive update caused by arbitrary interleaving of read

and write actions is interference

  • Interference leads to unexpected and undesirable behaviour
slide-7
SLIDE 7

Topic 4.2: Modelling and Analyzing Interference in FSP

Interference - summary

  • When 2 or more threads have access to the same object, there

is potential for interference between the actions on the object

  • Destructive update caused by arbitrary interleaving of read

and write actions is interference

  • Interference leads to unexpected and undesirable behaviour
  • The Ornamental Garden (from Magee and Kramer) example

is a good demonstration

slide-8
SLIDE 8

Topic 4.2: Modelling and Analyzing Interference in FSP

Interference - summary

  • When 2 or more threads have access to the same object, there

is potential for interference between the actions on the object

  • Destructive update caused by arbitrary interleaving of read

and write actions is interference

  • Interference leads to unexpected and undesirable behaviour
  • The Ornamental Garden (from Magee and Kramer) example

is a good demonstration

  • We will model and analyze the problem in FSP
slide-9
SLIDE 9

Topic 4.2: Modelling and Analyzing Interference in FSP

Outline

Topic 4.2: Modelling and Analyzing Interference in FSP Interference A GUI Example FSP Modelling of the Garden Modelling Locks in FSP

slide-10
SLIDE 10

Topic 4.2: Modelling and Analyzing Interference in FSP

GUI Example: Ornamental Garden

How Many Visitors are in The Garden??

slide-11
SLIDE 11

Topic 4.2: Modelling and Analyzing Interference in FSP

Desired Behaviour

  • Again, two processes reading and updating a common object
slide-12
SLIDE 12

Topic 4.2: Modelling and Analyzing Interference in FSP

Desired Behaviour

  • Again, two processes reading and updating a common object
  • Complexity due to GUI. . .
slide-13
SLIDE 13

Topic 4.2: Modelling and Analyzing Interference in FSP

Desired Behaviour

  • Again, two processes reading and updating a common object
  • Complexity due to GUI. . .
  • . . . and persuading the code/scheduler to Do The Wrong

Thing!!!

slide-14
SLIDE 14

Topic 4.2: Modelling and Analyzing Interference in FSP

Oh Dear!! ... It’s Gone Wrong

slide-15
SLIDE 15

Topic 4.2: Modelling and Analyzing Interference in FSP

Ornamental Garden: Class Structure

Applet Thread init() go() run() NumberCanvas setvalue() Counter increment()

east, west eastD, westD, counterD display

Garden Turnstile

display people

slide-16
SLIDE 16

Topic 4.2: Modelling and Analyzing Interference in FSP

The Garden Class

public class Garden extends Applet { private void go() { counter = new Counter(counterD); west= new Turnstile(westD,counter); east= new Turnstile(eastD,counter); west.start(); east.start(); }

slide-17
SLIDE 17

Topic 4.2: Modelling and Analyzing Interference in FSP

The Counter Class

class Counter { int value=0; NumberCanvas display; Counter(NumberCanvas n) { display=n; display.setvalue(value); } void increment() { int temp = value; //read[v] Simulate.HWinterrupt(); value=temp+1; //write[v+1] display.setvalue(value); } }

slide-18
SLIDE 18

Topic 4.2: Modelling and Analyzing Interference in FSP

The Turnstile Class

class Turnstile extends Thread { NumberCanvas display; Counter people; Turnstile(NumberCanvas n,Counter c) { display = n; people = c; } public void run() { try{ display.setvalue(0); for (int i=1;i<=Garden.MAX;i++){ Thread.sleep(500); //0.5 second display.setvalue(i); people.increment(); } } catch (InterruptedException e) {} } }

slide-19
SLIDE 19

Topic 4.2: Modelling and Analyzing Interference in FSP

Simulation of Hardware Interruption

class Simulate { public static void HWinterrupt() { if (Math.random()<0.5) try{ Thread.sleep(200); } catch(InterruptedException e){}; } }

slide-20
SLIDE 20

Topic 4.2: Modelling and Analyzing Interference in FSP

Outline

Topic 4.2: Modelling and Analyzing Interference in FSP Interference A GUI Example FSP Modelling of the Garden Modelling Locks in FSP

slide-21
SLIDE 21

Topic 4.2: Modelling and Analyzing Interference in FSP

Ornamental Garden: FSP Model

const N = 4 range T = 0..N set VarAlpha = {value.{read[T], write[T]}} VAR = VAR[0], VAR[u : T] = (read[u] → VAR[u] | write[v : T] → VAR[v]). TURNSTILE = (go → RUN), RUN = (arrive → INCREMENT | end → TURNSTILE), INCREMENT = (value.read[x : T] → value.write[x + 1] → RUN ) + VarAlpha. ||GARDEN = (east : TURNSTILE || west : TURNSTILE || {east, west, display} :: value : VAR) /{go/{east, west}.go}, end/{east, west}.end}.

slide-22
SLIDE 22

Topic 4.2: Modelling and Analyzing Interference in FSP

Exhaustive Analysis

TEST = TEST[0], TEST[v:T] = (when (v<N){east.arrive,west.arrive}->TEST[v+1] | end->CHECK[v] ), CHECK[v:T] = (display.value.read[u:T] -> (when (u==v) right -> TEST[v] |when (u!=v) wrong -> ERROR) )+{display.{VarAlpha}}.

Now compose TEST in parallel with the GARDEN process

||TESTGARDEN = (GARDEN || TEST).

The act of building the above composition will check all possible runs of the GARDEN. And we see it fails.

slide-23
SLIDE 23

Topic 4.2: Modelling and Analyzing Interference in FSP

Outline

Topic 4.2: Modelling and Analyzing Interference in FSP Interference A GUI Example FSP Modelling of the Garden Modelling Locks in FSP

slide-24
SLIDE 24

Topic 4.2: Modelling and Analyzing Interference in FSP

An FSP Model for Locks

LOCK = (acquire -> release -> LOCK). ||LOCKVAR = (LOCK || VAR) . set VarAlpha = {value.{read[T],write[T], acquire,release}} INCREMENT = (value.acquire

  • > value.read[x:T] -> value.write[x+1] ->

value.release -> RUN ) + VarAlpha.

slide-25
SLIDE 25

Topic 4.2: Modelling and Analyzing Interference in FSP

Include in the FSP GARDEN model and ..

. . . ||GARDEN = (east:TURNSTILE || west:TURNSTILE || east,west,display::value:LOCKVAR) /{go /{east,west}.go, end/{east,west}.end}.

and re-test

||TESTGARDEN = (GARDEN || TEST).

And the composition occurs without ERRORs.

slide-26
SLIDE 26

Topic 4.2: Modelling and Analyzing Interference in FSP

Synchronized Counter

For the Java version, create an extension of the Counter that has a synchronised increment() method.

slide-27
SLIDE 27

Topic 4.2: Modelling and Analyzing Interference in FSP

Synchronized Counter

For the Java version, create an extension of the Counter that has a synchronised increment() method.

class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) super(n); synchronized void increment() { super.increment(); } }

slide-28
SLIDE 28

Topic 4.2: Modelling and Analyzing Interference in FSP

Synchronized Counter

For the Java version, create an extension of the Counter that has a synchronised increment() method.

class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) super(n); synchronized void increment() { super.increment(); } }

and use SynchronizedCounter in place of Counter.

slide-29
SLIDE 29

Topic 4.2: Modelling and Analyzing Interference in FSP

Synchronized Counter

For the Java version, create an extension of the Counter that has a synchronised increment() method.

class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) super(n); synchronized void increment() { super.increment(); } }

and use SynchronizedCounter in place of Counter. (This is used in the applet when the “fix it” checkbox is checked)