170 likes | 326 Views
CIS 720. Mutual Exclusion. Critical Section problem. Process i do (true) entry protocol; critical section; exit protocol; non-critical section od. Correctness. Mutual exclusion: at most one process at a time is executing its critical section
E N D
CIS 720 Mutual Exclusion
Critical Section problem • Process i do (true) entry protocol; critical section; exit protocol; non-critical section od
Correctness • Mutual exclusion: at most one process at a time is executing its critical section • Absence of deadlock: If two or more processes are trying to enter their critical section, at least one will succeed • Absence of unnecessary delay: If a process is trying to enter its critical section and the other processes are executing their non-critical sections or have terminated then the first process is not prevented from entering its critical section. • Eventual entry: A process that is attempting to enter its critical section will eventually succeed.
Invariant based approach CS1 CS2 do (true) do (true) entry protocol; entry protocol; in1 = true in2 = true critical section critical section exit protocol; exit protocol; in1 = false in2 = false non-critical section non-critical section od
Invariant based approach CS1 CS2 do (true) do (true) <await (!lock) lock = true> <await(!lock) lock = true> critical section critical section lock = false lock = false non-critical section non-critical section od
Test and set instruction CS1 CS2 do (true) do (true) while (TS(lock)) skip; while(TS(lock)); critical section critical section lock = false lock = false non-critical section non-critical section od
Implementing await statements • CSenter while (!B) { CSexit; CSenter } S; CSexit
Tie Breaker Algorithm in1 = false; in2 = false; last = 1 co CS1: CS2: do true do true int = true; last = 1; in2 = true; last = 2; while(in2 /\ last == 1); while(in1 /\ last == 2); critical section critical section in1 = false; in2 = false; non-critical section non-critical section od od oc
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
Ticket Algorithm next = 1; number = 1; turn[1..n]= [0…0]; co CSi: do true < turn[i] = number; number = number + 1 > await( turn[i] ) == next critical section <next = next + 1> non-critical section od oc Invariant: next > 0 and for all i, cs[i] in CS turn[i] == next /\ turn[i] != turn[j] for all j
Bakery algorithm turn1 = 0; turn2 = 0; co CS1: CS2: do true do true turn1 = turn2 + 1; turn2 = turn1 + 1 while(turn2 != 0 /\ turn1 > turn2); while(turn1 != 0 and /\ turn2 > turn1); critical section critical section turn1 = 0; turn2 = 0; non-critical section non-critical section od od oc
Bakery algorithm turn1 = 0; turn2 = 0; co CS1: CS2: do true do true turn = 1; turn2 = 1 turn1 = turn2 + 1; turn2 = turn1 + 1 while(turn2 != 0 /\ while(turn1 != 0 and /\ (turn1,1) > (turn2,2)); (turn2,2) > (turn1,1)); critical section critical section turn1 = 0; turn2 = 0; 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.