Race Conditions and Mutual Exclusion · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.
En anglais

Locking the lines that must not mix

Computing · Computer Systems · ages 20-22
Name ______________________   Date ____________
  1. A shared counter is 6. In a non-atomic trace, both threads read 6 and calculate 7; A stores 7, then B stores 7. Why is an increment lost?

    • The later thread adds its increment to the already updated value
    • Both compute from old 6 and each stores its computed result, 7
    • The counter changes automatically whenever the scheduler switches
  2. In the lesson's non-atomic model, how does a thread increment a shared counter from 3?

    • Read 3, calculate 4 locally, then store 4
    • Read 3 and calculate 4 locally, leaving shared memory unchanged
    • Atomically change shared 3 to 4 with no separate read or store
  3. What is a critical section in this lesson?

    • Code the scheduler is forbidden to interrupt under any conditions
    • Code that makes every unrelated thread stop using the processor
    • Code whose shared-data accesses need mutual exclusion
  4. Every conflicting balance update uses the same lock. Where should one update acquire and release it to prevent a lost update?

    • Acquire after reading; release before storing the new balance
    • Acquire before reading; release after storing the new balance
    • Acquire only while printing; leave the balance update outside the lock
  5. In a non-atomic teaching trace, balance starts at 20. A and B each read 20, add 3 locally, then store. A stores first and B stores second. What is the final balance?

    Answer: ______________

  6. To protect one balance update, a thread must keep the balance lock while doing unrelated printing that does not access the balance.

    Circle one:   True   False

  7. If every thread changing a stock count uses the same lock, holding that lock across checking stock and taking an item prevents another such thread from changing stock between those two steps.

    Circle one:   True   False

  8. Balance is 12. A checks balance >= 10. B then withdraws 5, leaving 7. A uses its earlier check and withdraws 10, leaving -3. No shared lock protects the check and withdrawal. What caused the negative result?

    • A used a check made before B changed the balance
    • The comparison itself subtracted money from the shared balance
    • B's withdrawal increased the amount available for A to withdraw
LightMySky · lightmysky.comW1-mt_hygT74jJDE-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Locking the lines that must not mix W1-mt_hygT74jJDE-s1

  1. Both compute from old 6 and each stores its computed result, 7 · B stores 7 calculated from the old 6. It does not read A's updated 7 and calculate 8. Its store leaves 7, so one increment is lost.
  2. Read 3, calculate 4 locally, then store 4 · The thread reads 3, calculates 3 + 1 = 4, then stores 4. The model treats these as separate steps.
  3. Code whose shared-data accesses need mutual exclusion · Mutual exclusion controls entry to the protected operation. It does not prevent the lock holder from being paused or unrelated work from running.
  4. Acquire before reading; release after storing the new balance · The lock must cover the read, calculation and store. Otherwise another update can use the same old balance.
  5. 23 · Each calculates 20 + 3 = 23. The two stores leave 23, not the coordinated result 26.
  6. False · The protected region must cover the balance operation. Unrelated printing does not need that lock and can make contenders wait longer.
  7. True · The shared lock excludes the other participating updates until the check-and-take operation releases it.
  8. A used a check made before B changed the balance · A's check was true at 12 but stale after B left 7. A then subtracted 10 from 7, producing -3.
Worksheet · LightMySky