Operating System-process/thread internal communication-Critical Zone (Critical Regions), system-regions
The previous article described the competition conditions between processes: multiple processes enter a shared area at the same time, resulting in data inconsistency. This article mainly describes how to solve this problem.
I. Introduction to critical section
To solve this problem, multiple processes are prevented from entering the shared area at the same time. In other words, processes need to Exclusion the shared area ). The root cause of the problem is that process B enters the shared area before process A returns from the shared area.
In most cases, variables are not shared between two processes. Shared variables are only used in some steps. In program design, you can design the shared variable access program separately, this part of the program is called the Critical Regions ). If the two processes always have only one process entering this critical area, the conditional competition between processes will be solved.
The following conditions are required to achieve the above results:
- No two processes enter the critical zone at the same time
- No requirement on CPU speed or quantity
- Processes that are waiting outside the critical area Block other processes. For example, if process A is in the critical area, process B accesses the critical area, process B will be blocked outside the critical section. At this time, process B should not Block other processes.
- No process will be permanently blocked out of the critical section.
Ii. critical section demonstration
Illustration
- Process A enters the critical area at T1.
- T2 time, Process B tries to enter the critical area
- Because A is in the critical area, B is Block
- T3 time A leaves the critical area, and B enters the critical area time
- T4 time, B leaves the critical area
What are the benefits of using the concept of critical area?
- If process A and process B do not use shared resources for most of the time, process A and process B can be executed in parallel during this time.
- Only access to critical areas can be parallel.
- Allows processes to work in parallel and efficiently use shared data.
Iii. Summary
- Abstract Critical Area
- Exclusive to critical areas
- How can song achieve exclusive? The next article will introduce you.