Download presentation
Presentation is loading. Please wait.
1
CIS 720 Lecture 6
2
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.
3
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
4
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
5
Mutual exclusion CS1 CS2 in1 = 0; out1 = 0 in2 = 0; out2 = 0
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 od Associate in and out counter with each region Write an invariant using in and out counters I : (in1=out1) \/ (in2=out2) { in2 = 0 /\ out2 = 0 } { in2 = out2 } { in2 = out2 + 1} { I } { in1 = 0 /\ out1 = 0 } { in1 = out1 } { I } { in1 = out1 + 1} { I } { in1 = out1 } { I } { in1 + 1= out1 \/ in2 = out2 } in1 = in1 + 1 { in1 = out1 \/ in2 = out2 }
6
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
7
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
8
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
9
Barrier CS1 CS2 arrive1 = 0; depart1 = 0 arrive2 = 0; depart2 = 0
do (true) do (true) task for p1 task for p2 <await depart2 < arrive1 <await depart1 < arrive2 arrive1 = arrive1 + 1 > arrive2 = arrive2 + 1 > <await depart1 < arrive2 <await depart2 < arrive1 depart1 = depart1+1; > depart2 = depart2+1 > od od Associate counters with each region Write an invariant using the counters (depart1 <= arrive2 /\ (depart2<= arrive1) { depart1 <= arrive2 /\ depart2 <= arrive1 + 1} arrive1 = arrive1 + 1 { depart1 <= arrive2 /\ depart2 <= arrive1 }
10
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 nV + init – np >= 0 S = nv + init – np S >= 0
11
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
12
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 )
13
Mutual exclusion CSi do (true) in[i] = 1 mutex++ P(mutex)
critical section in[i] = mutex V(mutex) 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 )
14
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)
15
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)
16
Producer/consumer problem
do true do true produce pdata buf = pdata cdata = buf; consume cdata od od oc
17
Producer/consumer problem
in1 = out1 = in2 = out2 = 0; co Producer: Consumer: do true {in1=out1} do true {in2=out2} produce pdata in1 = in in2 = in2 + 1 buf = pdata {in1 = out1+1} cdata = buf {in2 = out2+1} out1 = out out2 = out2 + 1 {in1=out1} consume cdata {in2 = out2} od od oc Invariant: in2 <= out1 /\ in1 <= out2 + 1
18
Producer/consumer problem
do true do true produce pdata <await(in1 <=out2) in1 = in1 + 1> <await(in2 < out1) in2 = in2 + 1> buf = pdata cdata = buf <out1 = out1 + 1> <out2 = out2 + 1> consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) Empty = out2 + 1 – in1 Full = out1 – in2
19
Producer/consumer problem
do true do true produce pdata <await(empty > 0) empty= empty -1> <await(full > 0) full = full -1 > buf = pdata cdata = buf <full = full + 1> <empty = empty + 1> consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) empty = out2 + 1 in1 full = out1 – in2
20
Producer/consumer problem
do true do true produce pdata P(empty) P(full) buf = pdata cdata = buf V(full) V(empty) consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) empty = out2 + 1 in1 full = out1 – in2
21
Producer/consumer problem
in1 = out1 = in2 = out2 = 0; co Producer: Consumer1: Consumer2: do true do true do true produce pdata in1 = in in2 = in in3 = in3 + 1 buf = pdata cdata = buf cdata = buf out1 = out out2 = out out3 = out3 + 1 consume cdata consume cdata od od od oc Invariant:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.