Sub topic 9
User-Level and Kernel-Level Thread Models
This chapter assumes you know what a thread shares with its process (code, heap, globals, open files) and what it keeps private (stack, registers, program counter). It also assumes you know what a system call and a mode switch are. Here the question is who creates and schedules threads: a library inside the process, or the kernel.
Two places a thread can live
A user-level thread (ULT) is managed by a thread library in user space. The kernel does not know it exists. The kernel sees one process and schedules that process as a single unit.
A kernel-level thread (KLT) is known to the kernel. The kernel keeps a record for each thread and schedules each one on a CPU. Windows and modern Linux work this way.
| Property | User-level thread | Kernel-level thread |
|---|---|---|
| Managed by | Library in user space | Operating system kernel |
| Create or switch | Ordinary function calls, no system call | System call and mode switch needed |
| Switch cost | Small: save and restore registers and stack pointer | Larger: trap into the kernel, possibly a full scheduler pass |
| Kernel awareness | None | Full |
| Runs on several CPUs at once | No, the process is one schedulable unit | Yes |
| Blocking system call | Blocks every thread in the process | Blocks only the calling thread |
| Scheduling policy | Chosen by the library, can be application-specific | Chosen by the kernel |
| Portability | Runs on a kernel with no thread support | Needs kernel support |
Why a user-level switch is cheap
A user-level switch never enters the kernel. The library saves the current thread's registers into a thread table kept in user memory, picks another thread, and restores its registers. There is no trap, no mode change and no change of address space. A kernel-level switch between threads of the same process also skips the address-space change, but it still pays for the trap and the scheduler run.
The price is that the kernel cannot help. Two problems follow.
- One blocking call stops everything. If a ULT calls
readon a slow device, the kernel blocks the whole process, because it sees only one flow of execution. All sibling threads stop even though they could run. - No parallelism. The kernel gives a process one CPU at a time, so threads of one process cannot run simultaneously on a multiprocessor.
Mapping models
When both kinds exist, the system must map user threads onto kernel threads. There are three classic models.
Many-to-one
Many user threads map to one kernel thread. This is the pure user-level model. Creation and switching are fast, but a blocking call stops all threads, and there is no multiprocessor parallelism. Early Solaris green threads and some language runtimes used it.
One-to-one
Each user thread has its own kernel thread. A blocking call stops only that thread, and threads run in parallel on several CPUs. The cost is that every thread creation needs a system call and a kernel thread, which uses kernel memory. Systems therefore often limit the number of threads. Linux (NPTL) and Windows use this model.
Many-to-many
Many user threads are multiplexed onto a smaller or equal number of kernel threads. The application can create as many user threads as it wants. The kernel threads give real parallelism, and when one user thread blocks, the kernel can run another kernel thread. The weakness is complexity: the library and the kernel must cooperate on scheduling.
A two-level model is many-to-many plus the option to bind a chosen user thread to its own kernel thread. This is the hybrid model in the spine.
What the hybrid model needs
Because the kernel is unaware of user threads, the two schedulers can disagree. Two mechanisms help:
- Lightweight processes (LWPs). An LWP is an intermediate structure that looks like a virtual processor to the user thread library. The library schedules user threads onto LWPs, and the kernel schedules the kernel thread behind each LWP.
- Scheduler activations. The kernel makes an upcall to the library when an event occurs, such as a thread about to block. The library then moves the other user threads to a different LWP.
Checking thread scope in code
POSIX lets a program ask for a scheduling scope. PTHREAD_SCOPE_SYSTEM means the thread competes with all threads in the system, so it behaves as a kernel-scheduled thread. PTHREAD_SCOPE_PROCESS means it competes only inside its process. Linux supports only the system scope.
#include <stdio.h>
#include <pthread.h>
static void *work(void *arg) {
printf("thread %d running\n", *(int *)arg);
return NULL;
}
int main(void) {
pthread_attr_t attr;
int scope;
pthread_attr_init(&attr);
pthread_attr_getscope(&attr, &scope);
printf("default scope: %s\n",
scope == PTHREAD_SCOPE_SYSTEM ? "system" : "process");
if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) != 0) {
fprintf(stderr, "system scope not supported\n");
return 1;
}
pthread_t tid[3];
int ids[3] = {0, 1, 2};
for (int i = 0; i < 3; i++) {
pthread_create(&tid[i], &attr, work, &ids[i]);
}
for (int i = 0; i < 3; i++) {
pthread_join(tid[i], NULL);
}
pthread_attr_destroy(&attr);
return 0;
}Compile with gcc file.c -pthread. On Linux the default scope prints system. The three lines from the threads can appear in any order.
Common errors
- Saying ULT switching needs no cost at all. It avoids the trap, but it still saves and restores registers and the stack pointer.
- Assuming a multicore machine speeds up many-to-one. All user threads share one kernel thread, so only one runs at a time.
- Forgetting that a page fault or blocking I/O also blocks a whole ULT process. The kernel sees the process, not the thread.
- Mixing up the thread model with the programming interface. The
pthreadAPI can sit on top of any of the models above. - Thinking the kernel schedules ULTs. The kernel schedules the process or the kernel thread. The library schedules the ULTs.
Choosing a model
Use these criteria when comparing models in a question.
- If the question stresses parallel execution on multiple CPUs, the answer needs kernel threads (one-to-one or many-to-many).
- If it stresses cheap creation or switching, user-level threads are favoured.
- If one thread performs blocking I/O and the others must keep running, ULTs in a many-to-one model fail.
- If the number of threads can be very large, one-to-one may exhaust kernel resources, and many-to-many scales better.
Exercise
A process has 4 user-level threads in a many-to-one model on a machine with 4 CPUs. One thread makes a blocking read system call. How many of the 4 threads can make progress while the read waits, and what is the maximum number running at the same instant when no thread is blocked?
Show answer
None can make progress while the read waits, because the kernel sees one thread of control and blocks the whole process. When no thread is blocked, at most 1 thread runs at any instant, because the single kernel thread can occupy only one CPU.
Exercise
A system uses the one-to-one model. Give one advantage and one disadvantage compared with many-to-one.
Show answer
Advantage: a blocking call in one thread does not block the others, and threads can run in parallel on several CPUs. Disadvantage: creating each thread needs a system call and a kernel thread, so thread creation is more expensive and the number of threads is usually limited.