A way to control how concurrently-running threads and processes access shared resources. To maintain correctness, the OS must enforce an ordered way for processes to execute code that accesses shared data.
Terminology
Race Condition
A situation where the correctness of a program is unpredictable as it depends on the relative timing or interleaving of threads or processes.
Critical Section
Part of code that accesses or modifies shared resources.
Entry Section
Aka. pre-protocol. Part of the code executed before the critical section. Necessary synchronization steps are done by the thread to request permission to enter the critical section.
Exit Section
Aka. post-protocol. Part of the code executed immediately after the critical section. The thread releases locks and signals other threads waiting to enter their critical section.
Remainder Section
All other code than critical, entry and exit sections.
Contention
The number of threads waiting to enter their critical sections at the same time. Lower is better.
Can be reduced by:
- minimizing shared resources
- using finer-grained locks
- using algorithms that allow more concurrent access
No contention means, no threads are waiting to enter their critical section.