LightMySky
En anglais

Threads and Shared Memory

Threads in one process share its address space and resources. Each has its own execution state and normally its own stack, but those stacks are not separate protected address spaces. Threads can overlap work and waiting; conflicting accesses to shared data need coordination.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Say what threads in one process share and what each keeps to itself
  • Explain why creating a thread usually needs less address-space setup than creating a process
  • Identify a variable in a small program that two threads could reach at once

1 · Read

A process normally has its own virtual address space. Threads within that process share it, including program code, global variables and heap data. They also share process resources such as open files, which are not themselves memory addresses. Threads do not gain separate memory protection from each other.

Each thread has its own execution state: program counter, registers and stack pointer. It normally uses its own stack for local calls and variables. These stacks still lie in the shared address space; a pointer can let another thread reach a stack variable. In an example where A uses local a, B uses local b, and both update global total, total is the explicitly shared variable.

Creating a thread usually needs less address-space setup because it reuses its process's mappings. It still needs execution state and normally a stack. Creating a process establishes a separate address space, though techniques such as copy-on-write can avoid copying all memory immediately. Actual costs depend on the OS and workload.

With OS-scheduled threads, one thread can wait for disk input while another runnable thread computes, even on one CPU. Multiple CPUs may also run threads in parallel. During that wait, progress requires another thread that is runnable and able to proceed. The next trace shows why shared updates also need coordination.

total starts at 10A: read 10, add 2B: read 10, add 2A: write 12B: write 12total ends at 12
In this non-atomic trace, both updates use the old value. The second store replaces the first with the same result.

Switching between threads of one process keeps the same process memory mappings. Each thread's execution state still has to be saved and restored. Switching to a different process may also require changing the active address-space context. Cache effects and OS details still affect the cost; a thread switch is not free.

Conflicting accesses to shared data need coordination. It is not enough to coordinate only writers if readers need a consistent result. For a shared pair that must match, coordinate reading the pair with changing it, so a reader cannot observe only half an update. Sharing memory by itself provides no such rule.

Threads share process memory and resources but keep separate execution state. Shared data needs coordinated access; lower setup cost and progress depend on context.

Original lesson, questions and flashcards about thread execution state and shared memory.

The Operating System Engineering overview is adapted from MIT's course description, catalogued by OER Commons as course.303372. The reading "Shared memory and thread-owned state" and the flashcards were written for this topic.

2 · Watch

En anglais

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Threads and Shared Memory · Computing, ages 20 to 21 · LightMySky