0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- After fork, are variables shared? from Lesson 1.1.2
- Which segment holds local variables? from Lesson 1.1.1
- Interrupts come from? from Lesson 0.1.2
1 · The Hook
A web browser downloads an image, plays a video and responds to your scroll, all at once, inside one process. It uses threads: several lines of execution sharing the same memory. Threads are cheaper than processes and share data easily, which is also why they need careful synchronization (Phase 3). GATE asks what threads share and how user-level and kernel-level threads differ.
2 · Predict First
Try the questions first. Open when you are ready to check.
Two threads of one process both use a global variable count. One sets it to 5. Does the other see 5?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
Predict First — the answer
Yes. Threads of the same process share global data (unlike processes after fork, which get copies).
In plain words
A thread is one path of execution inside a process. All threads of a process share its code, its global data, its heap and its open files, but each thread has its own stack, registers and program counter (so each can be at a different line, inside different function calls).
1. Shared vs private
| Shared by all threads of a process | Private to each thread |
|---|---|
| code (text) | thread ID |
| global/static data | program counter |
| heap | registers |
| open files, signals | stack (local variables) |
2. Why threads?
- Cheaper than processes: creating a thread and switching between threads of the same process costs much less (no new address space).
- Sharing data is easy (same memory).
- Responsiveness and use of multiple CPU cores.
3. User-level vs kernel-level threads
| User-level threads | Kernel-level threads | |
|---|---|---|
| Managed by | a library in user space | the OS kernel |
| Kernel knows about them? | no (sees one process) | yes |
| Switching | very fast (no system call) | slower (kernel involved) |
| One thread blocks on I/O | the whole process blocks | only that thread blocks |
| Use several CPUs at once | no | yes |
4. Multithreading models (user threads → kernel threads)
- Many-to-one: many user threads on one kernel thread (one blocking call blocks all).
- One-to-one: each user thread has its own kernel thread (Linux, Windows); true parallelism, more overhead.
- Many-to-many: many user threads multiplexed onto a smaller or equal number of kernel threads.
4 · Visual
One process, three threads
+--------------------------------------------+
| code | global data | heap | files | <- shared by all threads
+--------------------------------------------+
| thread 1 thread 2 thread 3 |
| [stack 1] [stack 2] [stack 3] | <- private
| PC, regs PC, regs PC, regs |
+--------------------------------------------+
5 · Worked Examples
Example 1 Easy
Do two threads of a process share their local variables?
Solution
No: locals live on each thread's own stack.
Example 2 Medium
A process uses only user-level threads. One thread makes a blocking read. What happens to the other threads?
Solution
They all block: the kernel sees one process, which is now waiting.
Example 3 GATE
Which is cheaper: switching between two threads of the same process, or between two processes?
Solution
Between threads of the same process: no change of address space is needed.
6 · GATE Traps
- Threads share the heap and globals but not the stack or registers.
- User-level threads block together on a blocking system call.
- Kernel threads can run in parallel on multiple CPUs; many-to-one user threads cannot.
- fork copies memory; threads share it.
7 · Memory Hook
"Threads share the house, each has its own notebook (stack)."
8 · Connect the Dots
Threads share memory automatically. Separate processes don't, so they need inter-process communication: shared memory regions, messages or pipes, the topic of the next lesson.
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1 · MSQ · 2 mark(s) — Sharing
Which are shared by all threads of a process?
- (A) Global variables
- (B) Stack
- (C) Open files
- (D) Heap
Q2 · MCQ · 1 mark(s) — Private
Which does each thread have its own copy of?
- (A) Code
- (B) Heap
- (C) Open-file table
- (D) Program counter
Q3 · MCQ · 1 mark(s) — User-level threads
With purely user-level threads, a blocking system call by one thread:
- (A) crashes the process
- (B) blocks the whole process
- (C) blocks only that thread
- (D) is impossible
Q4 · MCQ · 1 mark(s) — Models
Linux maps each user thread to its own kernel thread. This is the:
- (A) many-to-many model
- (B) one-to-one model
- (C) many-to-one model
- (D) no-thread model
Q5 · MCQ · 1 mark(s) — Cost
Which is the main reason threads are cheaper to create than processes?
- (A) They share the existing address space.
- (B) They run in kernel mode.
- (C) They never make system calls.
- (D) They have no stack.
10 · Rule Card
- Thread = path of execution inside a process; shares code, data, heap, open files; private stack, registers, PC, thread ID.
- Threads are cheaper to create and switch than processes.
- User-level: fast, kernel unaware, one block blocks all, no parallelism. Kernel-level: kernel-scheduled, independent blocking, parallel on multiple cores.
- Models: many-to-one, one-to-one, many-to-many.
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
threadshared and private thread resourcesbenefits of threadsuser-level threadskernel-level threadsblocking behaviourmany-to-one, one-to-one and many-to-many models
12 · Answer Vault
Try the questions first. Open when you are ready to check.
🔐 Open the Answer Vault
Recall Warm-up
- No, they are copied.
- The stack.
- Hardware devices.
Self-Test
Q1 Answer: (A) (C) (D) · 2 mark(s)
Each thread has its own stack.
- (B) is wrong: Private.
Re-read: Lesson 1.2.1 — Threads: what they share, user-level vs kernel-level threads
Q2 Answer: (D) · 1 mark(s)
Each thread can be at a different instruction.
- (A) is wrong: Shared.
- (B) is wrong: Shared.
- (C) is wrong: Shared.
Re-read: Lesson 1.2.1 — Threads: what they share, user-level vs kernel-level threads
Q3 Answer: (B) · 1 mark(s)
The kernel sees only one schedulable entity.
- (A) is wrong: Nothing crashes.
- (C) is wrong: That is kernel-level behaviour.
- (D) is wrong: It is possible.
Re-read: Lesson 1.2.1 — Threads: what they share, user-level vs kernel-level threads
Q4 Answer: (B) · 1 mark(s)
One kernel thread per user thread.
- (A) is wrong: Multiplexed.
- (C) is wrong: Many user threads on one kernel thread.
- (D) is wrong: Not a model.
Re-read: Lesson 1.2.1 — Threads: what they share, user-level vs kernel-level threads
Q5 Answer: (A) · 1 mark(s)
No new memory map to build.
- (B) is wrong: Not in general.
- (C) is wrong: They may.
- (D) is wrong: Each thread has a stack.
Re-read: Lesson 1.2.1 — Threads: what they share, user-level vs kernel-level threads