Hardware Instructions for Synchronization
Hardware Instructions for Synchronization
Why Hardware Instructions Are Needed
Software-only solutions to the critical-section problem cannot be relied upon on modern processors. This is because:
-
Instructions may be reordered
-
Multiple cores may access memory simultaneously
To solve this, modern systems provide special hardware instructions that execute atomically, meaning:
They complete as a single, indivisible operation and cannot be interrupted.
These instructions form the foundation of synchronization mechanisms in operating systems.
Atomic Operations
An atomic operation ensures that:
-
No other process can observe a partial update
-
Concurrent executions are serialized in some order
Two commonly used atomic instructions are:
-
Test-and-Set
-
Compare-and-Swap (CAS)
Test-and-Set Instruction
Concept
The test-and-set() instruction:
-
Reads the value of a variable
-
Sets it to
true -
Returns the old value
-
All in one atomic step
How It Works
-
A shared boolean variable
lockis initialized tofalse -
A process repeatedly executes test-and-set on
lock -
If the old value was
false, the process enters the critical section -
If it was
true, the process keeps waiting (busy waiting)
Why It Works
-
Only one process at a time can change
lockfromfalsetotrue -
This guarantees mutual exclusion
Limitation
-
It causes busy waiting
-
It does not guarantee bounded waiting
-
A process may starve indefinitely
Compare-and-Swap (CAS) Instruction
Concept
The compare-and-swap() instruction operates on three values:
-
A memory location
-
An expected value
-
A new value
It performs the following atomically:
-
Compares the memory value with the expected value
-
Updates the memory only if they match
-
Returns the original value
Mutual Exclusion Using CAS
How It Works
-
A shared integer variable
lockis initialized to0 -
A process tries to change
lockfrom0to1 -
The first process succeeds and enters the critical section
-
Others fail and continue waiting
-
When the process exits, it resets
lockto0
Why It Works
-
CAS ensures only one process can acquire the lock
-
Atomic execution prevents race conditions
Bounded Waiting with CAS
Problem
Simple CAS-based locking:
-
Guarantees mutual exclusion
-
But does not guarantee bounded waiting
Solution
-
Use an additional array
waiting[n] -
Each process announces its intention to enter the critical section
-
When a process exits, it explicitly hands off the lock to the next waiting process in a cyclic order
Result
This enhanced CAS-based algorithm satisfies all three critical-section requirements:
-
Mutual exclusion
-
Progress
-
Bounded waiting
Hardware Support for Atomicity
How CAS Is Made Atomic in Practice
-
On Intel x86 systems, CAS is implemented using the
cmpxchginstruction -
The
lockprefix ensures:-
Exclusive access to the memory bus
-
Atomic execution across multiple processors
-
Key Takeaways
| Feature | Test-and-Set | Compare-and-Swap |
|---|---|---|
| Atomic | Yes | Yes |
| Mutual Exclusion | Yes | Yes |
| Bounded Waiting | No | Yes (with extra logic) |
| Busy Waiting | Yes | Yes |
| Hardware Support | Required | Required |
Final Summary
Hardware synchronization instructions provide:
-
Atomicity, which software alone cannot guarantee
-
A reliable way to implement mutual exclusion
-
The building blocks for higher-level synchronization tools like mutexes and semaphores
Without hardware instructions such as test-and-set and compare-and-swap, modern operating systems cannot safely support concurrency.




Comments
Post a Comment