Definition
Race Condition
A race condition is a flaw in a concurrent system where the output or behavior depends on the uncontrollable sequence or timing of external events, such as the operating system’s scheduling of processes or threads.
A race condition occurs when influences outside the control of the process determine the final result, often leading to inconsistent or incorrect outcomes.
Example: Concurrent Increment/Decrement
Consider a shared variable a = 100. Two processes perform operations:
- Process A: Increment
aby 100. - Process B: Decrement
aby 100.
If executed sequentially, a remains 100. However, if the execution is interleaved (interrupted after reading but before writing), the result might be 200 or 0, depending on which process writes back to memory last.
Mitigation
To prevent race conditions, the OS must provide mechanisms for Mutual Exclusion and Synchronisation, ensuring that access to shared resources is controlled and deterministic.