Threads and Shared Memory · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Threads share one memory, for better and worse

Computing · Computer Systems · ages 20-21
Name ______________________   Date ____________
  1. Separate thread stacks make one thread's stack memory inaccessible to every other thread in the process.

    Circle one:   True   False

  2. What memory do threads within one process share?

    • One address space, including globals and heap data
    • Separate address spaces, each holding copies of all globals
    • Only a disk directory, with no common memory addresses
  3. Why does creating a thread usually need less address-space setup than creating a process?

    • A thread never needs any stack storage or saved execution state
    • A thread usually reuses the process's existing memory mappings
    • A thread automatically runs on a separate, faster physical CPU
  4. Two OS-scheduled threads share a process on one CPU. A waits for disk input, while B is runnable and has independent computation to do. What can happen?

    • Another runnable thread can compute during the disk wait
    • Both threads must stop until every outstanding disk request ends
    • The waiting thread automatically releases all its memory mappings
  5. A uses local variable a, B uses local variable b, and both update global total. Neither local variable is passed to the other thread. Which variable is explicitly accessed by both?

    • a, the local value in thread A
    • b, the local value in thread B
    • total, the global value
  6. Which part of the setup can a switch between two threads of one process keep unchanged?

    • Keep the same saved program counter for both threads
    • Skip saving or restoring any register state at all
    • Keep the same process address-space mappings
  7. A shared pair starts left = 1, right = 1. A writer changes both to 2. A reader needs matching values. Which coordination is needed?

    • Coordinate the reader and writer using one shared rule
    • Coordinate only the writer while the reader ignores its updates
    • Give each access a separate rule and let the reader run at any time
  8. In a non-atomic teaching trace, total starts at 4. A and B each read 4, add 1 locally, then store their result. A stores before B. What is the final total?

    • 4
    • 5
    • 6
LightMySky · lightmysky.comW1-mt_N4EGNbN6Xh-s1

Answer key

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

Threads share one memory, for better and worse W1-mt_N4EGNbN6Xh-s1

  1. False · Thread stacks are normally used separately, but they occupy the shared address space. Another thread can reach stack data through a suitable pointer.
  2. One address space, including globals and heap data · Threads use their process's common address space. Separate execution state does not give each thread a separate memory space.
  3. A thread usually reuses the process's existing memory mappings · The thread reuses existing mappings instead of establishing a separate address space. It still needs thread-specific setup.
  4. Another runnable thread can compute during the disk wait · The scheduler can run B while A waits. One CPU is enough to overlap computation with an external I/O wait.
  5. total, the global value · The example states that both threads update total. It does not state that either accesses the other's local variable.
  6. Keep the same process address-space mappings · The threads use the same process mappings, but each has its own saved execution state.
  7. Coordinate the reader and writer using one shared rule · The reader must not read between the two writes. Coordination must include the reader as well as the writer.
  8. 5 · Both calculate 4 + 1 = 5. A stores 5, then B stores 5 over it, leaving 5 instead of 6.
Worksheet · LightMySky