CAP366 · Unit 3: Process management
Threads and multithreading
16 practice questions · in the MTE and ETE syllabus
Key points
- Threads share code, data and files
- Each thread has own stack and registers
- Many-to-one: blocking call blocks the process
- Amdahl: 1 / (S + (1 - S) / N)
A thread is a unit of CPU execution inside a process. Threads of one process share code, global data and open files; each has its own stack, registers and program counter.
Benefits: responsiveness, cheap resource sharing, cheaper creation and switching than processes, and use of multiple cores.
| Model | Mapping | Note |
|---|---|---|
| Many-to-one | Many user threads to one kernel thread | One blocking call blocks all; no parallelism |
| One-to-one | Each user thread to a kernel thread | Parallel; cost limits count (Linux, Windows) |
| Many-to-many | Many user to fewer kernel threads | Parallel without one kernel thread each |
Amdahl's law: speedup <= 1 / (S + (1 - S) / N). With 60 percent parallel code (S = 0.4) on 3 cores: 1 / (0.4 + 0.2) = 1.67.
Exam pitfall: shared data needs synchronization; counter++ by two threads can lose updates because it is a load, an add and a store.
Try two questions
In Windows, the basic unit that the kernel scheduler dispatches to a processor is the:
- Thread
- Process
- Fiber
- Job object
Show the answer
A. A Windows process is a container for resources; its threads are what the scheduler runs. Fibers are scheduled by the application in user mode and job objects group processes.
Why not B: A process only holds resources; its threads are scheduled.
Why not C: Fibers are scheduled by the application in user mode.
Why not D: A job object groups processes for management.
In Linux, threads are implemented as:
- Library-only user threads that the kernel cannot see or schedule
- Tasks created with clone() that share resources such as the address space with their parent
- Hardware contexts assigned directly by the BIOS at boot time
- Separate processes that are never allowed to share any memory
Show the answer
B. Linux treats a thread as a task created with clone() and chosen sharing flags, so the kernel schedules threads and processes with the same mechanism.
Why not A: Linux threads are kernel-visible and scheduled by the kernel.
Why not C: The BIOS plays no part in creating threads.
Why not D: Threads exist to share memory, such as the address space.