Operating System-solutions for mutual exclusion between processes-ensure that only one process enters the critical section at a time (1), operatingsystem-
In the previous article, we proposed the competition conditions for processes. In the previous article, we proposed the concept of a critical section. By allowing only one process to enter the critical section at the same time, we can avoid the competition conditions between processes, this article introduces how to allow only one process to enter the critical section at a time.
Summary
I. Introduction to blocked interrupt 1.1
In a single CPU system, interruption can be blocked to achieve mutual exclusion between processes: when a process enters the critical zone, all the system interruptions will be blocked immediately, and the interruption will be enabled after it leaves the critical zone.
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 the interruption? Of course, the result is that the system is down.
- If the system is multi-core, blocking interruption only affects the CPU that executes blocking interruption. Other CPUs can run other processes. This process may enter the critical area at the same time as other CPU processes.
1.3 advantages
- Easy to implement
- The kernel can implement some interrupt operations by blocking the interruption (but this mechanism is still used less and less)
Ii. Description of lock variable 2.1
The idea of 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. Introduction to the 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 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 the Turn to 0.
3.2 algorithm Representation
- When turn! = 0 always waiting
- When turn = 0, enter the critical area
- Turn to 1 after leaving the critical area
while(true){ while(turn != 0); //loop critical_region(); turn = 1; noncritical_region();}
- When turn! = 1 always waiting
- When turn = 1, enter the critical area
- Turn to 0 after leaving the critical area
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 continuous loop state, and the CPU is in the busy waiting (Spin Lock), which seriously wastes CPU resources.
The next article introduces the improved Peterson solution and The TSL instruction method.