4.2.7Operating Systems

Threads — user-level vs kernel-level, one-to-one, many-to-many

1,951 words9 min readdifficulty · medium

WHY do we even want threads?


WHAT is a thread, precisely?


The core distinction: WHO manages the thread?

Property User-level (ULT) Kernel-level (KLT)
Managed by user library OS kernel
Context switch cost very cheap (no trap) costly (mode switch)
Blocking call blocks... whole process only that thread
True multicore parallelism ❌ (1 kernel slot)
Portable across OS ❌ (OS-specific)

Mapping models: connecting ULTs to KLTs

The library may map user threads onto kernel threads in three ways.

Figure — Threads — user-level vs kernel-level, one-to-one, many-to-many

Common Mistakes (Steel-manned)


Flashcards

What is the unit of CPU scheduling in a threaded process?
The thread (each has its own PC, registers, stack).
What do sibling threads share vs keep private?
Share: code, data/heap, open files, address space. Private: stack, registers, PC, thread state.
Who schedules user-level threads?
A user-space thread library; the kernel only sees one process.
In Many-to-One, what happens when one thread makes a blocking system call?
The entire process blocks (kernel sees only one schedulable entity).
Why are user-level thread switches cheaper than kernel-level ones?
No mode switch / trap into the kernel — switching is just user-space bookkeeping.
Which mapping model gives true multicore parallelism with least kernel overhead per user thread?
Many-to-Many (M:N).
Which model do modern Linux pthreads and Windows use?
One-to-One (1:1).
In M:N with m user and n kernel threads, what is the constraint on n?
nmn \le m, and ideally nn \ge number of cores.
Main drawback of One-to-One?
One kernel thread (TCB) per user thread → kernel overhead limits thread count.
Why do threads need synchronization but separate processes mostly don't?
Threads share memory (heap/globals) → race conditions.

Recall Feynman: explain to a 12-year-old

Imagine a kitchen (the process). Cooks are threads — they all share the same fridge, counter, and ingredients (shared memory), but each cook has their own little chopping board (stack). Now, who tells cooks when to work?

  • If the head chef inside the kitchen does it (user-level), the manager of the whole restaurant (kernel) thinks the kitchen is just one worker. So if one cook goes to the freezer in the basement and gets stuck (a blocking call), the manager thinks the whole kitchen stopped and gives the stove to a different kitchen — even though other cooks were ready!
  • If the restaurant manager schedules each cook directly (kernel-level), only the stuck cook waits; the rest keep cooking — and they can use many stoves at once (cores). But asking the manager for every little thing is slow.
  • Many-to-Many: have a few cooks the manager knows about, and let extra helpers share those slots — fast and nobody freezes the whole kitchen.

Connections

  • Process vs Thread
  • Context Switching
  • CPU Scheduling
  • Race Conditions and Synchronization
  • System Calls and Mode Switch
  • Goroutines and M-N Scheduling
  • TLB and Address Space Switching

Concept Map

contains

owns private

shares

is unit of

managed by whom?

user library

OS kernel

blocking call blocks

cheap switch no trap

blocks only

mode switch

mapped via M:1

mapped via

Process shared space

Thread

Stack PC registers

Heap code files

CPU scheduling

Who schedules

User-level thread

Kernel-level thread

Whole process

Fast but no parallelism

That one thread

Costly but true parallelism

Many-to-One

One-to-One Many-to-Many

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek process ek ghar jaisa hai aur threads us ghar ke andar ke workers hain. Saare workers same fridge, same saaman (address space, code, data, files) share karte hain, par har worker ka apna chopping board hota hai — yaani apna stack aur registers. Isi sharing ki wajah se threads banana aur switch karna sasta hota hai, aur multi-core CPU par ye sach much parallel chal sakte hain.

Asli sawal yeh hai: in workers ko schedule kaun karta hai? Agar user-level library karti hai (user-level threads), to kernel ko lagta hai poori process bas ek hi banda hai. Problem yeh: agar ek thread ne blocking system call (jaise read()) maari, to kernel poori process ko block kar deta hai — baaki ready threads bhi atak jaate hain, aur multi-core ka fayda bhi nahi milta. Agar kernel schedule karta hai (kernel-level threads), to sirf wahi thread blocks hota hai, baaki chalte rehte hain, aur true parallelism milta hai — par har create/switch ek system call hai, thoda slow.

Mapping ke teen models yaad rakho: Many-to-One = bahut user threads ek kernel thread par → ek block karega to sab freeze, no parallelism. One-to-One = har user thread ka apna kernel thread → fast aur parallel, par kernel par bojh (Linux/Windows yahi use karte hain). Many-to-Many = m user threads ko n (n ≤ m) kernel threads par map karo → sasta bhi aur parallel bhi, best balance (jaise Go goroutines).

Mantra: "M1 freezes, 1:1 flies, MN balances." Aur galti se mat sochna ki threads ke alag address space hote hain — nahi, sirf stack aur registers private hote hain, baaki sab shared, isliye locks ki zaroorat padti hai.

Test yourself — Operating Systems

Connections