Hardware Based Solutions in Process Synchronization (original) (raw)

Last Updated : 15 Apr, 2026

Hardware-based solutions to the critical section problem use special instructions like Test-and-Set and Swap. These instructions help manage access to shared resources by allowing only one process to enter the critical section at a time. They are fast and efficient, making them ideal for systems with advanced hardware support.

Various hardware solutions to the Critical Section Problem are:

1. Test and Set

TAS is an atomic instruction that reads a variable’s old value and sets it to true in a single indivisible step.

boolean lock = false; // Shared lock variable

boolean TestAndSet(boolean &target) {
boolean rv = target; // Step 1: Read old value
target = true; // Step 2: Set lock (mark busy)
return rv; // Step 3: Return old value
}
while (1) {

while (TestAndSet(lock)); // Entry Section → Busy wait until lock is free
// ---- Critical Section ----
lock = false; // Exit Section → Release lock
// ---- Remainder Section ----
}

Explanation of Pseudocode:

**Mutual Exclusion

**Critical Section Protection

**Exit Section

After finishing, the process releases the lock by setting lock = false.

2. Swap (and CAS Enhancement)

The Swap instruction is a hardware-based synchronization mechanism similar to Test-and-Set. Instead of directly setting the lock, it swaps the values of a shared lock variable and a local key variable. This ensures mutual exclusion because only one process can set its key to false and enter the critical section, while others keep spinning.

boolean lock = false; // Shared variable

boolean key; // Local per-process variable

void swap(boolean &a, boolean &b) {
boolean temp = a;
a = b;
b = temp;
}

while (1) {
key = true; // Process wants to enter
while (key) // Entry Section
swap(lock, key); // Keep swapping until lock becomes true & key false
// ---- Critical Section ----
lock = false; // Exit Section → Release lock
}

Explanation of Pseudocode

**1. Initialization

**2. Entry Section

A process sets key = true (wants to enter).
It then calls swap(lock, key):

**3. Critical Section: Only the process that successfully swapped and got key = false enters.
4. Exit Section: When the process leaves, it sets lock = false, allowing other waiting processes to try again.
5. **Remainder Section: The process executes other code before attempting entry again.

The Swap instruction ensures mutual exclusion and progress, but like Test-and-Set, it suffers from busy waiting and no bounded waiting guarantee.

**Compare-and-Swap (CAS) Enhancement

Modern CPUs often use CAS (Compare-and-Swap), which improves efficiency compared to Swap. CAS atomically compares a variable to an expected value and updates it only if they match.

int lock = 0; // 0 = free, 1 = busy

boolean CompareAndSwap(int &target, int expected, int new_val) {
int old = target;
if (target == expected)
target = new_val;
return old == expected; // true if swap succeeded
}

while (1) {
while (!CompareAndSwap(lock, 0, 1)); // Try to acquire lock

// ---- Critical Section ----

lock = 0; // Exit Section → Release lock
}

**Key Points

**3. Spinlock

A Spinlock is a higher-level abstraction built using Test-and-Set or CAS.

Example with Test-and-Set:

int lock = 0; // 0 = free, 1 = busy

void acquire() {
while (TestAndSet(lock)); // Busy wait
}

void release() {
lock = 0;
}

while (1) {
acquire();

// ---- Critical Section ----

release();

// ---- Remainder Section ----
}

**Pros:

**Cons:

Comparison of Hardware-Based Solutions

Method Mechanism Pros Cons
**Test-and-Set Atomically sets lock variable Simple, widely supported Busy waiting, starvation possible
**Swap Swap shared & local variables Ensures mutual exclusion Busy waiting, less efficient than CAS
**CAS (enhanced) Compare-and-swap atomic update Efficient, lock-free possible Still spins under contention
**Spinlock Lock abstraction (built on TAS/CAS) Very fast for short waits CPU wasting, not fair