CIS 720 Lecture 5
Safety and liveness properties A safety property states that something bad will not happen A liveness property states that something good will eventually happen
Proving safety property BAD= predicate characterizing the bad property GOOD = not BAD Prove that GOOD is an invariant
Liveness property A statement is eligible if it is the next action that could be executed Scheduling policy determines the next statement to be executed
Weak Fairness: A scheduling policy is weak fair if x = true co while (x) skip [] x = false oc x = 0; y = 0 co while (x = 0) y = y + 1 [] await ( y > 5) x = 1 oc Unconditional fairness: A scheduling policy is unconditionally fair if every unconditional atomic action (one which does not have a guard) that is eligible is executed eventually. Weak Fairness: A scheduling policy is weak fair if It is unconditionally fair Every conditional action that is eligible is eventually executed assuming that its guard becomes true and remains true.
Strong Fairness: A scheduling policy is strong fair if It is unconditionally fair Every conditional action that is eligible is eventually executed assuming that its guard becomes true and becomes true infinitely often x = 0; y = 0 co while (x = 0) y = y + 1 [] await ( y is even) x = 1 oc
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 { in1 = false; in2 = false } 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 { I /\¬ in1} { I /\¬ in2} { I /\ in1} { I /\ in2} { I /\¬ in1} { I /\¬ in2} BAD = ¬ in1 /\ ¬ in2 I = ¬ BAD = ¬ (in1 /\ in2) = ¬ in1 \/ ¬ in2
Invariant based approach CS1 CS2 do (true) do (true) in1 = true in2 = true critical section critical section in1 = false in2 = false non-critical section non-critical section od { I /\¬ in1} <await (¬ in2) <await (¬ in1) > > { I} { I /\ ¬ in1} Weakest precondition wp(A, action) {wp(A, action) } action { A } --- given by the assignment axiom Wp( x = 5, x = x + 1) { x= 4} x = x + 1 {x = 5 }
Mutual exclusion: (¬ in1 \/ ¬ in2) /\ in1 /\ in 2 = false Absence of unnecessary wait: ¬ in2 /\ ¬ in1
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 od lock = in1 \/ in2 <await (¬ in2) in1 = true>
Test and set instruction lock = false CS1 CS2 do (true) do (true) while (TS(lock)) {}; while(TS(lock)) {}; critical section critical section lock = false lock = false non-critical section non-critical section od TS(x) = { temp = x; x = true; return temp }
Tie Breaker Algorithm in1 = false; in2 = false; last = 1 co CS1: CS2: do true do true last = 1; in1 = true; last = 2; in2 = true 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
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 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.