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:

  1. Test-and-Set

  2. 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 lock is initialized to false

  • 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 lock from false to true

  • 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:

  1. A memory location

  2. An expected value

  3. 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 lock is initialized to 0

  • A process tries to change lock from 0 to 1

  • The first process succeeds and enters the critical section

  • Others fail and continue waiting

  • When the process exits, it resets lock to 0

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:

  1. Mutual exclusion

  2. Progress

  3. Bounded waiting


Hardware Support for Atomicity

How CAS Is Made Atomic in Practice

  • On Intel x86 systems, CAS is implemented using the cmpxchg instruction

  • The lock prefix 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

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Differences Between Linux and Classic UNIX Kernels