Kernel thread

On this page 7

A kernel thread is a thread of execution that the operating system kernel creates, schedules and manages directly, so that the kernel scheduler sees it as an independent unit of CPU work. In the field of processes & scheduling within operating systems, the term has two closely related uses: a kernel-supported thread that runs user code on behalf of an application, and a kernel-only worker thread that runs entirely in kernel mode to perform housekeeping such as flushing dirty pages or handling deferred interrupt work.

Both uses share one property that defines the concept: the kernel keeps a per-thread control structure, a dedicated kernel stack and saved register state for each thread, which allows it to block, preempt and migrate threads individually across processor cores.

How the kernel represents and runs a kernel thread

The kernel represents each kernel thread with a thread control structure that records its register context, scheduling priority, CPU affinity, signal mask and current state. In Linux this structure is task_struct, the same type used for processes; in Windows it is the ETHREAD and KTHREAD pair, linked to an owning EPROCESS. Threads of one process share the address space, open file handles and credentials, while each keeps its own stack and registers.

When a thread blocks on I/O, a lock or a timer, the kernel marks it as waiting and places it on a wait queue. The scheduler then picks another runnable thread from the run queue, performs a context switch by saving and restoring registers, and resumes the chosen thread. Because the kernel knows about every thread, one blocked thread never stalls its siblings, and several threads of the same process can run truly in parallel on different cores.

The following table lists the main elements the kernel keeps for every kernel thread and what each one controls.

ElementTypical contentPurpose
Kernel stack8-16 KiB on Linux, 12-24 KiB on Windows x64Holds call frames during system calls and interrupts
Register contextInstruction pointer, stack pointer, general and vector registersAllows the thread to resume exactly where it stopped
Scheduling dataPriority, policy, time slice, virtual runtimeDetermines when and how long the thread runs
Affinity maskSet of permitted CPU coresRestricts or pins the thread to specific processors
Thread stateRunning, ready, waiting, stoppedTells the scheduler whether the thread can be selected
Thread-local storage pointerBase address of per-thread dataGives each thread private variables in a shared address space
Per-thread state kept by the kernel

Kernel-supported threads and kernel-only worker threads

The two meanings differ mainly in which address space the thread executes. A kernel-supported thread created through pthread_create on Linux or CreateThread on Windows spends most of its time in user mode and enters kernel mode only for system calls, page faults and interrupts. A kernel-only worker thread has no user-space memory map at all and borrows the kernel portion of whichever address space was last active.

Linux kernel-only threads are started with kthread_create and appear in process listings with names in square brackets, such as [kworker/0:1], [ksoftirqd/2] or [kswapd0]. Windows runs comparable worker threads inside the System process, which has process ID 4. The table below contrasts the two forms.

PropertyKernel-supported application threadKernel-only worker thread
Execution modeMostly user mode, enters kernel on demandAlways kernel mode
User address spaceShares the process address spaceNone
Created byApplication through a threading APIKernel subsystems and drivers
Typical examplesWeb server request workers, game render threadsPage writeback, softirq processing, RCU callbacks
Can be killed by usersYes, with its processGenerally no
Comparison of the two forms of kernel thread

Kernel threads compared with user-level threads

The essential contrast with a user-level thread is visibility: a user-level thread exists only inside a runtime library, while a kernel thread is known to the kernel scheduler. This visibility explains every trade-off between the two models, including the cost of creation and switching and the behavior during blocking calls.

Threading models describe how user-level threads map onto kernel threads. The one-to-one model, used by modern Linux (NPTL), Windows and macOS, gives every application thread its own kernel thread. The many-to-one model multiplexes many user threads onto one kernel thread, and the many-to-many model, also called M:N, maps many user threads onto a smaller pool of kernel threads, which is the approach of the Go runtime scheduler.

ModelMappingParallelismBlocking behavior
One-to-oneEach user thread has one kernel threadFull multicore parallelismOnly the calling thread blocks
Many-to-oneAll user threads share one kernel threadNoneOne blocking call stalls every thread
Many-to-manyN user threads on M kernel threadsUp to M threads in parallelRuntime moves work to other kernel threads
Threading models and their consequences

Kernel threads carry a higher creation and switching cost: creation typically takes several microseconds to tens of microseconds, and a context switch costs roughly one to a few microseconds including cache and TLB effects, depending on hardware and mitigations. User-level switches can be ten times cheaper, which is why runtimes with very large numbers of lightweight tasks multiplex them onto a small kernel thread pool.

Scheduling, priorities and practical configuration

Kernel threads are scheduled under the same preemptive multitasking policies as processes. On Linux, normal threads use the fair scheduling class (CFS, and EEVDF since kernel 6.6), while threads with SCHED_FIFO, SCHED_RR or SCHED_DEADLINE receive real-time priority and preempt normal work. Windows uses 32 priority levels, with levels 16-31 reserved for real-time threads.

Practitioners tune kernel threads through priority, affinity and pool size. The decision table below summarizes common situations.

SituationRecommended choiceReason
CPU-bound parallel computationOne thread per physical coreMore threads add switching without extra throughput
I/O-bound server with blocking callsLarger pool or asynchronous I/OBlocked threads leave cores idle
Latency-sensitive audio or control loopReal-time policy with pinned affinityAvoids preemption by ordinary work
Interrupt-heavy network hostSpread softirq threads across coresPrevents one core from saturating
Configuration choices for kernel threads

Limitations and common mistakes

Kernel threads are powerful but not free, and most problems come from creating too many or prioritizing them carelessly. Each thread consumes kernel memory for its stack and control structure, plus a user stack that often reserves 8 MiB of virtual memory on Linux by default.

  • Oversubscription: thousands of runnable threads on a few cores waste time in context switches and degrade cache locality.
  • Priority inversion: a high-priority thread waits for a lock held by a low-priority one; priority inheritance in the kernel mitigates this.
  • Unbounded real-time threads: a runaway SCHED_FIFO thread can starve the system; Linux reserves about 5 percent of CPU time for normal tasks by default.
  • Confusing names with ownership: a busy [kworker] thread usually reflects work queued by a driver or filesystem, not a fault in the worker itself.

In plain words

A kernel thread is a worker the operating system knows by name. The system keeps a small file on each worker: where it stopped, how important it is and which processor it may use. Because of that file, the system can pause one worker and let another run without losing anyone’s place.

Think of a restaurant kitchen where the head chef assigns every cook personally. If one cook waits for an oven, the chef simply gives the next task to another cook. The key point is that the operating system, not the program, decides who works next, so one stuck worker does not stop the rest.

A real-world example

A systems administrator runs a database server with 16 physical cores on Linux. During nightly batch imports, monitoring shows the CPU at only 40 percent while query latency doubles. Inspection with top -H reveals that the database runs 400 threads, most of them blocked on disk I/O, and that [kswapd0] is unusually busy because memory pressure forces page reclaim.

The administrator reduces the import worker pool from 64 to 16 threads, switches the import to asynchronous I/O and adds memory so the kernel reclaim thread no longer runs constantly. The result is CPU utilization near 85 percent and query latency back to normal, because fewer kernel threads now compete for cores and none of them waits needlessly.