Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Process synchronization and race conditions

Watch a race happen, instruction by instruction.

Module 3 Session 3.2 Lesson 1 of 4

Operating Systems: Process Synchronization

Detailed Teaching & Learning Module: Race Conditions & Critical Section Anatomy

1. The Problem: What is a Race Condition?

In a multi-tasking operating system, multiple concurrent processes or threads often share data (e.g., shared memory variables, files, or database records). A Race Condition occurs when multiple processes access and manipulate the same shared data concurrently, and the outcome of the execution depends on the specific order in which instructions are executed.

Classic Example: Shared Counter
Suppose Process A and Process B both execute counter++ on a shared variable initialized to 5.
  • At machine code level, counter++ expands to three instructions: 1. Read counter, 2. Increment register, 3. Write counter.
  • If CPU interrupts Process A midway and switches to Process B, the final value of counter could end up as 5 or 6 instead of the correct value 7.

2. Anatomy of a Synchronized Process

To prevent race conditions, access to shared data must be synchronized. Each process must request permission to enter its Critical Section. The general code structure for any typical process Pi executing in a loop is defined as follows:

// General structure of a typical process Pi
do {
    entry section    critical section    exit section    remainder section} while (true);
1. Entry Section
Requests permission to access the shared resources. Implements lock acquisition, semaphores, or hardware atomic signals.
2. Critical Section (CS)
The region of code where shared resources (variables, memory, files) are modified. Only one process may execute here at any given time.
3. Exit Section
Releases permission (unlocks resource or signals semaphore) so other waiting processes can enter their critical section.
4. Remainder Section
The rest of the code executing non-shared local operations after exiting the critical section.

3. Three Requirements for a Valid Critical Section Solution

Any software or hardware protocol designed to solve the Critical Section problem must satisfy three criteria:

  1. Mutual Exclusion: If process Pi is executing in its critical section, then no other processes can be executing in their critical sections.
  2. Progress: If no process is executing in its critical section and some processes wish to enter, only those processes not executing in their remainder section can participate in deciding which will enter next.
  3. Bounded Waiting: There must be a bound or limit on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter and before that request is granted.

4. Interactive Lab: Race Condition vs. Mutual Exclusion

Use the simulator below to compare unsynchronized execution (Race Condition) with synchronized mutual exclusion locks.

Process P1

Remainder Section

Process P2

Remainder Section
Shared Balance Variable: $100 | Active Processes in CS: 0
⚠️ RACE CONDITION DETECTED! Both Process P1 and Process P2 are inside the Critical Section simultaneously! Data corruption will occur!