CSC 394 · Operating Systems
Our notes
2 min readRace condition
When processes share data and the result depends on the order in which they run. Example: two threads each do count = count + 1 starting from 5. Each loads 5, adds 1, and stores 6: the result is 6, not 7 (one update is lost).
The critical-section problem
A critical section is code that uses shared data. A solution must give:
- Mutual exclusion: at most one process in its critical section at a time.
- Progress: if none is inside, one that wants to enter must be able to, without endless postponement.
- Bounded waiting: a limit on how many times others can go first after a process has asked to enter.
Peterson's solution (two processes)
Shared: flag[2] (wants to enter) and turn.
Process i: flag[i] = true; turn = j; wait while (flag[j] and turn == j); critical section; flag[i] = false.
It satisfies all three requirements (in theory; modern CPUs reorder memory operations, so real systems use hardware instructions).
Hardware support
Test-and-set and compare-and-swap are atomic instructions used to build locks (mutexes). A lock that busy-waits is a spinlock (wastes CPU while waiting, fine for very short waits on multicore).
Semaphores (Dijkstra)
An integer S used only through two atomic operations:
- wait(S) (also P or down): if S > 0, S = S − 1; otherwise block.
- signal(S) (also V or up): S = S + 1 and wake a waiting process.
- Binary semaphore (0/1, like a mutex) for mutual exclusion; counting semaphore for a pool of n resources (start S = n).
Mutual exclusion: wait(mutex); critical section; signal(mutex), with mutex starting at 1.
Producer-consumer (bounded buffer) with semaphores
Shared: buffer of n slots, mutex = 1, empty = n, full = 0.
Producer: wait(empty); wait(mutex); add item; signal(mutex); signal(full).
Consumer: wait(full); wait(mutex); remove item; signal(mutex); signal(empty).
If the producer did wait(mutex) before wait(empty) with a full buffer, it would block while holding mutex and the consumer could never get in: deadlock. Order matters.
Other classic problems
- Readers-writers: many readers may read at once, but a writer needs exclusive access; risk of starving writers (or readers).
- Dining philosophers: five philosophers, five chopsticks; each needs two to eat. If all pick up their left chopstick at once, deadlock. Fixes: allow only four to sit, pick up both at once, or odd/even philosophers pick up in different orders.
Monitors
A high-level construct (Java's synchronized methods) where only one process can be active inside at a time; condition variables (wait, signal) let processes wait for a condition inside.
Exams usually ask: define race condition and critical section; the three requirements; Peterson's algorithm; semaphore operations; solve producer-consumer with semaphores; explain dining philosophers and how to avoid deadlock in it.
Common mistake: confusing semaphore signal() with UNIX signals. They are different things.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick OSTEP: Semaphores
Free chapter PDF from the authors' site (146 KB).
(opens pages.cs.wisc.edu in a new tab)
-
Introduction to Computer Science, 6.3 Processes and Concurrency
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 6.3 Processes and Concurrency, CC BY-NC-SA 4.0 · licence
-
OSTEP: Locks
Free chapter PDF from the authors' site (167 KB).
(opens pages.cs.wisc.edu in a new tab)
-
Semaphore (programming)
(opens en.wikipedia.org in a new tab)
Wikipedia, Semaphore (programming), CC BY-SA 4.0 · licence
-
Critical section
(opens en.wikipedia.org in a new tab)
Wikipedia, Critical section, CC BY-SA 4.0 · licence
Watch
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.