Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Mutex locks

acquire/release and spinlocks, interactively.

Module 3 Session 3.3 Lesson 3 of 3

Operating Systems: Mutex Locks

Software Synchronization with Binary Locks & Spinlocks

1. What is a Mutex Lock?

A Mutex Lock (short for Mutual Exclusion Lock) is the simplest software tool provided by operating systems to solve the Critical-Section Problem. It acts as a binary gatekeeper to protect shared variables, memory, or devices from concurrent access.

A mutex uses a boolean variable available whose value indicates if the lock is free (true) or held (false). It defines two main atomic functions:

// Atomic implementation of acquire()
void acquire() {
    while (!available)
        ; // busy wait (spinlock)
    available = false;
}

// Atomic implementation of release()
void release() {
    available = true;
}

Key Operational Rules:

  • Strict Atomic Calls: Calls to acquire() and release() must complete atomically without hardware interrupts (implemented via atomic instructions like compare_and_swap).
  • Ownership Requirement: Only the thread/process that successfully acquired the lock is allowed to release it.
  • Binary States: The lock exists in only two states: Unlocked (1/true) or Locked (0/false).

2. Structure of Process using Mutex Lock

Every participating process Pi acquires the lock in its entry section before making modifications in its critical section, releasing it in the exit section:

do {
    acquire(lock); // Entry Section: Claim lock or wait

    /* CRITICAL SECTION (Modify shared memory / data) */

    release(lock); // Exit Section: Release lock for others

    /* REMAINDER SECTION */
} while (true);

3. Spinlocks & Trade-Off Analysis

Because the process loops continuously while waiting in acquire(), this type of mutex is known as a Spinlock.

Disadvantage: CPU Cycle Waste

Busy waiting wastes processor execution cycles that other active processes could use productively while a process waits for a lock.

Advantage: No Context Switch

No context switch is required when a process waits on a spinlock. Context switches are expensive CPU operations.

Optimal Use Case

Spinlocks are preferred on multi-core systems when locks are expected to be held for short durations.

4. Interactive Mutex Lock Simulator

Simulate 3 concurrent threads (T0, T1, T2) competing to acquire a shared Mutex Lock.

Thread T0

Remainder Section

Thread T1

Remainder Section

Thread T2

Remainder Section
Mutex Variable: available = true | Lock Held By: None