A situation where the outcome depends on the order of access to shared data is a:
Process Synchronization
Race conditions, the critical-section problem and its three requirements, Peterson’s solution, hardware support (test_and_set, swap), semaphores and their implementation, deadlock and starvation, the bounded-buffer, readers–writers and dining-philosophers problems, and monitors.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 45 slides
- 1 Slide 1
- 2 Outline of this lecture
- 3 Objectives
- 4 Background
- 5 Producer
- 6 Race condition example
- 7 Race Condition
- 8 Race condition
- 9 The Critical Section (CS) Problem
- 10 Critical Section
- 11 Solution to Critical-Section Problem
- 12 Types of Solutions
- 13 Peterson ’ s Solution
- 14 Algorithm for Process P
- 15 Drawbacks of Software Solutions
- 16 Synchronization Hardware
- 17 test_and_set Instruction
- 18 Solution using test_and_set
- 19 swap Instruction
- 20 Solution using swap
- 21 Semaphore
- 22 Semaphore Usage
- 23 Semaphore Implementation
- 24 Semaphore Implementation with no Busy waiting
- 25 Semaphore Implementation with no Busy waiting (Cont.)
- 26 Deadlock and Starvation
- 27 Classical Problems of Synchronization
- 28 Bounded-Buffer Problem
- 29 Bounded Buffer Problem (Cont.)
- 30 Bounded Buffer Problem (Cont.)
- 31 Readers-Writers Problem
- 32 Readers-Writers Problem (Cont.)
- 33 Readers-Writers Problem (Cont.)
- 34 Dining-Philosophers Problem
- 35 Dining-Philosophers Problem (Cont.)
- 36 Problems with Semaphores
- 37 Operating System Solutions: Monitors
- 38 Operating System Solutions: Monitors
- 39 Operating System Solutions: Monitors
- 40 Variables
- 41 Monitor with Condition Variables
- 42 Condition Variables choices
- 43 Solution to Dining Philosophers
- 44 Solution to Dining Philosophers (cont)
- 45 Process Synchronization Conclusion
Race condition example
Notes
count++ is load–increment–store; interleaving these steps from two processes can lose an update.
Race Condition
Notes
Race condition: the result depends on the order in which processes access shared data.
Exam angle
Critical Section
Notes
Critical section: the code that touches shared data. Structure: entry section, critical section, exit section, remainder section.
Solution to Critical-Section Problem
Notes
Requirements: Mutual exclusion (one at a time), Progress (no needless waiting when the CS is free), Bounded waiting (no starvation).
Memory trick M-P-B: Mutual exclusion, Progress, Bounded waiting.
Exam angle
Which is NOT a requirement of a critical-section solution?
Peterson ’ s Solution
Notes
Peterson’s solution (2 processes): shared turn and flag[2]. Pᵢ sets flag[i] = true, turn = j, then waits while flag[j] && turn == j.
Synchronization Hardware
Notes
Hardware support: atomic (uninterruptible) instructions such as test_and_set and compare_and_swap.
test_and_set Instruction
Notes
test_and_set returns the old value and sets the lock to true, in one atomic step.
Semaphore
Notes
Semaphore S: integer accessed only by wait() (P: decrement, block if < 0) and signal() (V: increment, wake one).
Exam angle
wait(S) on a semaphore:
Semaphore Usage
Notes
Binary semaphore (0/1) = mutex lock. Counting semaphore controls access to N instances; initialize it to N.
Exam angle
A counting semaphore guarding 5 identical printers should be initialized to:
Semaphore Implementation
Notes
Busy waiting (spinlock) wastes CPU; better implementation blocks the process on the semaphore’s waiting list.
Deadlock and Starvation
Exam angle
P0 runs wait(S); wait(Q); and P1 runs wait(Q); wait(S); with S = Q = 1. What can happen?
Compare with the model answer
Deadlock: P0 gets S, P1 gets Q, then each waits forever for the semaphore the other holds.
Bounded-Buffer Problem
Notes
Bounded buffer: mutex = 1, full = 0, empty = n. Producer: wait(empty); wait(mutex); … signal(mutex); signal(full).
Trap Swapping the order to wait(mutex); wait(empty) can deadlock when the buffer is full.
Exam angle
In the bounded-buffer solution with n slots, empty starts at:
Dining-Philosophers Problem (Cont.)
Exam angle
Which avoids deadlock in dining philosophers?
- Also: pick up both only if both are free, or odd/even asymmetric ordering.
Problems with Semaphores
Notes
Semaphore misuse: signal before wait, wait twice, or forgetting one → broken mutual exclusion or deadlock.
Operating System Solutions: Monitors
Notes
Monitor: high-level construct; only one process active inside at a time.
Variables
Notes
Condition variables: x.wait() suspends the caller; x.signal() resumes one waiting process (no effect if none).