Critical Section in OS: Definition, Example & Solution

Home 

Critical Section in OS: Definition, Example & Solution

A critical section in OS refers to the part of a program where shared resources such as variables, memory, or files are accessed and modified. In a multitasking environment, multiple processes or threads may try to access these resources at the same time, which makes proper control essential.

This concept is important in process synchronization because it ensures that only one process executes the critical section at a time. By controlling access to shared resources, the operating system maintains data consistency, prevents conflicts, and ensures smooth execution of processes.

In the context of an operating system, managing critical sections is a key responsibility, as modern systems handle multiple processes simultaneously and must coordinate them efficiently.

In this blog, you will learn what a critical section in OS is, understand its structure, see a simple example, explore the critical section problem, and learn about the conditions and solutions used to manage it effectively.

What is Critical Section in OS?

What is Critical Section in OS?

A critical section in OS is a part of a program where shared resources such as variables, memory, or files are accessed and modified by a process.

In simple terms, it is the section of code that needs special attention because multiple processes may try to use the same resource at the same time. To avoid conflicts, only one process is allowed to execute the critical section at a time. This controlled access helps maintain data correctness and ensures smooth execution of programs.

Structure of Critical Section

A program that uses shared resources is typically divided into four parts to manage access properly:

Entry Section

This part of the code decides whether a process can enter the critical section. It checks conditions and waits if another process is already using the shared resource.

Critical Section

This is the main part where the shared resource is accessed or modified. Only one process is allowed to execute this section at a time to avoid conflicts.

Exit Section

After completing its task in the critical section, the process executes the exit section. Here, it releases the resource and allows other waiting processes to enter.

Remainder Section

This includes all the other parts of the program that do not involve shared resources. Processes can execute this section freely without any restrictions.

Example of Critical Section

To understand a critical section in OS, consider a simple example where two processes access the same shared data.

Shared Variable Example

Assume there is a shared variable:
balance = 1000

Two processes (Process A and Process B) try to update this balance at the same time. Process A wants to deposit ₹500, and Process B wants to withdraw ₹200.

Two Processes Accessing Same Data

Both processes start execution and access the same variable without any control. Since there is no restriction, they can enter the critical section simultaneously.

  • Process A reads balance = 1000
  • Process B also reads balance = 1000
  • Process A adds 500 → 1500
  • Process B subtracts 200 → 800

Conflict Scenario

Now both processes write their results back:

  • Process A writes 1500
  • Process B writes 800

The final value of balance becomes 800, which is incorrect. The correct value should be 1300 (1000 + 500 − 200).

This conflict happens because both processes accessed and modified the shared variable at the same time without proper control. If only one process had entered the critical section at a time, the final result would have been correct.

Critical Section Problem

Definition

The critical section problem is the challenge of ensuring that when multiple processes or threads share resources, only one of them is allowed to execute the critical section at a time. The goal is to maintain proper coordination so that shared data remains correct and consistent.

Why It Occurs

This problem occurs in multitasking systems where several processes run simultaneously and need access to shared resources like memory or variables. Without proper control, more than one process may enter the critical section at the same time, leading to conflicts and incorrect results.

Relation to Race Condition

The critical section problem is closely related to race condition in OS. A race condition occurs when multiple processes access shared data concurrently and the final result depends on the execution order. Solving the critical section problem ensures that such situations are controlled, thereby preventing race conditions and maintaining system stability.

Conditions for a Good Solution

To properly solve the critical section problem, any solution must satisfy the following three conditions:

Mutual Exclusion

Mutual exclusion ensures that only one process can enter the critical section at a time. If one process is already accessing shared resources, other processes must wait. This prevents conflicts and maintains data consistency.

Progress

The progress condition ensures that if no process is in the critical section, then the decision of which process should enter next cannot be delayed indefinitely. In simple terms, the system should not unnecessarily block processes when the critical section is free.

Bounded Waiting

Bounded waiting guarantees that every process will get a fair chance to enter the critical section. There is a limit on how long a process has to wait before it gets access, preventing starvation where a process waits indefinitely.

Solutions to Critical Section Problem

To handle the critical section problem, operating systems use different techniques to ensure that shared resources are accessed safely and only by one process at a time.

Software Solutions

Peterson’s Solution

Peterson’s solution is a simple software-based method designed for two processes. It uses two variables—flag (to indicate interest) and turn (to decide which process gets priority). This approach ensures that only one process enters the critical section at a time. However, it is mainly used for understanding concepts and is not widely used in modern systems.

Hardware Solutions

Test-and-Set

Test-and-Set is a hardware instruction that checks and updates a variable in a single atomic operation. It is used to create locks so that when one process is using a resource, others are prevented from entering the critical section until the lock is released.

Compare-and-Swap

Compare-and-Swap is another atomic instruction that compares a variable’s current value with an expected value and updates it only if they match. This ensures safe updates to shared data and helps prevent multiple processes from accessing the critical section simultaneously.

Synchronization Tools

Mutex

A mutex (mutual exclusion) is a locking mechanism that allows only one process to access a shared resource at a time. A process must acquire the mutex before entering the critical section and release it after completing its task.

Semaphores

Semaphores are synchronization tools that use a counter to control access to shared resources. They can allow one or multiple processes to access a resource, depending on the type (binary or counting), and are widely used for managing process synchronization in operating systems.

Advantages of Critical Section

Advantages of Critical Section

Prevents Data Inconsistency

The critical section ensures that only one process accesses shared resources at a time. This prevents multiple processes from modifying data simultaneously, which helps maintain correct and consistent results.

Ensures Proper Synchronization

By controlling access to shared resources, the critical section helps coordinate multiple processes effectively. It ensures that processes execute in an organized manner without interfering with each other.

Improves System Reliability

When shared resources are managed properly, the chances of errors, crashes, or unexpected behavior are reduced. This makes the system more stable and reliable, especially in environments where multiple processes run concurrently.

Disadvantages / Limitations

Can Reduce Performance

While critical sections ensure safe access to shared resources, they can slow down system performance. Since only one process is allowed to execute the critical section at a time, other processes must wait, which reduces parallel execution and overall efficiency.

Waiting Time for Processes

Processes that want to enter the critical section may have to wait until it becomes free. If many processes are competing for the same resource, this waiting time can increase, leading to delays and reduced responsiveness.

Complexity in Implementation

Implementing a proper critical section solution is not simple. It requires careful handling of synchronization mechanisms like locks, semaphores, or hardware instructions. If not implemented correctly, it can lead to issues like deadlock or starvation, making the system harder to design and maintain.

FAQs

Q1. What is critical section in OS in simple words?

A critical section in OS is the part of a program where shared resources like variables, memory, or files are accessed. Since multiple processes may try to use these resources at the same time, this section needs to be carefully controlled.

Q2. What is critical section problem?

The critical section problem is the challenge of ensuring that only one process or thread accesses shared data at a time. This prevents conflicts and helps maintain correct results when multiple processes are running.

Q3. What are the conditions of critical section?

A correct solution to the critical section problem must satisfy three conditions:

  • Mutual Exclusion – only one process can enter at a time
  • Progress – processes should not be unnecessarily delayed
  • Bounded Waiting – every process gets a fair chance to execute

Q4. How to solve critical section problem?

The problem can be solved using synchronization techniques such as mutex (locks) and semaphores, which control access to shared resources and ensure that only one process executes the critical section at a time.

Q5. Is critical section related to race condition?

Yes, the critical section is closely related to race condition in OS. Proper handling of the critical section helps prevent situations where multiple processes interfere with each other and produce incorrect results.

Conclusion

A critical section in OS is an essential concept for managing shared resources in a multitasking environment. It ensures that only one process accesses critical data at a time, helping maintain accuracy and consistency.

By properly handling the critical section, the operating system can achieve effective process synchronization and avoid conflicts between processes. Techniques like mutex, semaphores, and hardware instructions play a key role in implementing these controls.

Understanding the critical section and its solutions is important for building reliable and efficient systems, especially where multiple processes run simultaneously.