Dynamic Systems DM519 Concurrent Programming 1 1 Repetition: - - PowerPoint PPT Presentation

dynamic systems
SMART_READER_LITE
LIVE PREVIEW

Dynamic Systems DM519 Concurrent Programming 1 1 Repetition: - - PowerPoint PPT Presentation

Chapter 9 Dynamic Systems DM519 Concurrent Programming 1 1 Repetition: Chapter 8 Model-Based Design Requirements Model Java DM519 Concurrent Programming 2 2 Repetition: Chapter 8 Model-Based Design Requirements Model Java DM519


slide-1
SLIDE 1

DM519 Concurrent Programming

Chapter 9

Dynamic Systems

1 1

slide-2
SLIDE 2

DM519 Concurrent Programming

Repetition: Chapter 8 Model-Based Design

2

Requirements Model Java

2

slide-3
SLIDE 3

DM519 Concurrent Programming

Repetition: Chapter 8 Model-Based Design

2

Requirements Model Java

2

slide-4
SLIDE 4

DM519 Concurrent Programming

Repetition: Chapter 8 Model-Based Design

2

Requirements Model Java

2

slide-5
SLIDE 5

DM519 Concurrent Programming

Course Outline

  • 2. Processes and Threads
  • 3. Concurrent Execution
  • 4. Shared Objects & Interference
  • 5. Monitors & Condition Synchronization
  • 6. Deadlock
  • 7. Safety and Liveness Properties
  • 8. Model-based Design
  • 9. Dynamic systems
  • 10. Message Passing
  • 11. Concurrent Software Architectures

Concepts Models Practice

  • 12. Timed Systems
  • 13. Program Verification
  • 14. Logical Properties

The main basic Advanced topics …

3 3

slide-6
SLIDE 6

DM519 Concurrent Programming

Dynamic Systems

4 4

slide-7
SLIDE 7

DM519 Concurrent Programming

Dynamic Systems Concepts: dynamic creation and deletion of processes

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

4 4

slide-8
SLIDE 8

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

4 4

slide-9
SLIDE 9

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

4 4

slide-10
SLIDE 10

DM519 Concurrent Programming

9.1 Golf Club Program

Player

d4 is

waiting for four balls Players at a Golf Club hire golf balls and then return them after use.

5 5

slide-11
SLIDE 11

DM519 Concurrent Programming

9.1 Golf Club Program

Expert players tend not to lose any golf balls and only hire one or two. Novice players hire more balls, so that they have spares during the game in case of loss. However, they buy replacements for lost balls so that they return the same number that they originally hired. Player

d4 is

waiting for four balls Players at a Golf Club hire golf balls and then return them after use.

5 5

slide-12
SLIDE 12

DM519 Concurrent Programming

Golf Club - Java Implementation

6 6

slide-13
SLIDE 13

DM519 Concurrent Programming

Golf Club - Java Implementation

The Java interface Allocator permits us to develop a few implementations of the golf ball allocator without modifying the rest of the program. public interface Allocator { public void get(int n) throws InterruptedException; public void put(int n); }

6 6

slide-14
SLIDE 14

DM519 Concurrent Programming

Golf Club - Java Implementation

The Java interface Allocator permits us to develop a few implementations of the golf ball allocator without modifying the rest of the program. DisplayAllocator class implements this interface and delegates calls to get and put to SimpleAllocator. public interface Allocator { public void get(int n) throws InterruptedException; public void put(int n); }

6 6

slide-15
SLIDE 15

DM519 Concurrent Programming

Java Implementation - SimpleAllocator Monitor

7 7

slide-16
SLIDE 16

DM519 Concurrent Programming

Java Implementation - SimpleAllocator Monitor

public class SimpleAllocator implements Allocator { private int available; public SimpleAllocator(int n) { available = n; }

7 7

slide-17
SLIDE 17

DM519 Concurrent Programming

Java Implementation - SimpleAllocator Monitor

public class SimpleAllocator implements Allocator { private int available; public SimpleAllocator(int n) { available = n; } synchronized public void get(int n) throws InterruptedException { while (n>available) wait(); available -= n; }

7 7

slide-18
SLIDE 18

DM519 Concurrent Programming

Java Implementation - SimpleAllocator Monitor

get blocks a calling thread until sufficient golf balls are available. public class SimpleAllocator implements Allocator { private int available; public SimpleAllocator(int n) { available = n; } synchronized public void get(int n) throws InterruptedException { while (n>available) wait(); available -= n; }

7 7

slide-19
SLIDE 19

DM519 Concurrent Programming

Java Implementation - SimpleAllocator Monitor

get blocks a calling thread until sufficient golf balls are available. public class SimpleAllocator implements Allocator { private int available; public SimpleAllocator(int n) { available = n; } synchronized public void get(int n) throws InterruptedException { while (n>available) wait(); available -= n; } synchronized public void put(int n) { available += n; notifyAll(); } }

7 7

slide-20
SLIDE 20

DM519 Concurrent Programming

Java Implementation - SimpleAllocator Monitor

get blocks a calling thread until sufficient golf balls are available. public class SimpleAllocator implements Allocator { private int available; public SimpleAllocator(int n) { available = n; } synchronized public void get(int n) throws InterruptedException { while (n>available) wait(); available -= n; } synchronized public void put(int n) { available += n; notifyAll(); } }

A novice thread requesting a large number of balls may be overtaken and remain blocked!

7 7

slide-21
SLIDE 21

DM519 Concurrent Programming

Java Implementation - Player Thread

class Player extends Thread { private GolfClub gc; private String name; private int nballs; Player(GolfClub g, int n, String s) { gc = g; name = s; nballs =n; } public void run() { try { gc.getGolfBalls(nballs,name); Thread.sleep(gc.playTime); gc.relGolfBalls(nballs,name); } catch (InterruptedException e){} } }

8 8

slide-22
SLIDE 22

DM519 Concurrent Programming

Java Implementation - Player Thread

class Player extends Thread { private GolfClub gc; private String name; private int nballs; Player(GolfClub g, int n, String s) { gc = g; name = s; nballs =n; } public void run() { try { gc.getGolfBalls(nballs,name); Thread.sleep(gc.playTime); gc.relGolfBalls(nballs,name); } catch (InterruptedException e){} } }

The run() method terminates after releasing golf balls. New player threads are created dynamically.

8 8

slide-23
SLIDE 23

DM519 Concurrent Programming

Dynamic Systems In Java

9 9

slide-24
SLIDE 24

DM519 Concurrent Programming

Dynamic Systems In Java

Approach 1: explicitly create threads,

  • Create one thread for each player

9

new Thread(new Player(...)).start()

9

slide-25
SLIDE 25

DM519 Concurrent Programming

Dynamic Systems In Java

Approach 1: explicitly create threads,

  • Create one thread for each player
  • Drawbacks:

– thread life cycle overhead – resources consumption, especially memory – Stability: no controlled limits on #threads that can be created, OutOfMemoryError

9

new Thread(new Player(...)).start()

9

slide-26
SLIDE 26

DM519 Concurrent Programming

Dynamic Systems In Java

10 10

slide-27
SLIDE 27

DM519 Concurrent Programming

Dynamic Systems In Java

Approach 2: Executor framework

10

interface Executor{ void execute(Runnable command); }

10

slide-28
SLIDE 28

DM519 Concurrent Programming

Dynamic Systems In Java

Approach 2: Executor framework

10

interface Executor{ void execute(Runnable command); } Executor exec = Executors.newFixedThreadPool(NTHREADS); exec.execute(new Player(...));

10

slide-29
SLIDE 29

DM519 Concurrent Programming

Dynamic Systems In Java

Approach 2: Executor framework

– By decoupling the task submission from execution, we can easily change or specify execution policies, such as

  • execution order, how many tasks are allowed to run

concurrently and how many are queued, etc.

10

interface Executor{ void execute(Runnable command); } Executor exec = Executors.newFixedThreadPool(NTHREADS); exec.execute(new Player(...));

10

slide-30
SLIDE 30

DM519 Concurrent Programming

9.2 Golf Club Model

11 11

slide-31
SLIDE 31

DM519 Concurrent Programming

9.2 Golf Club Model

const N=5 // maximum #golf balls range B=0..N // available range ALLOCATOR = BALL[N], BALL[b:B] = (when (b>0) get[i:1..b]->BALL[b-i] |put[j:1..N] ->BALL[b+j] ). Allocator:

11 11

slide-32
SLIDE 32

DM519 Concurrent Programming

9.2 Golf Club Model

const N=5 // maximum #golf balls range B=0..N // available range ALLOCATOR = BALL[N], BALL[b:B] = (when (b>0) get[i:1..b]->BALL[b-i] |put[j:1..N] ->BALL[b+j] ). Allocator:

Allocator will accept requests for up to b balls, and block requests for more than b balls.

11 11

slide-33
SLIDE 33

DM519 Concurrent Programming

9.2 Golf Club Model

const N=5 // maximum #golf balls range B=0..N // available range ALLOCATOR = BALL[N], BALL[b:B] = (when (b>0) get[i:1..b]->BALL[b-i] |put[j:1..N] ->BALL[b+j] ). How do we model the potentially infinite stream of dynamically created player threads? Allocator:

Allocator will accept requests for up to b balls, and block requests for more than b balls.

Players:

11 11

slide-34
SLIDE 34

DM519 Concurrent Programming

9.2 Golf Club Model

const N=5 // maximum #golf balls range B=0..N // available range ALLOCATOR = BALL[N], BALL[b:B] = (when (b>0) get[i:1..b]->BALL[b-i] |put[j:1..N] ->BALL[b+j] ). How do we model the potentially infinite stream of dynamically created player threads? Allocator:

Allocator will accept requests for up to b balls, and block requests for more than b balls.

Players:

Cannot model infinite state spaces, but can model infinite (repetitive) behaviors.

11 11

slide-35
SLIDE 35

DM519 Concurrent Programming

Golf Club Model

12 12

slide-36
SLIDE 36

DM519 Concurrent Programming

Golf Club Model

Players:

12 12

slide-37
SLIDE 37

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range Players:

12 12

slide-38
SLIDE 38

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range Players:

Fixed population of golfers: infinite stream of requests.

12 12

slide-39
SLIDE 39

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range PLAYER = (need[b:R]->PLAYER[b]), PLAYER[b:R] = (get[b]->put[b]->PLAYER[b]). Players:

Fixed population of golfers: infinite stream of requests.

12 12

slide-40
SLIDE 40

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range PLAYER = (need[b:R]->PLAYER[b]), PLAYER[b:R] = (get[b]->put[b]->PLAYER[b]). set Experts = {alice,bob,chris} set Novices = {dave,eve} set Players = {Experts,Novices} Players:

Fixed population of golfers: infinite stream of requests.

12 12

slide-41
SLIDE 41

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range PLAYER = (need[b:R]->PLAYER[b]), PLAYER[b:R] = (get[b]->put[b]->PLAYER[b]). set Experts = {alice,bob,chris} set Novices = {dave,eve} set Players = {Experts,Novices} Players:

Fixed population of golfers: infinite stream of requests.

Players is the

union of Experts and Novices.

12 12

slide-42
SLIDE 42

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range PLAYER = (need[b:R]->PLAYER[b]), PLAYER[b:R] = (get[b]->put[b]->PLAYER[b]). set Experts = {alice,bob,chris} set Novices = {dave,eve} set Players = {Experts,Novices} HANDICAP = ({Novices.{need[3..N]},Experts.need[1..2]}

  • > HANDICAP

) +{Players.need[R]}. Players:

Fixed population of golfers: infinite stream of requests.

Players is the

union of Experts and Novices.

12 12

slide-43
SLIDE 43

DM519 Concurrent Programming

Golf Club Model

range R=1..N //request range PLAYER = (need[b:R]->PLAYER[b]), PLAYER[b:R] = (get[b]->put[b]->PLAYER[b]). set Experts = {alice,bob,chris} set Novices = {dave,eve} set Players = {Experts,Novices} HANDICAP = ({Novices.{need[3..N]},Experts.need[1..2]}

  • > HANDICAP

) +{Players.need[R]}. Players:

Fixed population of golfers: infinite stream of requests.

Players is the

union of Experts and Novices.

Constraint on need action of each player.

12 12

slide-44
SLIDE 44

DM519 Concurrent Programming

Golf Club Model - Analysis

||GOLFCLUB =( Players:PLAYER ||Players::ALLOCATOR ||HANDICAP). CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->SPACES[i-1]

ALLOCATOR get put get need Players:PLAYER put HANDICAP Players.need

GOLFCLUB

13 13

slide-45
SLIDE 45

DM519 Concurrent Programming

Golf Club Model - Analysis

||GOLFCLUB =( Players:PLAYER ||Players::ALLOCATOR ||HANDICAP).

Safety? Do players

return the right number of balls?

Liveness? Are players

eventually allocated balls ? CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->SPACES[i-1]

ALLOCATOR get put get need Players:PLAYER put HANDICAP Players.need

GOLFCLUB

13 13

slide-46
SLIDE 46

DM519 Concurrent Programming

Golf Club Model - Liveness

progress NOVICE = {Novices.get[R]} progress EXPERT = {Experts.get[R]} ||ProgressCheck = GOLFCLUB >>{Players.put[R]}.

14 14

slide-47
SLIDE 47

DM519 Concurrent Programming

Golf Club Model - Liveness

progress NOVICE = {Novices.get[R]} progress EXPERT = {Experts.get[R]} ||ProgressCheck = GOLFCLUB >>{Players.put[R]}. Progress violation: NOVICE Trace to terminal set of states: alice.need.2 bob.need.2 chris.need.2 chris.get.2 dave.need.5 eve.need.5 Cycle in terminal set: alice.get.2 alice.put.2 Actions in terminal set: {alice, bob, chris}.{get, put}[2]

14 14

slide-48
SLIDE 48

DM519 Concurrent Programming

Golf Club Model - Liveness

progress NOVICE = {Novices.get[R]} progress EXPERT = {Experts.get[R]} ||ProgressCheck = GOLFCLUB >>{Players.put[R]}. Progress violation: NOVICE Trace to terminal set of states: alice.need.2 bob.need.2 chris.need.2 chris.get.2 dave.need.5 eve.need.5 Cycle in terminal set: alice.get.2 alice.put.2 Actions in terminal set: {alice, bob, chris}.{get, put}[2]

Novice players

dave and eve

suffer starvation. They are continually

  • vertaken by

experts alice,

bob and chris.

14 14

slide-49
SLIDE 49

DM519 Concurrent Programming

9.3 Fair Allocation

const TM = 5 // maximum ticket range T = 1..TM // ticket values TICKET = NEXT[1], NEXT[t:T] = (ticket[t]->NEXT[t%TM+1]). Allocation in arrival order, using tickets:

15 15

slide-50
SLIDE 50

DM519 Concurrent Programming

9.3 Fair Allocation

const TM = 5 // maximum ticket range T = 1..TM // ticket values TICKET = NEXT[1], NEXT[t:T] = (ticket[t]->NEXT[t%TM+1]). Allocation in arrival order, using tickets: Players and Allocator: PLAYER = (need[b:R]->PLAYER[b]), PLAYER[b:R]= (ticket[t:T]->get[b][t]->put[b]

  • >PLAYER[b]).

ALLOCATOR = BALL[N][1], BALL[b:B][t:T] = (when (b>0) get[i:1..b][t]->BALL[b-i][t%TM+1] |put[j:1..N] ->BALL[b+j][t] ).

15 15

slide-51
SLIDE 51

DM519 Concurrent Programming

Fair Allocation - Analysis

HANDICAP = ({Novices.{need[4]},Experts.need[1]}-> HANDICAP ) +{Players.need[R]}. Ticketing increases the size of the model for analysis. We compensate by modifying the HANDICAP constraint: ||GOLFCLUB =( Players:PLAYER ||Players::(ALLOCATOR||TICKET) ||HANDICAP).

Experts use 1 ball, Novices use 4 balls.

16 16

slide-52
SLIDE 52

DM519 Concurrent Programming

Fair Allocation - Analysis

HANDICAP = ({Novices.{need[4]},Experts.need[1]}-> HANDICAP ) +{Players.need[R]}. Ticketing increases the size of the model for analysis. We compensate by modifying the HANDICAP constraint: ||GOLFCLUB =( Players:PLAYER ||Players::(ALLOCATOR||TICKET) ||HANDICAP).

Safety? Liveness?

progress NOVICE = {Novices.get[R][T]} progress EXPERT = {Experts.get[R][T]}

Experts use 1 ball, Novices use 4 balls.

16 16

slide-53
SLIDE 53

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

17 17

slide-54
SLIDE 54

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

17 17

slide-55
SLIDE 55

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

synchronized public void get(int n) throws InterruptedException {

17 17

slide-56
SLIDE 56

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

synchronized public void get(int n) throws InterruptedException { long myturn = turn; ++turn;

17 17

slide-57
SLIDE 57

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

Block calling thread until sufficient balls and next turn.

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

synchronized public void get(int n) throws InterruptedException { long myturn = turn; ++turn; while (n>available || myturn != next) wait();

17 17

slide-58
SLIDE 58

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

Block calling thread until sufficient balls and next turn.

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

synchronized public void get(int n) throws InterruptedException { long myturn = turn; ++turn; while (n>available || myturn != next) wait(); ++next; available -= n; notifyAll(); }

17 17

slide-59
SLIDE 59

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

Block calling thread until sufficient balls and next turn.

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

synchronized public void get(int n) throws InterruptedException { long myturn = turn; ++turn; while (n>available || myturn != next) wait(); ++next; available -= n; notifyAll(); } synchronized public void put(int n) { available += n; notifyAll(); } }

17 17

slide-60
SLIDE 60

DM519 Concurrent Programming

9.4 Revised Golf Club Program - FairAllocator Monitor

Block calling thread until sufficient balls and next turn.

public class FairAllocator implements Allocator { private int available; private long turn = 0; // next ticket to be dispensed private long next = 0; // next ticket to be served

public FairAllocator(int n) { available = n; }

synchronized public void get(int n) throws InterruptedException { long myturn = turn; ++turn; while (n>available || myturn != next) wait(); ++next; available -= n; notifyAll(); } synchronized public void put(int n) { available += n; notifyAll(); } }

Why is it necessary for get to include notifyAll()?

17 17

slide-61
SLIDE 61

DM519 Concurrent Programming

Revised Golf Club Program - FairAllocator

18 18

slide-62
SLIDE 62

DM519 Concurrent Programming

Revised Golf Club Program - FairAllocator

Players g1 and h1 are waiting. Even though two balls are available, they cannot overtake player f4.

18 18

slide-63
SLIDE 63

DM519 Concurrent Programming

Revised Golf Club Program - FairAllocator

Players g1 and h1 are waiting. Even though two balls are available, they cannot overtake player f4. What happens if c, d and e all return their golf balls?

18 18

slide-64
SLIDE 64

DM519 Concurrent Programming

9.5 Bounded Allocation

Allocation in arrival order is not efficient. A bounded allocation scheme allows experts to overtake novices but denies starvation by setting an upper bound on the number of times a novice can be

  • vertaken.

19 19

slide-65
SLIDE 65

DM519 Concurrent Programming

9.5 Bounded Allocation

Allocation in arrival order is not efficient. A bounded allocation scheme allows experts to overtake novices but denies starvation by setting an upper bound on the number of times a novice can be

  • vertaken.

We model players who have overtaken others as a set.

19 19

slide-66
SLIDE 66

DM519 Concurrent Programming

9.5 Bounded Allocation

Allocation in arrival order is not efficient. A bounded allocation scheme allows experts to overtake novices but denies starvation by setting an upper bound on the number of times a novice can be

  • vertaken.

We model players who have overtaken others as a set. const False = 0 const True = 1 range Bool = 0..1 ELEMENT(Id=0) = IN[False], IN[b:Bool] = ( add[Id] -> IN[True] | remove[Id] -> IN[False] | contains[Id][b] -> IN[b] ). ||SET = (forall[i:T] (ELEMENT(i))).

19 19

slide-67
SLIDE 67

DM519 Concurrent Programming

9.5 Bounded Allocation

Allocation in arrival order is not efficient. A bounded allocation scheme allows experts to overtake novices but denies starvation by setting an upper bound on the number of times a novice can be

  • vertaken.

A SET is modeled as the parallel composition

  • f elements

We model players who have overtaken others as a set. const False = 0 const True = 1 range Bool = 0..1 ELEMENT(Id=0) = IN[False], IN[b:Bool] = ( add[Id] -> IN[True] | remove[Id] -> IN[False] | contains[Id][b] -> IN[b] ). ||SET = (forall[i:T] (ELEMENT(i))).

19 19

slide-68
SLIDE 68

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

20 20

slide-69
SLIDE 69

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

We model bounded overtaking using tickets, where ticket numbers indicate the order in which players make their requests. The allocator records which ticket number is next.

20 20

slide-70
SLIDE 70

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

We model bounded overtaking using tickets, where ticket numbers indicate the order in which players make their requests. The allocator records which ticket number is next. Overtaking occurs when we allocate balls to a player whose turn - indicated by his/her ticket number – is subsequent to a waiting player with the next ticket. The overtaking player is added to the

  • vertaking set, and a count ot is incremented to indicate the number
  • f times next has been overtaken.

20 20

slide-71
SLIDE 71

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

We model bounded overtaking using tickets, where ticket numbers indicate the order in which players make their requests. The allocator records which ticket number is next. Overtaking occurs when we allocate balls to a player whose turn - indicated by his/her ticket number – is subsequent to a waiting player with the next ticket. The overtaking player is added to the

  • vertaking set, and a count ot is incremented to indicate the number
  • f times next has been overtaken.

When the count equals the bound, we allow allocation to the next player only. When allocation is made to the next player, we update next to indicate the next (waiting) player. We skip the ticket numbers of overtaking players who already received their allocation, remove each of these intervening players from the overtaking set and decrement the overtaking count ot accordingly. (This is achieved in the local process, WHILE, in the ALLOCATOR model.)

20 20

slide-72
SLIDE 72

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

21 21

slide-73
SLIDE 73

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] =

21 21

slide-74
SLIDE 74

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] = (when (b>0 && ot<Bd) get[i:1..b][turn:T] ->

21 21

slide-75
SLIDE 75

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] = (when (b>0 && ot<Bd) get[i:1..b][turn:T] -> if (turn!=next) then (add[turn] -> BALL[b-i][next][ot+1]) else WHILE[b-i][next%TM+1][ot]

21 21

slide-76
SLIDE 76

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] = (when (b>0 && ot<Bd) get[i:1..b][turn:T] -> if (turn!=next) then (add[turn] -> BALL[b-i][next][ot+1]) else WHILE[b-i][next%TM+1][ot] |when (b>0 && ot==Bd) get[i:1..b][next] -> WHILE[b-i][next%TM+1][ot]

21 21

slide-77
SLIDE 77

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] = (when (b>0 && ot<Bd) get[i:1..b][turn:T] -> if (turn!=next) then (add[turn] -> BALL[b-i][next][ot+1]) else WHILE[b-i][next%TM+1][ot] |when (b>0 && ot==Bd) get[i:1..b][next] -> WHILE[b-i][next%TM+1][ot] |put[j:1..N] -> BALL[b+j][next][ot] ),

21 21

slide-78
SLIDE 78

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] = (when (b>0 && ot<Bd) get[i:1..b][turn:T] -> if (turn!=next) then (add[turn] -> BALL[b-i][next][ot+1]) else WHILE[b-i][next%TM+1][ot] |when (b>0 && ot==Bd) get[i:1..b][next] -> WHILE[b-i][next%TM+1][ot] |put[j:1..N] -> BALL[b+j][next][ot] ), WHILE[b:B][next:T][ot:0..Bd] = (contains[next][yes:Bool] ->

21 21

slide-79
SLIDE 79

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

ALLOCATOR = BALL[N][1][0], //initially N balls, 1 is next, empty set BALL[b:B][next:T][ot:0..Bd] = (when (b>0 && ot<Bd) get[i:1..b][turn:T] -> if (turn!=next) then (add[turn] -> BALL[b-i][next][ot+1]) else WHILE[b-i][next%TM+1][ot] |when (b>0 && ot==Bd) get[i:1..b][next] -> WHILE[b-i][next%TM+1][ot] |put[j:1..N] -> BALL[b+j][next][ot] ), WHILE[b:B][next:T][ot:0..Bd] = (contains[next][yes:Bool] -> if (yes) then (remove[next] -> WHILE[b][next%TM+1][ot-1]) else BALL[b][next][ot] ).

21 21

slide-80
SLIDE 80

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

const N = 5 // maximum #golf balls const Bd = 2 // bound on overtaking range B = 0..N // available range const TM = N + Bd // maximum ticket range T = 1..TM // ticket values where

22 22

slide-81
SLIDE 81

DM519 Concurrent Programming

Bounded Allocation - Allocator Model

const N = 5 // maximum #golf balls const Bd = 2 // bound on overtaking range B = 0..N // available range const TM = N + Bd // maximum ticket range T = 1..TM // ticket values where ||GOLFCLUB = (Players:PLAYER || ALLOCATOR || TICKET || SET || HANDICAP )/ {Players.get/get, Players.put/put, Players.ticket/ticket}.

22 22

slide-82
SLIDE 82

DM519 Concurrent Programming

Bounded Allocation - An Explanatory Trace

eve.need.4 Experts Eve and Dave dave.need.4 chris.need.1 Novices Alice, Bob and Chris alice.need.1 bob.need.1 alice.ticket.1 alice.get.1.1 Alice gets 1 ball, ticket 1 contains.2.0 Ticket 2 is next bob.ticket.2 bob.get.1.2 Two allocated, three available contains.3.0 Ticket 3 is next dave.ticket.3 Dave needs four balls: waits chris.ticket.4 chris.get.1.4 Chris overtakes add.4 eve.ticket.5 Eve needs four balls: waits alice.put.1 alice.ticket.6 alice.get.1.6 Alice overtakes add.6 bob.put.1 bob.ticket.7 bob.get.1.7 Bob overtakes: bound reached add.7

Using animation, we can perform a scenario and produce a trace.

23 23

slide-83
SLIDE 83

DM519 Concurrent Programming

Bounded Allocation - An Explanatory Trace

chris.put.1 chris.ticket.8 Chris waits: three available alice.put.1 alice.ticket.1 Alice waits: four available dave.get.4.3 Dave gets four balls contains.4.1 remove intervening overtaker remove.4 contains.5.0 Ticket 5 (Eve) is next dave.put.4 dave.ticket.2 alice.get.1.1 Alice overtakes: bound reached add.1 bob.put.1 bob.ticket.3 eve.get.4.5 Eve gets four balls contains.6.1 remove intervening overtakers remove.6 contains.7.1 remove.7 contains.8.0 Ticket 8 (Chris) is next . . .

24 24

slide-84
SLIDE 84

DM519 Concurrent Programming

Bounded Allocation - An Explanatory Trace

chris.put.1 chris.ticket.8 Chris waits: three available alice.put.1 alice.ticket.1 Alice waits: four available dave.get.4.3 Dave gets four balls contains.4.1 remove intervening overtaker remove.4 contains.5.0 Ticket 5 (Eve) is next dave.put.4 dave.ticket.2 alice.get.1.1 Alice overtakes: bound reached add.1 bob.put.1 bob.ticket.3 eve.get.4.5 Eve gets four balls contains.6.1 remove intervening overtakers remove.6 contains.7.1 remove.7 contains.8.0 Ticket 8 (Chris) is next . . .

Exhaustive checking:

Safety? Liveness?

Can we also specify the bounded nature

  • f this allocator

as a safety property?

24 24

slide-85
SLIDE 85

DM519 Concurrent Programming

Bounded Allocation – Safety Property

For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

25 25

slide-86
SLIDE 86

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

25 25

slide-87
SLIDE 87

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-88
SLIDE 88

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = ([P].ticket[t:T] -> WAITING[t][0] |[Players].get[R][T] -> BOUND ), For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-89
SLIDE 89

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = ([P].ticket[t:T] -> WAITING[t][0] |[Players].get[R][T] -> BOUND ), WAITING[ticket:T][overtaken:0..Bd] = For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-90
SLIDE 90

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = ([P].ticket[t:T] -> WAITING[t][0] |[Players].get[R][T] -> BOUND ), WAITING[ticket:T][overtaken:0..Bd] = ([P].get[b:R][ticket] -> BOUND For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-91
SLIDE 91

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = ([P].ticket[t:T] -> WAITING[t][0] |[Players].get[R][T] -> BOUND ), WAITING[ticket:T][overtaken:0..Bd] = ([P].get[b:R][ticket] -> BOUND |{Players\{[P]}}.get[b:R][t:T] -> For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-92
SLIDE 92

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = ([P].ticket[t:T] -> WAITING[t][0] |[Players].get[R][T] -> BOUND ), WAITING[ticket:T][overtaken:0..Bd] = ([P].get[b:R][ticket] -> BOUND |{Players\{[P]}}.get[b:R][t:T] -> if (t>ticket) then WAITING[ticket][overtaken+1] else WAITING[ticket][overtaken] For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-93
SLIDE 93

DM519 Concurrent Programming

Bounded Allocation – Safety Property

property BOUND(P='alice) = ([P].ticket[t:T] -> WAITING[t][0] |[Players].get[R][T] -> BOUND ), WAITING[ticket:T][overtaken:0..Bd] = ([P].get[b:R][ticket] -> BOUND |{Players\{[P]}}.get[b:R][t:T] -> if (t>ticket) then WAITING[ticket][overtaken+1] else WAITING[ticket][overtaken] |Players.ticket[last:T] ->WAITING[ticket][overtaken] ). For each player, check that he/she is not overtaken more than bound

  • times. Overtaking is indicated by an allocation to another player whose

ticket t lies between the turn of the player and the latest ticket.

Action labels used in expressions or as parameter values must be prefixed with a single quote.

25 25

slide-94
SLIDE 94

DM519 Concurrent Programming

9.6 Bounded Overtaking Allocator - Implementation

Implementation of the BoundedOvertakingAllocator monitor follows the algorithm in the model.

26 26

slide-95
SLIDE 95

DM519 Concurrent Programming

9.6 Bounded Overtaking Allocator - Implementation

Novice player f4 has been overtaken by expert players g1, h1 and

  • i1. Since the overtaking bound of three has been exceeded, players

j1 and k1 are blocked although there are two golf balls available.

Implementation of the BoundedOvertakingAllocator monitor follows the algorithm in the model.

26 26

slide-96
SLIDE 96

DM519 Concurrent Programming

9.7 Master-Slave Program

27 27

slide-97
SLIDE 97

DM519 Concurrent Programming

9.7 Master-Slave Program

A Master thread creates a Slave thread to perform some task (eg. I/O) and continues.

27 27

slide-98
SLIDE 98

DM519 Concurrent Programming

9.7 Master-Slave Program

A Master thread creates a Slave thread to perform some task (eg. I/O) and continues. Later, the Master synchronizes with the Slave to collect the result.

27 27

slide-99
SLIDE 99

DM519 Concurrent Programming

9.7 Master-Slave Program

A Master thread creates a Slave thread to perform some task (eg. I/O) and continues. Later, the Master synchronizes with the Slave to collect the result. How can we avoid busy waiting for the Master?

27 27

slide-100
SLIDE 100

DM519 Concurrent Programming

9.7 Master-Slave Program

A Master thread creates a Slave thread to perform some task (eg. I/O) and continues. Later, the Master synchronizes with the Slave to collect the result. How can we avoid busy waiting for the Master? Java class Thread provides method join() which waits for the thread to die, i.e., by returning from run() or as a result of stop().

27 27

slide-101
SLIDE 101

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Master implements Runnable { ThreadPanel slaveDisplay; SlotCanvas resultDisplay; Master(ThreadPanel tp, SlotCanvas sc) {slaveDisplay=tp; resultDisplay=sc;} public void run() { try { String res=null; while(true) { while (!ThreadPanel.rotate());

28 28

slide-102
SLIDE 102

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Master implements Runnable { ThreadPanel slaveDisplay; SlotCanvas resultDisplay; Master(ThreadPanel tp, SlotCanvas sc) {slaveDisplay=tp; resultDisplay=sc;} public void run() { try { String res=null; while(true) { while (!ThreadPanel.rotate()); if (res!=null) resultDisplay.leave(res);

28 28

slide-103
SLIDE 103

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Master implements Runnable { ThreadPanel slaveDisplay; SlotCanvas resultDisplay; Master(ThreadPanel tp, SlotCanvas sc) {slaveDisplay=tp; resultDisplay=sc;} public void run() { try { String res=null; while(true) { while (!ThreadPanel.rotate()); if (res!=null) resultDisplay.leave(res); Slave s = new Slave(); // create new slave thread Thread st = slaveDisplay.start(s,false);

Slave thread is created and started using the ThreadPanel method start.

28 28

slide-104
SLIDE 104

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Master implements Runnable { ThreadPanel slaveDisplay; SlotCanvas resultDisplay; Master(ThreadPanel tp, SlotCanvas sc) {slaveDisplay=tp; resultDisplay=sc;} public void run() { try { String res=null; while(true) { while (!ThreadPanel.rotate()); if (res!=null) resultDisplay.leave(res); Slave s = new Slave(); // create new slave thread Thread st = slaveDisplay.start(s,false); while (ThreadPanel.rotate()); // continue execution

Slave thread is created and started using the ThreadPanel method start.

28 28

slide-105
SLIDE 105

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Master implements Runnable { ThreadPanel slaveDisplay; SlotCanvas resultDisplay; Master(ThreadPanel tp, SlotCanvas sc) {slaveDisplay=tp; resultDisplay=sc;} public void run() { try { String res=null; while(true) { while (!ThreadPanel.rotate()); if (res!=null) resultDisplay.leave(res); Slave s = new Slave(); // create new slave thread Thread st = slaveDisplay.start(s,false); while (ThreadPanel.rotate()); // continue execution st.join(); // wait for slave termination

Slave thread is created and started using the ThreadPanel method start.

28 28

slide-106
SLIDE 106

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Master implements Runnable { ThreadPanel slaveDisplay; SlotCanvas resultDisplay; Master(ThreadPanel tp, SlotCanvas sc) {slaveDisplay=tp; resultDisplay=sc;} public void run() { try { String res=null; while(true) { while (!ThreadPanel.rotate()); if (res!=null) resultDisplay.leave(res); Slave s = new Slave(); // create new slave thread Thread st = slaveDisplay.start(s,false); while (ThreadPanel.rotate()); // continue execution st.join(); // wait for slave termination res = String.valueOf(s.result()); //get and display result from slave resultDisplay.enter(res); } } catch (InterruptedException e){} } }

Slave thread is created and started using the ThreadPanel method start.

28 28

slide-107
SLIDE 107

DM519 Concurrent Programming

Java Implementation - Master-Slave

class Slave implements Runnable { int rotations = 0; public void run() { try { while (!ThreadPanel.rotate()) ++rotations; } catch (InterruptedException e){} } int result(){ return rotations; } }

Slave method result need not be synchronized to avoid interference with the Master thread. Why not?

29 29

slide-108
SLIDE 108

DM519 Concurrent Programming

9.8 Master-Slave Model

SLAVE = (start->rotate->join->SLAVE). MASTER = (slave.start->rotate

  • >slave.join->rotate->MASTER).

||MASTER_SLAVE = (MASTER || slave:SLAVE).

join is modeled by a synchronized action.

30 30

slide-109
SLIDE 109

DM519 Concurrent Programming

9.8 Master-Slave Model

SLAVE = (start->rotate->join->SLAVE). MASTER = (slave.start->rotate

  • >slave.join->rotate->MASTER).

||MASTER_SLAVE = (MASTER || slave:SLAVE).

join is modeled by a synchronized action. slave.rotate and rotate are interleaved, i.e., concurrent

30 30

slide-110
SLIDE 110

DM519 Concurrent Programming

Dynamic Systems

31 31

slide-111
SLIDE 111

DM519 Concurrent Programming

Dynamic Systems Concepts: dynamic creation and deletion of processes

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

31 31

slide-112
SLIDE 112

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

31 31

slide-113
SLIDE 113

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

31 31