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. Each thread keeps its own program counter, stack pointer and registers, while sharing variables.

    Circle one:   True   False

  2. What do threads inside one process share?

    • One address space with the same variables
    • Separate private memories each
    • Nothing, they are fully isolated
  3. Why is creating a thread cheaper than creating a process?

    • Threads skip all safety checks
    • A thread reuses the existing address space
    • Threads run on faster hardware
  4. One thread waits for a slow disk while another keeps computing. What does this show?

    • The cost of sharing memory
    • The gain of threads: work continues while one part waits
    • That waiting threads use no memory
  5. Two threads both run total = total + 1 on a shared counter. Which variable can both reach at once?

    • total, since it sits in shared space
    • Only each thread's own program counter
    • Neither, threads cannot share variables
  6. What makes switching processes cost more than switching threads of one process?

    • Threads never need the CPU at all
    • Processes share everything, threads share nothing
    • The shared space stays put for threads, but processes swap address spaces
  7. You want the speed of sharing without the collisions. What is the best fix?

    • Give every thread a fully private memory
    • Stop using threads entirely
    • Let only one thread change the shared data at a time
  8. Two threads each raise a shared counter 1000 times, but it ends near 1000 instead of 2000. What happened?

    • The counter overflowed its memory
    • The threads stepped on each other and raises got lost
    • The threads ran on separate address spaces
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. True · Private CPU state per thread, shared variables across threads.
  2. One address space with the same variables · All threads of one process live in the same address space with no barrier between them.
  3. A thread reuses the existing address space · No fresh address space and no fresh translation are needed for a thread.
  4. The gain of threads: work continues while one part waits · While one thread waits, another can run, so the program keeps working.
  5. total, since it sits in shared space · Shared variables sit in the common space, so both threads can touch total together.
  6. The shared space stays put for threads, but processes swap address spaces · A process switch must swap the address space and its translation, a thread switch skips that.
  7. Let only one thread change the shared data at a time · Guarding shared data keeps the cheap sharing while removing the stepping.
  8. The threads stepped on each other and raises got lost · Both threads read the same value together and wrote back once, losing raises.
Worksheet · LightMySky