Topic 5.1 · Process Synchronization & Concurrency Problems

Critical Section Problem, Race Conditions & Mutex Locks

3 min read1 question to test yourself

1Definition

The Critical Section (CS) is a segment of code in concurrent programming where threads or processes access shared resources (shared variables, memory, files) and perform read/write operations. A Race Condition occurs when two or more concurrent threads access shared data simultaneously and the final output depends unpredictably on thread execution order. A Mutex (Mutual Exclusion Lock) is a synchronization primitive used to enforce that only one thread can execute inside the critical section at a time.

2Simple Explanation

Imagine a shared bank account variable balance = $100. Thread A wants to deposit $50, and Thread B wants to withdraw $20 at the exact same millisecond.

  • Thread A reads $100.
  • Before Thread A writes $150, CPU context-switches to Thread B.
  • Thread B reads $100, subtracts $20, and writes $80 to memory.
  • CPU context-switches back to Thread A. Thread A completes its old math and writes $150 to memory!
  • The $20 withdrawal was completely lost! This bug is a Race Condition.

To prevent this, we surround the code modifying balance with a Mutex Lock. Before entering the Critical Section, a thread calls lock(). Other threads trying to enter must wait. When finished, the thread calls unlock().

3Real-Life Example / Analogy

A Single-Occupancy Public Restroom: The restroom interior is the Critical Section. The door lock is the Mutex. When Person A enters, they flip the door handle lock to RED (Acquire Mutex). If Person B tries to open the door, they see RED and must wait outside. When Person A leaves, they unlock the door to GREEN (Release Mutex), allowing Person B to enter.

4Diagram / Visual Representation

Thread 2MutexThread 1Thread 2MutexThread 1mutex_lock()Lock acquiredCritical sectionmutex_lock()Busy · wait / blockmutex_unlock()Lock acquiredCritical sectionmutex_unlock()

5Working / How It Works

  • Requirements for a Valid Critical Section Solution:
    1. Mutual Exclusion: If Process P1 is executing in its critical section, no other processes can execute in their critical sections.
    2. Progress: If no process is in its critical section and some wish to enter, only processes not executing in their remainder sections can participate in deciding who enters next.
    3. Bounded Waiting: There must be a bound on the number of times other processes are allowed to enter their critical sections after a process has made a request to enter and before that request is granted.

6Types / Components / Characteristics

  • Disadvantages of Basic Mutex Locks:
    • Contention / Busy Waiting: Threads spin continuously in a loop consuming CPU cycles waiting for a lock (Spinlocks).
    • Deadlock Potential: If Thread 1 holds Lock A and requests Lock B, while Thread 2 holds Lock B and requests Lock A, both freeze forever.
    • Priority Inversion: A lower-priority thread holding a lock delays a higher-priority thread waiting for that lock.

7Solved Examples / Problems

Problem: Can a simple global boolean flag variable bool flag = false; solve the critical section problem between two concurrent threads? Solution: No. Consider the entry code:

C
Code
while (flag == true); // Wait
flag = true;          // Acquire
/* Critical Section */
flag = false;         // Release

If Thread 1 evaluates while(flag == true) as false at the exact instant before setting flag = true, a context switch to Thread 2 can occur. Thread 2 also evaluates flag == true as false and sets flag = true. Now both threads enter the Critical Section simultaneously, violating Mutual Exclusion.

8Advantages, Disadvantages & Applications

  • Mutex Advantages: Simple binary exclusion primitive; guarantees data consistency on shared memory structures.
  • Disadvantages: Can lead to deadlocks, busy-waiting CPU waste, and priority inversion.

9Important Points to Remember

  • Peterson's solution provides a software-based critical section solution for exactly two processes.
  • Atomic CPU operations (e.g., Test-and-Set instructions executed in 1 clock cycle) are necessary hardware foundations for lock implementations.

10Test Your Understanding

  1. MCQ: What mandatory criterion of the Critical Section Problem requires that no process outside its critical section can block other requesting processes from entering?
    • A) Mutual Exclusion
    • B) Bounded Waiting
    • C) Progress
    • D) Aging
    Show answer
    C.
  2. Conceptual Question: Define a Race Condition and write a short code block illustrating how it occurs.
  3. Application Question: Compare the operational efficiency of a Spinlock vs. a Blocking Mutex on a single-core CPU.

Finished this topic?

0 of 27 topics read in Operating Systems

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic