Dekker's algorithm provides mutual exclusion for two processes f0 = false //flags f1 = false 1 turn = 0 //0 for P0 and 1 for P1 2 3 P0: P1: 4 f0 = true f1 = true 5 while(f1){ while(f0){ 6 if turn != 0{ if turn !=1{ 7 f0 = false f1 = false 8 while turn != 0{ while turn != 1{ 9 } } 10 f0 = true f1 = true 11 } } 12 } } 13 14 *Critical Section* *Critical Section* 15 16 turn = 1 turn = 0 17 f0 = false f1 = false Given two processes P0 and P1 there is no way for both of them to simultaneously enter their critical sections Say P0 is in its critical section, then f0 = true and turn = 0, turn is 0 iff P1 has already completed its critical section and has executed line 16 or the algorithm has just begun execution and turn is initialized to 0. In which case P1 will become stuck at line 8 before it can execute its critical section. P1 cannot move beyond line 8 until turn is set to 1 on line 16 of P0, which is after its critical section. The same is true for P0 when P1 is in its critical section because the two processes mirror each other. To prove Peterson's algorithm for n-processes you would need two arrays array flag[0..N-1) //initialized to false array turn[0..N-1] //either 0 or 1, initialized to 1 //let i be an integer 0 <= i < N Pi: flag[i] = true loop through N processes