If a process is executing in its critical section, then no other process is allowed to execute in the critical
section. This is called Mutual exclusion.
section. This is called Mutual exclusion.
Mutual exclusion conditions:
- No two processes simultaneously in the critical region
- No assumptions made about speeds or numbers of CPUs
- No process running outside its critical region may block another process
- No process must wait forever to enter its critical region
- Critical regions with mutual exclusion:
Mutual Exclusion using test and set instruction
What the test-and-set instruction does is as follows. It returns the old value pointed to by the old ptr,
and simultaneously updates said value to new. The key, of course, is that this sequence of operations
is performed atomically. The reason it is called “test and set” is that it enables you to “test” the old
value (which is what is returned) while simultaneously “setting” the memory location to a new value;
as it turns out, this slightly more powerful instruction is enough to build a simple spin lock.
and simultaneously updates said value to new. The key, of course, is that this sequence of operations
is performed atomically. The reason it is called “test and set” is that it enables you to “test” the old
value (which is what is returned) while simultaneously “setting” the memory location to a new value;
as it turns out, this slightly more powerful instruction is enough to build a simple spin lock.
Test-and-Set Instruction:
- It is an instruction that returns the old value of a memory location and sets the memory location value to 1 as a single atomic operation.
- If one process is currently executing a test-and-set, no other process is allowed to begin anothertest-and-set until the first process test-and-set is finished.
Implementation:
Initially, lock value is set to 0.
- Lock value = 0 means the critical section is currently vacant and no process is present inside it.
- Lock value = 1 means the critical section is currently occupied and a process is present inside it.
Working:
Step 1:
|
|
Step 2:
|
|
Step 3:
|
|
Comments
Post a Comment