Critical Section in Synchronization (original) (raw)

Last Updated : 14 Apr, 2026

A critical section is a part of a program where shared resources (like memory, files, or variables) are accessed by multiple processes or threads. To avoid problems such as race conditions and data inconsistency, only one process/thread should execute the critical section at a time using synchronization techniques. This ensures that operations on shared resources are performed safely and predictably.

Structure of a Critical Section

A critical section is a key concept in operating systems that ensures safe and controlled access to shared resources when multiple processes or threads are executing simultaneously. It helps prevent issues like race conditions and data inconsistency by allowing only one process or thread to access the shared resource at a time, using synchronization techniques for proper coordination.

**1. Entry Section

**2. Critical Section: The actual code where shared resources are accessed or modified.

**3. **Exit Section: The process releases the lock or semaphore, allowing other processes to enter the critical section.

4. **Remainder Section: The rest of the program that does not involve shared resource access.

Critical_section

Block-Diagram of Critical Section

**Critical Section Problem

**Shared Resources and Race Conditions

It could be visualized using the pseudo-code below

do{
flag=1;
while(flag); // (entry section)
// critical section
if (!flag)
// remainder section
} while(true);

**Requirements of a Solution

A good critical section solution must ensure:

  1. **Correctness - Shared data should remain consistent.
  2. **Efficiency - Should minimize waiting and maximize CPU utilization.
  3. **Fairness - No process should be unfairly delayed or starved.

**Requirements of Critical Section Solutions

**1. Mutual Exclusion

**2. Progress

**3. Bounded Waiting

**Example Use Case: Older operating systems or embedded systems where simplicity and reliability outweigh responsiveness.

**Solution to Critical Section Problem :

A simple solution to the critical section can be thought of as shown below,

acquireLock();
Process Critical Section
releaseLock();

A thread must acquire a lock prior to executing a critical section. The lock can be acquired by only one thread. There are various ways to implement locks in the above pseudo-code.

Examples of critical sections in real-world applications

**Banking System (ATM or Online Banking)

**Ticket Booking System (Airlines, Movies, Trains)

**Print Spooler in a Networked Printer

**File Editing in Shared Documents (e.g., Google Docs, MS Word with shared access)