Operating Systems · Threads
Thread Memory Model: Shared Code/Data/Heap and Private Stack/Registers/PC
Overview
A thread is the unit that actually runs and does work inside a process. Within one process, the code, data and heap are created only once and shared by all threads, while each thread gets its own private stack, register set and program counter.
Key concepts
- Thread: the unit that actually runs and does work inside a process; for example, when a client has to be served, one thread is run and that thread services the client.
- Same section structure as a process: a thread has a code section, a data section, a heap section and a stack.
- Shared state: within one process, the code, data and heap are created only once and are shared by all threads.
- Private state: each thread gets its own private copy of the stack, its own copy of the register set, and its own copy of the program counter (PC).
How it works
- A process is created with a code section and a data section, and it also has heap data.
- When the process is multithreaded (e.g. one thread per client to be served), the code and data section exist only once — they are not duplicated per thread. The heap is also shared.
- Each thread is given its own stack (private), its own register set (private) and its own program counter (private).
- Because the PC and registers are private, each thread can be executing a different point in the shared code independently, while all threads read and write the same code, global data and heap.
Comparison
What is shared vs. what is private inside a single process with multiple threads:
| Memory / state | Shared or private |
|---|---|
| Code section | Shared (only one copy) |
| Data section | Shared (only one copy) |
| Heap | Shared |
| Stack | Private — one copy per thread |
| Register set | Private — one copy per thread |
| Program counter (PC) | Private — one copy per thread |
Revision summary
- A thread performs the actual work, e.g. servicing a client request.
- A thread has the same section structure as a process: code, data, heap, stack.
- Code + data + heap are created once per process and shared by all its threads.
- Stack, register set and program counter are per-thread (private).
- Memorise the split: shared = code, data, heap; private = stack, registers, PC.
Previous-year exam insights
- Threads of a process share the address space (code, global data, heap), open file descriptors/file table, and signal handlers; each thread has its own stack, registers, PC, and thread-local storage.
Asked inGATE 2024GATE 2017GATE 2004 - On a context switch between two threads of the same process, only the private state (stack pointer, program counter, general-purpose registers) is saved/restored; the page table base register and address space are not switched because the threads share the same address space.
Asked inGATE 2023 - A thread is called a lightweight process because the OS maintains less per-thread state than per-process state: per-thread it keeps only CPU register state, stack, and PC, while virtual memory state (address space) is per-process, not per-thread.
Asked inGATE 2011 - Global variables are shared by all threads of a process, but automatic (local) variables are private because each thread has its own stack; thus unsynchronized access to a shared global from multiple threads causes a race condition.
Asked inGATE 2021GATE 2017 - Threads can be implemented in user space (library-managed, kernel unaware), in kernel space (kernel-managed), or as a hybrid/multilevel model; user-level threads are invisible to the kernel and a blocking call in one can block all.
Asked inGATE 2024 - The stack is never shared among threads of a process; statements claiming a common/shared stack for all threads are false.
Asked inUGC NET 2023GATE 2024
Thread memory layout in one process
Drawing the diagram…
Shows that code, data and heap are created once and shared, while each thread has its own stack, registers and program counter.