Sub topic 9
User-Level and Kernel-Level Thread Models
Prerequisites and the Basic Idea
Assume you know process address spaces, system calls, interrupts, and basic C pointers. A thread is an execution path inside a process. Threads in one process share its code, global data, and heap, but each thread has its own stack, registers, and program counter.
A thread model describes who creates, schedules, and switches between threads: a user-space library or the operating-system kernel.
User-Level Threads
A user-level thread is managed by a library running in user mode. The kernel sees only one process, or only the kernel threads underneath the library. Creating and switching threads is fast because it does not require a system call for every switch.
The main weakness is blocking. If one user thread performs a blocking system call, the kernel may block the entire process. Also, with a many-to-one model, only one thread can execute at a time, even on a multiprocessor machine.
Kernel-Level Threads
A kernel-level thread is known to the operating system. The kernel schedules each thread and saves its registers during a context switch. If one thread blocks, the kernel can schedule another thread in the same process.
Kernel-level threads can run in parallel on multiple CPU cores. Their disadvantages are higher creation and switching costs, because kernel data structures and mode transitions are involved.
The common one-to-one model maps each user-visible thread to one kernel thread. POSIX threads on modern Linux systems use this general approach.
#include <pthread.h>
#include <stdio.h>
static void *worker(void *argument) {
printf("worker thread: %s\n", (char *)argument);
return NULL;
}
int main(void) {
pthread_t thread;
if (pthread_create(&thread, NULL, worker, "running in parallel") != 0)
return 1;
pthread_join(thread, NULL);
return 0;
}Compile this program with cc program.c -pthread. pthread_join waits for the worker to finish.
Many-to-Many and Hybrid Models
A many-to-many model maps many user threads onto a smaller or equal number of kernel threads. The library can perform inexpensive user-level scheduling, while the kernel still permits parallel execution. A hybrid model may also bind selected user threads permanently to kernel threads when the application needs predictable behaviour.
| Model | Kernel sees | Main advantage | Main risk |
|---|---|---|---|
| Many-to-one | One thread per process | Very cheap switching | Blocking and no multicore parallelism |
| One-to-one | Every application thread | Parallelism and independent blocking | More kernel overhead |
| Many-to-many | A pool of kernel threads | Flexible balance | Complex implementation |
flowchart TD
A[Application threads] --> B{Thread model}
B --> C[Many-to-one]
B --> D[One-to-one]
B --> E[Many-to-many]
C --> F[One kernel-visible thread]
D --> G[One kernel thread per application thread]
E --> H[User threads mapped to a kernel-thread pool]caption: How application threads are mapped to kernel-visible threads alt: Three thread models map application threads to one kernel thread, one kernel thread each, or a pool of kernel threads.
Exercise
A process has four user-level threads mapped many-to-one. Can two of them execute simultaneously on two CPU cores?
Show answer
No. The kernel sees only one schedulable thread, so the process can use at most one core at a time.