Critical Section Problem

The critical section problem occurs when multiple processes access shared resources concurrently. A critical section is a code segment where shared variables can be accessed. To maintain data consistency, only one process can execute in its critical section at a time ? this requires an atomic operation. All other processes must wait to execute in their critical sections.

Critical Section Structure Process 1 Entry Section Critical Section Exit Section Remainder Section Process 2 Entry Section Critical Section (Waiting) Exit Section Remainder Section Process N Entry Section Critical Section (Waiting) Exit Section Remainder Section Only one at a time Only Process 1 can access critical section; others must wait

Each process has four sections:

  • Entry Section − Code that requests permission to enter the critical section

  • Critical Section − Code that accesses shared resources

  • Exit Section − Code that signals completion and releases the critical section

  • Remainder Section − Non-critical code that doesn't access shared resources

Solution Requirements

Any solution to the critical section problem must satisfy three essential conditions:

Condition Description Purpose
Mutual Exclusion Only one process can be in its critical section at any time Prevents data corruption from concurrent access
Progress If no process is in critical section, one of the waiting processes must be allowed to enter Prevents unnecessary blocking and deadlock
Bounded Waiting Each process must have a finite waiting time before entering its critical section Ensures fairness and prevents starvation

Common Solutions

Several approaches can solve the critical section problem:

  • Hardware Solutions − Test-and-set, compare-and-swap instructions

  • Software Solutions − Peterson's algorithm, Bakery algorithm

  • Operating System Primitives − Semaphores, mutexes, monitors

Example Scenario

Consider two processes trying to update a shared bank account balance:

Process A:                    Process B:
read balance (500)           read balance (500)
balance = balance + 100      balance = balance - 200
write balance (600)          write balance (300)

Without proper synchronization, the final balance could be 600, 300, or 400 depending on execution order. Critical section synchronization ensures the operations are atomic and produce the correct result (400).

Conclusion

The critical section problem is fundamental to concurrent programming, requiring careful synchronization to maintain data consistency. Solutions must guarantee mutual exclusion while ensuring progress and bounded waiting to create efficient, fair, and deadlock-free systems.

Updated on: 2026-03-17T09:01:38+05:30

105K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements