Critical Section in OS

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!


The critical section problem is one of the classic problems in Operating Systems. In operating systems, there are processes called cooperative processes that share and access a single resource. In these kinds of processes, the problem of synchronization occurs. The critical section problem is a problem that deals with this synchronization.


  • This article explains the structure of the critical section.
  • The critical section problem and the solution for the critical section problem
  • This article also provides an intuitive example of the critical section and the different ways to solve the critical section problem.

What is the Critical Section in OS?

  • Critical Section refers to the segment of code or the program which tries to access or modify the value of the variables in a shared resource.
  • The section above the critical section is called the Entry Section. The process that is entering the critical section must pass the entry section.
  • The section below the critical section is called the Exit Section.
  • The section below the exit section is called the Reminder Section and this section has the remaining code that is left after execution.

Process of critical section in OS

What is the Critical Section Problem in OS?

When there is more than one process accessing or modifying a shared resource at the same time, then the value of that resource will be determined by the last process. This is called the race condition.

Consider an example of two processes, p1 and p2. Let value=3 be a variable present in the shared resource.

Let us consider the following actions are done by the two processes,

The original value of,value should be 6, but due to the interruption of the process p2, the value is changed back to 3. This is the problem of synchronization.

The critical section problem is to make sure that only one process should be in a critical section at a time. When a process is in the critical section, no other processes are allowed to enter the critical section. This solves the race condition.

Example of Critical Section Problem

Let us consider a classic bank example, this example is very similar to the example we have seen above.

  • Let us consider a scenario where money is withdrawn from the bank by both the cashier(through cheque) and the ATM at the same time.
  • Consider an account having a balance of ₹10,000. Let us consider that, when a cashier withdraws the money, it takes 2 seconds for the balance to be updated in the account.
  • It is possible to withdraw ₹7000 from the cashier and within the balance update time of 2 seconds, also withdraw an amount of ₹6000 from the ATM.
  • Thus, the total money withdrawn becomes greater than the balance of the bank account.

This happened because of two withdrawals occurring at the same time. In the case of the critical section, only one withdrawal should be possible and it can solve this problem.

Solutions to the Critical Section Problem

A solution developed for the critical section should have the following properties,

  • If a process enters the critical section, then no other process should be allowed to enter the critical section. This is called mutual exclusion.
  • If a process is in the critical section and another process arrives, then the new process must wait until the first process exits the critical section. In such cases, the process that is waiting to enter the critical section should not wait for an unlimited period. This is called progress.
  • If a process wants to enter into the critical section, then there should be a specified time that the process can be made to wait. This property is called bounded waiting.
  • The solution should be independent of the system's architecture. This is called neutrality

Some of the software-based solutions for critical section problems are Peterson's solution, semaphores, monitors. Some of the hardware-based solutions for the critical section problem involve atomic instructions such as TestAndSet,compare and swap, Unlock and Lock.

Pseudocode of Critical Section Problem

The concept of a critical section problem can be illustrated with the following pseudocode

Consider a process is in the critical region and a new process wants to enter the critical section. In this case, the new process will be made to wait in the entry section by the while loop. The while loop will continue to stall the process until the variable process_entered is made 0 by the process exiting from the critical section in the exit section.

Thus, this mechanism prevents two processes to enter the critical section at the same time.


  • The critical section is used to solve the problem of race conditions occurring due to synchronization.
  • The critical section represents the segment of code that can access or modify a shared resource.
  • There can be only one process in the critical section at a time.
  • There are many solutions to critical section problems such as Peterson's solution, semaphore, etc.