Algorithm 1 public class Algorithm_1 extends MutualExclusion public Algorithm 10 t turn E turn O public void entering CriticalSection(int t)t while (turn l= t) Thread yield public void leav ing CriticalSection(int ti urn=1-t private volatile int turn; Applied Operating System Concepts 7.11 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.11 Applied Operating System Concepts Algorithm 1 public class Algorithm_1 extends MutualExclusion { public Algorithm_1() { turn = TURN_0; } public void enteringCriticalSection(int t) { while (turn != t) Thread.yield(); } public void leavingCriticalSection(int t) { turn = 1 - t; } private volatile int turn; }
Algorithm 2 public class Algorithm_ 2 extends MutualExclusion i public Algorithm 20 flag[]=false; flag[1]= false public void entering CriticalSection(int t)t ∥ see next slide public void leav ing CriticalSection(int ti flag[t]= false; private volatile boolean flag new boolean [2] Applied Operating System Concepts 7.12 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.12 Applied Operating System Concepts Algorithm 2 public class Algorithm_2 extends MutualExclusion { public Algorithm_2() { flag[0] = false; flag[1] = false; } public void enteringCriticalSection(int t) { // see next slide } public void leavingCriticalSection(int t) { flag[t] = false; } private volatile boolean[] flag = new boolean[2]; }
Algorithm 2-entering Critical Sectiono public void entering Critical Section(int t)t int other = 1-t flag[t]= true; while(flaglother]== true) HH Thread yield: Applied Operating System Concepts 7.13 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.13 Applied Operating System Concepts Algorithm 2 – enteringCriticalSection() public void enteringCriticalSection(int t) { int other = 1 - t; flag[t] = true; while (flag[other] == true) Thread.yield(); }
Algorithm 3 blic class Algorithm 3 extends MutualExclusion public Algorithm 30 flag[o]= false flag[1]=false; turn TURN 0; public void entering CriticalSection(int t)I* see next slides */1 public void leav ing CriticalSection (int tI see next slides */y private volatile int turn; private volatile boolean flag new boolean[2] Applied Operating System Concepts 7 14 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.14 Applied Operating System Concepts Algorithm 3 public class Algorithm_3 extends MutualExclusion { public Algorithm_3() { flag[0] = false; flag[1] = false; turn = TURN_0; } public void enteringCriticalSection(int t) {/* see next slides */ } public void leavingCriticalSection(int t) {{/* see next slides */ } private volatile int turn; private volatile boolean[] flag = new boolean[2]; }
Algorithm 3-entering Critical Sectiono public void entering Critical Section(int t)t int other = 1-t flag[t]= true; turn other while((flaglother]== true)&&(turn = other)) Thread yield Applied Operating System Concepts 7.15 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.15 Applied Operating System Concepts Algorithm 3 – enteringCriticalSection() public void enteringCriticalSection(int t) { int other = 1 - t; flag[t] = true; turn = other; while ( (flag[other] == true) && (turn == other) ) Thread.yield(); }