0
Explore
0

Critical Section Problem in Operating System

Updated on July 21, 2025

In the world of operating systems, concurrency plays a vital role. Multiple processes may run simultaneously, trying to access shared resources like variables, files, or memory. But what happens when two or more processes try to access the same resource at the same time?

That’s where the Critical Section Problem comes in. It’s a classic problem in OS that deals with process synchronization, resource sharing, and data consistency. It is a contention situation arises if two independent programs or processes want to access common data, and this can lead to a collision. Code segments that access common data areas, are prone to collisions known as critical sections. A critical section is understood as program parts that must not be interrupted by other processes or threads while executed on the CPU. 

Critical Section Problem in Operating System

It provided the processes or threads involved access shared resources. An atomic action is needed in a critical section i.e., a single process can run at a time in its critical section. All the entire processes have to wait to run in their critical sections. 

What is Critical Section Problem?

The Critical Section Problem arises when multiple processes (or threads) need to access and modify shared resources, but the operating system must ensure that only one process is in the critical section at any given time.

It is a fundamental concept in operating systems and concurrent programming, addressing how multiple processes can access a shared resource, such as data in memory or a file, without leading to data inconsistency or corruption. It’s a key issue in the design and implementation of multitasking operating systems, where processes must be coordinated to ensure that only one process at a time can enter its critical section, where it accesses shared resources.

We can also say that a critical section is a code segment in a complete program where processes access shared resources. Shared resources can be a common variable or file. Suppose our code is performing some write operations on common variable.

As multiple processes are executing concurrently, any process can be interrupted in the mid of its execution. Due to partial execution on shared resources by processes I mean here we can say shared variables can lead to data inconsistencies. This situation is known as a race condition.

Race conditions lead to inconsistent states of data. Therefore, we need a synchronization protocol that allows processes to cooperate while manipulating shared resources, which essentially is the critical section problem.

Goals of Solving the Critical Section Problem

To solve this problem, any synchronization solution must satisfy three essential conditions:

1. Mutual Exclusion

  • Only one process can be inside the critical section at any moment.

2. Progress

  • If no process is in the critical section, and some want to enter, one of them should be allowed without unnecessary delay.

3. Bounded Waiting

  • There should be a limit on the number of times other processes can enter the critical section before a waiting process gets a chance.

A solution must satisfy all three conditions to be considered valid.

Structure of a Process for Synchronization

Typically, a process that wants to use a shared resource follows this structure:

do {
    // Entry Section (request permission)
    
    // Critical Section (access shared resource)
    
    // Exit Section (release permission)
    
    // Remainder Section (rest of the code)
} while (true);

Understanding the Critical Section

  • Critical Section: A critical section is a part of a process that accesses shared resources (data structure, file, peripheral devices) that must not be concurrently accessed by more than one thread of execution.
  • Problem Statement: Ensure that when one process is executing within its critical section, no other process is allowed to execute in its critical section.

Requirements for Critical Section Problems

Primary

  • Mutual exclusion: If a process P1 is executing in a critical section then other processes can’t be executed in their critical section.
  • Progress: If a process doesn’t require to run in a critical section, then it should not let wait indefinitely for other processes to take into the critical section. 

Secondary

  • Bounded waiting: Capable to forecast the waiting time for the entire process to take into the critical section. 
  • Architectural neutrality: When our solution is functioning perfectly on architecture, then it should execute on the other ones also. Hence, our technique must be architectural natural.

There are multiple ways to solve the critical section problem, based on the level of abstraction:

Common Solutions to the Critical Section Problem

1. Software Solutions

a. Peterson’s Algorithm

A famous solution for two processes using flags and a turn variable.

b. Dekker’s Algorithm

One of the earliest solutions using busy waiting and turn management. These are low-level solutions and useful mainly for understanding the concept.

2. Hardware Solutions

Some processors provide atomic hardware instructions, such as:

  • Test-and-Set
  • Compare-and-Swap
  • Exchange

These help implement mutual exclusion at the hardware level, though they may use busy waiting.

3. Synchronization Tools in OS

Modern operating systems and programming languages provide high-level abstractions:

Semaphores

  • Integer variables used to control access.
  • Introduced by Dijkstra.
  • Types: Binary Semaphore (mutex) and Counting Semaphore

Mutex (Mutual Exclusion)

  • A lock that a process must acquire before entering the critical section.
  • Ensures only one process executes the critical section.

Monitors

  • High-level synchronization construct that encapsulates shared variables and their operations.
  • Used in languages like Java via synchronized blocks.

Real-World Example

Imagine an ATM where users withdraw money. If two users try to withdraw from a joint account at the same time:

  • Both processes check the balance at the same time
  • Both see ₹10,000
  • Both try to withdraw ₹10,000
  • Balance becomes negative ❌

This is a race condition, and it happens due to the critical section problem. Only one transaction should be allowed to proceed at a time.

Summary Table

TermDescription
Critical SectionCode segment accessing shared resource
ProblemPrevent multiple processes accessing it simultaneously
Key ConditionsMutual Exclusion, Progress, Bounded Waiting
Software SolutionsPeterson’s, Dekker’s Algorithm
Hardware SolutionsTest-and-Set, Compare-and-Swap
OS ToolsMutex, Semaphore, Monitor

Conclusion

The Critical Section Problem is at the heart of process synchronization in operating systems. Solving it ensures that shared data remains consistent, prevents deadlocks, and avoids race conditions.