Phase 3
Phase 3 Checkpoint: 15 questions on synchronization.
15 questions · 20 marks · 45 minutes. Negative marking on MCQ only: −1/3 mark for a wrong 1-mark MCQ, −2/3 for a wrong 2-mark MCQ. No negative marking for MSQ or NAT; MSQ has no partial credit.
Q1 · NAT · 1 mark(s) — Race
x = 10. Thread 1 runs x = x + 5 and thread 2 runs x = x * 2 (each a load, a compute and a store). How many different final values are possible?
Numerical answer: ________
Q2 · MCQ · 1 mark(s) — Definition
A race condition is:
- (A) a situation where the result depends on the order in which threads run
- (B) a deadlock between two threads
- (C) a hardware fault
- (D) a scheduling algorithm
Q3 · NAT · 2 mark(s) — Loops
x = 0; two threads each execute x++ 5 times. Largest possible final value?
Numerical answer: ________
Q4 · MCQ · 1 mark(s) — Strict alternation
Strict alternation guarantees mutual exclusion but violates:
- (A) nothing
- (B) progress
- (C) mutual exclusion
- (D) atomicity of turn
Q5 · MCQ · 2 mark(s) — Peterson
Which line order is correct for Peterson's entry section of process i?
- (A)
turn = j; flag[i] = true; while (flag[j] && turn == j); - (B)
flag[i] = true; turn = j; while (flag[j] && turn == j); - (C)
turn = i; while (turn == j); flag[i] = true; - (D)
flag[i] = true; turn = i; while (flag[j] && turn == j);
Q6 · MCQ · 1 mark(s) — TSL
The test-and-set instruction:
- (A) only works on one CPU
- (B) returns the new value
- (C) needs interrupts to be enabled
- (D) sets the lock and returns the old value, atomically
Q7 · NAT · 1 mark(s) — CAS
v = 9. CAS(&v, 9, 2) is called, then CAS(&v, 9, 5). Final v?
Numerical answer: ________
Q8 · NAT · 1 mark(s) — Arithmetic
A semaphore starts at 4; then 9 wait and 3 signal operations are done (blocking version). How many processes are waiting?
Numerical answer: ________
Q9 · NAT · 2 mark(s) — Outputs
S = 1, T = 0. P1 runs twice: wait(S); print "0"; signal(T); P2 runs twice: wait(T); print "1"; signal(S); How many different outputs are possible?
Numerical answer: ________
Q10 · NAT · 2 mark(s) — Outputs
Same code with S = 1 and T = 1. How many different outputs are possible?
Numerical answer: ________
Q11 · MCQ · 1 mark(s) — Use
A semaphore initialised to 0 is typically used for:
- (A) ordering one event after another
- (B) counting free buffer slots
- (C) mutual exclusion
- (D) nothing
Q12 · NAT · 1 mark(s) — Bounded buffer
Producer–consumer with a 10-slot buffer: after the producer adds 4 items and the consumer removes 1 (no one waiting), what is the value of full?
Numerical answer: ________
Q13 · MCQ · 1 mark(s) — Philosophers
All five philosophers pick up the left chopstick at the same moment. The result is:
- (A) two philosophers eating
- (B) mutual exclusion violated
- (C) deadlock
- (D) starvation of one philosopher only
Q14 · MCQ · 1 mark(s) — Monitors
The main difference between signal on a condition variable and signal on a semaphore is that the condition variable's signal:
- (A) needs busy waiting
- (B) can wake all processes
- (C) is lost if no process is waiting
- (D) increments a counter
Q15 · NAT · 2 mark(s) — Race with semaphore
x = 0. W and X each do x = x + 1; Y and Z each do x = x − 2; each brackets its read-modify-write with wait(S) … signal(S), and S starts at 1. Final value of x?
Numerical answer: ________
✓Answer Vault
Try the questions first. Open when you are ready to check.
🔐 Open the Answer Vault
Q1 Answer: 4 · 1 mark(s)
30, 25, 15, 20.
Re-read: Lesson 3.1.1 — Race conditions and the critical-section problem
Q2 Answer: (A) · 1 mark(s)
Timing-dependent results.
- (B) is wrong: Different problem.
- (C) is wrong: Software, not hardware.
- (D) is wrong: Not an algorithm.
Re-read: Lesson 3.1.1 — Race conditions and the critical-section problem
Q3 Answer: 10 · 2 mark(s)
No updates lost.
Re-read: Lesson 3.1.1 — Race conditions and the critical-section problem
Q4 Answer: (B) · 1 mark(s)
A free CS can be refused.
- (A) is wrong: Progress fails.
- (C) is wrong: That holds.
- (D) is wrong: Not a requirement.
Re-read: Lesson 3.1.2 — Software solutions: turn and flag attempts, Peterson's algorithm
Q5 Answer: (B) · 2 mark(s)
Flag first, then give away the turn.
- (A) is wrong: Swapped order breaks mutual exclusion.
- (C) is wrong: Not Peterson.
- (D) is wrong: Giving the turn to yourself with that test can let both in.
Re-read: Lesson 3.1.2 — Software solutions: turn and flag attempts, Peterson's algorithm
Q6 Answer: (D) · 1 mark(s)
Atomic read-and-set.
- (A) is wrong: Works on multiprocessors too.
- (B) is wrong: Returns the old value.
- (C) is wrong: Unrelated.
Re-read: Lesson 3.1.3 — Hardware help: test-and-set, compare-and-swap, spinlocks
Q7 Answer: 2 · 1 mark(s)
The second call finds 2, not 9.
Re-read: Lesson 3.1.3 — Hardware help: test-and-set, compare-and-swap, spinlocks
Q8 Answer: 2 · 1 mark(s)
4 − 9 + 3 = −2.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q9 Answer: 1 · 2 mark(s)
Only 0101: the semaphores force alternation.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q10 Answer: 4 · 2 mark(s)
0101, 0110, 1001, 1010.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q11 Answer: (A) · 1 mark(s)
The waiter blocks until signalled.
- (B) is wrong: Starts at N.
- (C) is wrong: Mutex starts at 1.
- (D) is wrong: It is useful.
Re-read: Lesson 3.2.1 — Semaphores: wait, signal, counting and binary semaphores
Q12 Answer: 3 · 1 mark(s)
0 + 4 − 1.
Re-read: Lesson 3.2.2 — Classic problems: bounded buffer, readers–writers, dining philosophers
Q13 Answer: (C) · 1 mark(s)
Everyone holds one and waits for one.
- (A) is wrong: Nobody can eat.
- (B) is wrong: Each chopstick is held by one.
- (D) is wrong: Everyone is stuck.
Re-read: Lesson 3.2.2 — Classic problems: bounded buffer, readers–writers, dining philosophers
Q14 Answer: (C) · 1 mark(s)
No memory.
- (A) is wrong: No.
- (B) is wrong: signal wakes one.
- (D) is wrong: That is the semaphore.
Re-read: Lesson 3.2.3 — Monitors, and reasoning about concurrent code
Q15 Answer: -2 · 2 mark(s)
S = 1 removes the race.
Re-read: Lesson 3.2.3 — Monitors, and reasoning about concurrent code