1. LPU MCA Semester 1
  2. CAP366
  3. 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.

ModelMappingNote
Many-to-oneMany user threads to one kernel threadOne blocking call blocks all; no parallelism
One-to-oneEach user thread to a kernel threadParallel; cost limits count (Linux, Windows)
Many-to-manyMany user to fewer kernel threadsParallel 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

Question 1 · Tier 5, Exam level

A process with three threads calls fork() on a POSIX system. How many threads does the child have?

  1. Three, because fork() copies every thread of the process
  2. Zero, because the child is created without any thread
  3. Four, since the child gets the three threads plus a new main thread
  4. 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.

Question 2 · Tier 2, Understand

In the many-to-one threading model:

  1. Each user thread maps to its own kernel thread, so a blocking call stops that thread
  2. Kernel threads are not used; the library dispatches threads straight onto the CPU
  3. If one user thread makes a blocking system call, the whole process blocks
  4. 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.