Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 720 Mutual Exclusion 2.

Similar presentations


Presentation on theme: "CIS 720 Mutual Exclusion 2."— Presentation transcript:

1 CIS 720 Mutual Exclusion 2

2 Tie Breaker Algorithm in1 = false; in2 = false; last = 1 co CS1: CS2:
do true  do true  last = 1; in1 = true; last = 2; in2 = true <await (!in2 \/ last == 2)>; <await (!in1 \/ last == 1)>; critical section critical section in1 = false; in2 = false; non-critical section non-critical section od od oc

3 Barrier synchronization
Worker[i]: do true  code for task i wait for all tasks to complete od

4 Barrier synchronization
Worker[i]: do true  code for task i <count = count + 1> < await( count == n) > od

5 Barrier synchronization
Worker[i]: do true  code for task i <count = count + 1> < await( count == n) > od

6 Barrier synchronization
co worker[i]: Coordinator do true  do true  code for task i for (i = 1 to n) arrive[i] = await(arrive[i]= 1); await(continue ==1) continue = 1 od od oc

7 Barrier synchronization
co worker[i]: Coordinator do true  do true  code for task I; for (i = 1 to n) arrive[i] = { await(arrive[i]= 1); await(continue[i]==1) arrive[i] = 0; } continue[i] = 0; for (i = 1 to n) continue[i] = 1 od od oc Flag rule: A process that waits for the synchronization flags should reset it.

8 Invariant based approach
Identify synchronization regions in your program Synchronization region: segment of code or control point at which a thread must wait for another thread or signal another thread.

9 Mutual exclusion CS1 CS2 do (true) do (true) 
****start of region **** start of region critical section critical section **** end of region **** end of region non-critical section non-critical section od

10 Mutual exclusion CS1 CS2 Associate in and out counter with each region
do (true) do (true)  ****start of region **** start of region in1 = in in2 = in2 + 1 critical section critical section **** end of region **** end of region out1 = out1+1; out2 = out2+1; non-critical section non-critical section od Associate in and out counter with each region

11 Mutual exclusion CS1 CS2 Associate in and out counter with each region
do (true) do (true)  ****start of region **** start of region in1 = in in2 = in2 + 1 critical section critical section **** end of region **** end of region out1 = out1+1; out2 = out2+1; non-critical section non-critical section od Associate in and out counter with each region Write an invariant using in and out counters I = (in1=out1) \/ (in2=out2)

12 Mutual exclusion CS1 CS2 do (true) do (true) 
****start of region **** start of region <await(in2=out2) in1 = in1 + 1> <await(in1=out1) in2 = in2 + 1> critical section critical section **** end of region **** end of region out1 = out1+1; out2 = out2+1; non-critical section non-critical section od Associate in and out counter with each region Write an invariant using in and out counters I = (in1=out1) \/ (in2=out2) Derive guards for the increment assertions

13 Barrier Synchronization
P1 P2 do (true) do (true)  task for p1 task for p2 ****start of region **** start of region **** end of region **** end of region od

14 Barrier CS1 CS2 Associate counters with each region
do (true) do (true)  task for p1 task for p2 ****start of region **** start of region arrive1 = arrive arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Associate counters with each region

15 Barrier CS1 CS2 Associate counters with each region
do (true) do (true)  task for p1 task for p2 ****start of region **** start of region arrive1 = arrive arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Associate counters with each region Write an invariant using the counters (depart[1] <= arrive[2]) /\ (depart[2] <= arrive[1])

16 Semaphores Two operations: P(s), V(s) Semaphore invariant:
nP = number of P operations invoked so far nV = number of V operations invoked so far init = initial value of s nP <= nV + init

17 Binary semaphore: 0 <= s <= 1
Let s = nV + init – nP Invariant: s >= 0 P(s): nP = nP + 1 <await s > 0  s = s - 1> V(s): nV = nV + 1 < s = s + 1> Binary semaphore: 0 <= s <= 1

18 Mutual exclusion CSi do (true)
in[i] = 1 critical section in[i] = 0 non-critical section od I = ( 0 <= (in[1] + in[2] + …. + in[n]) <= 1 ) Let mutex = 1 - (in[1] + in[2] + …. + in[n]) I = ( 0 <= mutex <= 1 )

19 Mutual exclusion CSi do (true)
critical section non-critical section od I = ( 0 <= (in[1] + in[2] + …. + in[n]) <= 1 ) Let mutex = 1 - (in[1] + in[2] + …. + in[n]) I = ( 0 <= mutex <= 1 )

20 Barrier CS1 CS2 do (true) do (true)  task for p1 task for p2
****start of region **** start of region arrive1 = arrive arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Barrier1 = (arrive1 – depart2) Barrier2 = (arrive2 – depart1)

21 Barrier CS1 CS2 Barrier1 = (arrive1 – depart2)
do (true) do (true)  task for p1 task for p2 barrier barrier2++ barrier2--; barrier1--; od Barrier1 = (arrive1 – depart2) Barrier2 = (arrive2 – depart1)


Download ppt "CIS 720 Mutual Exclusion 2."

Similar presentations


Ads by Google