Operating System-solutions for mutual exclusion between processes-ensure that only one process enters the critical section at a time (1)
Summary and blocking interrupt lock variable strict rotation method Peterson solution TSL Instruction 1. Blocking interrupt 1.1 Introduction in a single CPU system, mutual exclusion between processes can be achieved by shielding the interrupt: when a process enters the critical area, all system interruptions will be blocked immediately, and the interruption will be resumed after it leaves the critical area. When all the blocking operations are blocked, including clock interruption, the CPU will be blocked only when the clock interruption or other interruptions occur. SO when the interruption is blocked, the CPU cannot run other processes. Of course, no other processes will enter the critical area. This is a simple and crude method to achieve the goal. And can only be completed through hardware instructions. 1.2 disadvantages what if the user process has the right to block interruptions? Of course, the result is that the system is down. If the system is multi-core, blocking interruption only affects the CPU that blocks the interruption. Other CPUs can still run other processes, this process may enter the critical area at the same time as other CPU processes. 1.3 advantage: simple kernel can be implemented by blocking interruptions (but this mechanism is still used less and less ).) II. The idea of locking variable 2.1 to describe locking variables is very simple. Define a variable (N) for a critical region ). When a process first determines whether the N is 0 before entering the critical section, if it is 0, it may enter. before entering the critical section, set N to 1 (so that other processes cannot enter the critical section ), when leaving the critical section, set N to 0 (so that other processes can enter the critical section ). As shown in. 2.2 disadvantages although this solution is relatively simple, it has a fatal drawback. The Spooler example in the previous article is a counterexample. Assume that Process A first enters the critical area, and then changes N to 1, the CPU starts A switchover, Process A stops running, and ProcessB starts running. At this time, N is still 0, therefore, Process B can enter this critical zone, so that Process A and Process B enter the critical zone at the same time, thus losing the mutex between processes for the critical zone. Therefore, this solution is just an attempt and cannot fundamentally solve the problem. Iii. strict rotation method 3.1 The strict rotation method also sets a variable for a critical section, which is assumed to be "Turn. Take two processes as an example: When the Turn is 0, Process 0 can enter the critical section; otherwise, wait. After Process 0 leaves the critical section, set the Turn to 1. When the Turn is 1, Process 1 can enter the critical section; otherwise, wait. After Process 1 leaves the critical section, set "Turn" to "0. 3.2" to indicate "turn! = 0 always wait for turn = 0 when entering the critical area left the critical area after the turn is set to 1 while (true) {while (turn! = 0); // loop critical_region (); turn = 1; noncritical_region () ;}when turning! = 1 always waiting for turn = 1 when entering the critical area left the critical area after the turn is set to 0 while (true) {while (turn! = 1); // loop critical_region (); turn = 0; noncritical_region ();} 3.3 disadvantages when the condition is not met, the process enters the state of continuous loop, CPU is in busy waiting (Spin Lock), which seriously wastes CPU resources