Phase 3Ch 3.2 Semaphores and classic problems
ACompressed recap
Semaphore: wait (S--, block if S < 0), signal (S++, wake if S ≤ 0); −k = k waiters; final = initial − waits + signals. Mutex 1, ordering 0, k resources k. Bounded buffer: mutex 1, empty N, full 0, counting wait before mutex. Readers–writers (first): first reader waits rw, last signals; writers starve. Philosophers: all-left deadlock; ≤ 4 at table, asymmetry, both-at-once; max ⌊n/2⌋ eat. Monitor: one active inside; condition signal lost if no waiter. Method: shared/local, split, extremes, semaphores, four questions.
BMerged rule card
3.2.1 Semaphores: wait, signal, counting and binary semaphores
- wait/P: S--; if S < 0 block. signal/V: S++; if S ≤ 0 wake one. Atomic.
- Negative value −k = k processes waiting. Final value = initial − waits + signals.
- Binary (mutex, starts 1) vs counting (starts k). Ordering semaphore starts 0.
- Opposite orders of waits can deadlock; LIFO queues can starve.
3.2.2 Classic problems: bounded buffer, readers–writers, dining philosophers
- Bounded buffer: mutex = 1, empty = N, full = 0; counting wait before mutex wait; signals in any order.
- Readers–writers (reader priority): first reader waits on rw, last reader signals rw; writers may starve.
- Dining philosophers: all left-first → deadlock; fixes: ≤ 4 at table, asymmetry, both-at-once. Max ⌊n/2⌋ eat together.
3.2.3 Monitors, and reasoning about concurrent code
- Monitor: shared data + functions; one process active inside; condition variables wait/signal.
- x.wait releases the monitor; x.signal wakes one, or does nothing if none waits.
- Signal-and-wait vs signal-and-continue (re-check with while).
- Code analysis: shared/local → split → extremes → semaphore values → ME, deadlock, progress, starvation.
CInterleaved GATE-style questions
Q1 · NAT · 1 mark(s) — Arithmetic
A semaphore starts at 7; 20 wait and 15 signal operations follow. Final value?
Numerical answer: ________
Q2 · NAT · 2 mark(s) — Outputs
s = 0, t = 0. P1: print "A"; signal(s); wait(t); print "C"; P2: wait(s); print "B"; signal(t); How many different outputs are possible?
Numerical answer: ________
Q3 · MCQ · 1 mark(s) — Deadlock
P1 does wait(S); wait(Q); and P2 does wait(Q); wait(S); (S = Q = 1). What can happen?
- (A) Nothing can go wrong
- (B) Mutual exclusion is violated
- (C) Deadlock
- (D) Both processes always finish
Q4 · MCQ · 1 mark(s) — Bounded buffer
In the bounded-buffer solution, which pair of lines can be swapped safely?
- (A) None
- (B) The consumer's
wait(full)andwait(mutex) - (C) The producer's
wait(empty)andwait(mutex) - (D) The producer's
signal(mutex)andsignal(full)
Q5 · NAT · 1 mark(s) — Initial values
Bounded buffer of size 5: sum of the initial values of empty, full and mutex?
Numerical answer: ________
Q6 · MCQ · 1 mark(s) — Readers–writers
In the first readers–writers solution, which reader releases the writers' semaphore?
- (A) Every reader
- (B) The writer itself
- (C) The first reader
- (D) The last reader to leave
Q7 · MCQ · 1 mark(s) — Monitor
A process calls x.wait() inside a monitor. Which is true?
- (A) It terminates the process
- (B) It returns immediately if no one signalled before
- (C) It releases the monitor and sleeps on x
- (D) It keeps the monitor locked while sleeping
Q8 · NAT · 2 mark(s) — Final values
x = 5. P1 runs wait(s); x = x + 1; signal(s); but P2 runs x = x − 1; without using s (s starts at 1). Statements are load/compute/store. How many different final values of x are possible?
Numerical answer: ________
DAnswer Vault
Try the questions first. Open when you are ready to check.
🔐 Open the Answer Vault
Q1 Answer: 2 · 1 mark(s)
7 − 20 + 15.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q2 Answer: 1 · 2 mark(s)
Only ABC: B needs s, and C needs t from P2.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q3 Answer: (C) · 1 mark(s)
Each holds one and waits for the other.
- (A) is wrong: A bad interleaving exists.
- (B) is wrong: Each semaphore still admits one.
- (D) is wrong: Not always.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q4 Answer: (D) · 1 mark(s)
Signals never block.
- (A) is wrong: The signals can be swapped.
- (B) is wrong: Deadlock on an empty buffer.
- (C) is wrong: Deadlock on a full buffer.
Re-read: Lesson 3.2.2 — Classic problems: bounded buffer, readers–writers, dining philosophers
Q5 Answer: 6 · 1 mark(s)
5 + 0 + 1.
Re-read: Lesson 3.2.2 — Classic problems: bounded buffer, readers–writers, dining philosophers
Q6 Answer: (D) · 1 mark(s)
When read_count drops to 0.
- (A) is wrong: Only when the count is 0.
- (B) is wrong: Readers release it.
- (C) is wrong: The first reader acquires it.
Re-read: Lesson 3.2.2 — Classic problems: bounded buffer, readers–writers, dining philosophers
Q7 Answer: (C) · 1 mark(s)
Others can enter meanwhile.
- (A) is wrong: No.
- (B) is wrong: There is no remembered signal.
- (D) is wrong: That would block everyone.
Re-read: Lesson 3.2.3 — Monitors, and reasoning about concurrent code
Q8 Answer: 3 · 2 mark(s)
P2 ignores the lock, so the race remains: 4, 5 or 6.
Re-read: Lesson 3.2.3 — Monitors, and reasoning about concurrent code