CIS 720 Mutual Exclusion 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
Barrier synchronization Worker[i]: do true code for task i wait for all tasks to complete od
Barrier synchronization Worker[i]: do true code for task i <count = count + 1> < await( count == n) > od
Barrier synchronization Worker[i]: do true code for task i <count = count + 1> < await( count == n) > od
Barrier synchronization co worker[i]: Coordinator do true do true code for task i for (i = 1 to n) arrive[i] = 1 await(arrive[i]= 1); await(continue ==1) continue = 1 od od oc
Barrier synchronization co worker[i]: Coordinator do true do true code for task I; for (i = 1 to n) arrive[i] = 1 { 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.
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.
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
Mutual exclusion CS1 CS2 Associate in and out counter with each region do (true) do (true) ****start of region **** start of region in1 = in1 + 1 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
Mutual exclusion CS1 CS2 Associate in and out counter with each region do (true) do (true) ****start of region **** start of region in1 = in1 + 1 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)
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
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
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 = arrive1 + 1 arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Associate counters with each region
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 = arrive1 + 1 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])
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
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
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 )
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 )
Barrier CS1 CS2 do (true) do (true) task for p1 task for p2 ****start of region **** start of region arrive1 = arrive1 + 1 arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Barrier1 = (arrive1 – depart2) Barrier2 = (arrive2 – depart1)
Barrier CS1 CS2 Barrier1 = (arrive1 – depart2) do (true) do (true) task for p1 task for p2 barrier1++ barrier2++ barrier2--; barrier1--; od Barrier1 = (arrive1 – depart2) Barrier2 = (arrive2 – depart1)