Ch. 21 · Operating Systems

Race Conditions and Atomic State Transitions

Race Conditions and Atomic State Transitions. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

A race occurs when correctness depends on uncontrolled timing between operations. Atomic individual reads and writes do not make a compound transaction atomic.

Before you start

You should understand processes, threads, memory and basic file operations. Track ownership and state changes over time. For concurrent scenarios, write down at least two possible execution orders; the same instructions can behave differently when scheduling or resource availability changes.

The practical goal is to reason through this situation: Two workers read the same balance and overwrite each other’s increments. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: List compound steps

Reading, calculating and writing form more than one atomic action.

Step 2: Construct an interleaving

Two workers can both calculate from the same old value.

Step 3: Protect the invariant

Use an atomic operation or consistent lock around the complete transition.

Worked scenario

Two workers read the same balance and overwrite each other’s increments.

Both workers read balance 10. Each adds one and writes 11, producing 11 instead of 12. Atomic individual reads and writes do not prevent this sequence. A delay can expose the race more frequently in a test, but only changing the coordination establishes correctness.

Common mistake

A delay inserted in tests may expose a race but does not fix it.

Verify the behavior

Trace the lost update and verify the chosen synchronization covers every relevant access.

Interview exercise

Protect a shared update.

Answer and reasoning

Use a synchronization or atomic operation matching the complete invariant, then test concurrent interleavings.

Continue learning

Compare the scenario with the Operating Systems interview questions and test your understanding with the Operating Systems MCQs. For terminology and implementation details, consult the reference material.

More in Operating Systems

read ✓Operating Systems · hard

Operating Systems: CPU Cache Locality

Improve performance with spatial and temporal locality, avoid pointer chasing, and understand false sharing between threads.

~2 min readread →
read ✓Operating Systems · hard

Copy-on-Write Memory and fork

How copy-on-write lets fork share pages until a write, why RSS can be misleading, and the implications for memory and latency.

~2 min readread →
read ✓Operating Systems · hard

Operating Systems: epoll and select

How select, poll and epoll report I/O readiness, and the difference between level- and edge-triggered notifications.

~2 min readread →
esc