OS Notes
Deep dive into the critical section problem — requirements, software solutions (Peterson\
Introduction
The critical section problem is the fundamental challenge of concurrent programming. When multiple processes need to access shared resources, how do we ensure only one accesses it at a time without creating deadlocks or starvation? Solving this elegantly and correctly has been a focus of computer science research since the 1960s.
Think about a shared printer in an office. If two people send print jobs simultaneously without coordination, pages from different documents would interleave, producing gibberish. The critical section is the act of printing — only one job should print at a time. The solution must ensure mutual exclusion while being fair and not creating unnecessary delays.
Formal Definition
Consider n processes {P0, P1, ..., Pn-1}. Each process has a segment of code called the critical section where it accesses shared data. The problem is to design a protocol ensuring:
- Mutual Exclusion: At most one process executes in its critical section at any time
- Progress: If no process is in the critical section and some processes wish to enter, the selection of who enters next cannot be postponed indefinitely. Only processes trying to enter participate in the decision.
- Bounded Waiting: After a process requests entry, there is a bound on the number of times other processes can enter before it gets its turn.
Software Solution: Peterson's Algorithm (2 Processes)
Proof of Correctness
Mutual Exclusion: Both processes cannot be in their CS simultaneously. If P0 is in CS, then either flag[1]=false OR turn=0. If P1 is also trying to enter, flag[1]=true and turn would equal either 0 or 1 (set by whoever wrote last). Only one value is possible, so one must be spinning.
Progress: If P0 wants to enter and P1 is in remainder section (flag[1]=false), P0 enters immediately without waiting.
Bounded Waiting: P0 gives priority to P1 (sets turn=1). If both want in, whoever set turn last loses one round. After P1 enters and exits, P0 will enter next.
Bakery Algorithm (N Processes)
Lamport's Bakery Algorithm extends the solution to N processes using a "take a number" approach (like a bakery queue):
The process with the smallest non-zero ticket number enters. Ties are broken by process ID. This satisfies all three requirements for any number of processes.
Hardware-Assisted Solutions
Disabling Interrupts (Uniprocessor Only)
void enter_critical_section() {
disable_interrupts(); // Cannot be preempted
}
void exit_critical_section() {
enable_interrupts();
}Simple but dangerous — a process can monopolize the CPU. Only works on single-core systems (other cores are not affected by disabling one core's interrupts).
Atomic Test-and-Set Lock (TSL)
// Implementation using hardware TSL instruction
bool lock = false;
void acquire() {
while (test_and_set(&lock) == true)
; // Spin (busy wait)
}
void release() {
lock = false;
}Satisfies mutual exclusion but NOT bounded waiting — a process can be starved if others keep getting the lock.
Bounded Waiting with Test-and-Set
This ensures bounded waiting by explicitly passing the lock to the next waiting process in circular order.
Busy Waiting vs Blocking
Busy waiting (Spinlock): Process loops continuously checking if it can enter. Wastes CPU cycles but no context switch overhead. Good for short critical sections.
Blocking (Sleep/Wake): Process is put to sleep (moved to wait queue) when it cannot enter. Woken up when the CS becomes available. No CPU waste but context switch overhead. Good for long critical sections.
Real-World Analogy
The critical section problem is like managing a single conference room. Mutual exclusion means only one meeting at a time. Progress means if the room is empty and someone wants it, they can take it immediately (no bureaucratic delays). Bounded waiting means no one is permanently locked out while others keep booking. Peterson's solution is like two polite people who both say "after you" — whoever says it last actually goes first.
Key Takeaways
- The critical section problem requires mutual exclusion, progress, and bounded waiting
- Peterson's Algorithm solves it for 2 processes; Bakery Algorithm solves it for N processes
- Software solutions are elegant but impractical on modern multi-core CPUs without memory barriers
- Hardware atomic instructions (test-and-set, CAS) provide the foundation for practical solutions
- Bounded waiting requires explicit fairness mechanisms (passing the lock in order)
- Spinlocks waste CPU but avoid context switch cost; blocking saves CPU but has switch overhead
- Modern systems use a combination: spin briefly, then block if still waiting
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Critical Section Problem.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, process, synchronization, critical, section
Related Operating Systems Topics