- Published on
COMP2310 - Week 4 - Solutions to Critical Section Problem
Solutions
Dekker's Algorithm
- Task Q is identical with swapped variables
-> Allows right to insist on entering
Dekker's
task body P isbegin loop -- Non-critical section P
Want_P := True;
loop exit when Want_Q = False; if Turn = 2 then Want_P := False; await Turn = 1; Want_P := True; end loop;
-- Critical section P
Turn := 2; Want_P := False; end loop;end P;Satisfies
- Mutual exclusion
- No deadlock
- No starvation
Drawbacks
- Only works for two tasks
Bakery Algorithm
-
Solves for critical regions
-
competing to access critical regions. Each process supplies a globally readable number initialised to 0.
-
ALl processes are equal and must coordinate, i.e. no controlling process
-
Before a process enters a critical section:
- draws a new number
- Take first number , it reads all other tickets, find max ticket value and adds one.
- Check if is allowed to enter the critical section, iff:
- After a process leaves a critical section, the ticket number is reset, -> 0 (Set to 0)
- draws a new number
Realistic Hardware Support
Atomic test-and-set operations
[L := C; C:= 1]Atomic exchange operations
[Temp := L; L := C; C := Temp]Memory cell reservations
-> Read by using special instruction that puts reservation on
then, calcualte new value for
for -> Succeeds iff was not manipulated by processors or devices since the reservation