2.13.2 Mutual exclusion: the hardware approach
Interrupt disable (mask interrupt)
- For single CPU system
- Using the method of shielding interrupt, the process cannot be switched during shielding, achieving the purpose of mutual exclusion of processes
while (true ) {
disable interrupt //屏蔽中断
critical section //临界区
enable interrupt //启用中断
remainder //其余部分
}
Evaluation
- Obviously low efficiency
- Unable to respond to external requests during blocking
- Unable to switch process
- Cannot work in a multi-processor environment
Special machine instructions
- The two machine instructions specified by the processor designer are executed in one instruction cycle and will not be interrupted or interfered by other instructions
- Can also be used in a multi-processor environment
Compare the value of a memory cell with the test value, exchange if they are equal
int compare_and_swap(int *word, int testval, int newval){
int oldval;
oldval = *word;
if(oldval == testval) *word = newval;
return oldval;
}
Atomic exchange of register and memory values
procedure exchange(var r: register; var m: memory);
var temp;
begin
temp := m;
m := r;
r := temp;
end
Evaluation
advantage
- Supports multiple processors
- Simple and easy to prove
- Support multiple critical sections
Disadvantage
- Busy waiting
- Hunger may appear
- Possible deadlock