Algo. 3-leavinging CriticalSectiono public void leaving Critical Section(int t)( flag[t]= false; Applied Operating System Concepts 7.16 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.16 Applied Operating System Concepts Algo. 3 – leavingingCriticalSection() public void leavingCriticalSection(int t) { flag[t] = false; }
Synchronization Hardware public class Hardware Data t public Hardware Data(boolean v)i data=V: public boolean geto t return data; public void set(boolean v)i dataS: private boolean data; Applied Operating System Concepts 7.17 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.17 Applied Operating System Concepts Synchronization Hardware public class HardwareData { public HardwareData(boolean v) { data = v; } public boolean get() { return data; } public void set(boolean v) { data = v; } private boolean data; }
Test-and-Set Instruction (in Java) public class Hardware Solution public static boolean testAnd Set(HardwareData target)I HardwareData temp new Hardware Data(target geto) target set(true); return temp. get( Applied Operating System Concepts 7.18 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.18 Applied Operating System Concepts Test-and-Set Instruction (in Java) public class HardwareSolution { public static boolean testAndSet(HardwareData target) { HardwareData temp = new HardwareData(target.get()); target.set(true); return temp.get(); } }
Thread using Test-and-set Hardware Data lock new HardwareData(false) while (true)i while(Hardware Solution testAnd Set(lock)) ai Thread yield; / do nothing // now in critical section code lock set(false) l/ out of critical section Applied Operating System Concepts 7.19 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.19 Applied Operating System Concepts Thread using Test-and-Set HardwareData lock = new HardwareData(false); while (true) { while (HardwareSolution.testAndSet(lock)) Thread.yield(); // do nothing // now in critical section code lock.set(false); // out of critical section }
Swap instruction public static void swap(Hardware Data a, HardwareData b)i Hardware Data temp= new Hardware Data (ageto) aset(b get() b set(temp. geto) Applied Operating System Concepts Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.20 Applied Operating System Concepts Swap instruction public static void swap(HardwareData a, HardwareData b) { HardwareData temp = new HardwareData(a.get()); a.set(b.get()); b.set(temp.get()); }