Operating System Interview Questions · Question 13

What is a race condition, and what is a critical section?

Interview preparation resource from Gate Smashers.

Interview-ready answer

A race condition occurs when a result depends on the unpredictable timing of concurrent operations. A critical section is the code that accesses shared mutable data and therefore needs controlled synchronization.

Understand it clearly

Consider two threads incrementing the same counter. An increment is usually a read-modify-write sequence rather than one indivisible action. If both threads read the old value before either writes the new value, one update can be lost.

Mutual exclusion, semaphores, atomic operations or carefully designed lock-free algorithms can protect the critical section. Synchronization should cover the smallest correct region because an unnecessarily large critical section reduces concurrency.

Suppose two threads increment the same counter by reading, adding and writing. If both read the old value before either write occurs, one increment is lost. Protecting that read-modify-write sequence as a critical section makes the operation appear indivisible to competing threads.