- LPU MCA Semester 1
- CAP366
- Unit 3
CAP366 · Unit 3: Process management
Threads and multithreading
14 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
A process with three threads calls fork() on a POSIX system. How many threads does the child have?
- Three, because fork() copies every thread of the process
- Zero, because the child is created without any thread
- Four, since the child gets the three threads plus a new main thread
- One, because only the calling thread is duplicated
Show the answer
D. In POSIX, fork() creates a child that contains only the calling thread, while the other threads are not duplicated.
Why not A: POSIX fork copies only the thread that called it.
Why not B: A process must have at least one thread to run.
Why not C: No extra main thread is added.
In the many-to-one threading model:
- Each user thread maps to its own kernel thread, so a blocking call stops that thread
- Kernel threads are not used; the library dispatches threads straight onto the CPU
- If one user thread makes a blocking system call, the whole process blocks
- Multiple threads of one process can run in parallel on many CPUs at once
Show the answer
C. Many user threads map to a single kernel thread, so no real parallelism and one blocking call blocks all.