Technical skill

Linux thread models

How a program divides its work

Imagine a program that must keep its screen responsive while it loads a file. It can split the work into threads: one path handles the screen while another reads the file. A thread is a path of instructions running inside a program. Threads in the same process share much of its memory and open files, but each has its own execution state. The operating system's kernel decides when each schedulable thread gets processor time.

A threading model describes how threads made by an application or runtime connect to the threads the kernel can schedule. This connection affects whether work can run on several processors at once, whether one blocking operation stalls other work, and how much scheduling state the system must maintain. “User thread” and “kernel thread” refer to different layers of this arrangement; they are not always two models an application chooses between.

Compare three ways to map threads

In an N:1 model, a runtime manages many user-space threads but runs them on one kernel scheduling entity. Switching between those user threads can be inexpensive. If one makes a blocking system call, that single kernel entity can stop and keep the other user threads from running. They also cannot run on multiple CPUs at once unless the runtime creates more kernel entities.

In a 1:1 model, each application thread has its own kernel-schedulable thread. The kernel can schedule them separately, so different threads can run at the same time on different CPUs. Each also uses kernel-visible state and resources, so an application still needs to manage the number of threads and their stacks.

An M:N model runs many user-space threads over a pool of kernel threads. The runtime can schedule work in user space while the kernel schedules the pool, allowing parallel execution. That flexibility requires coordination between the runtime and kernel.

What modern Linux uses

Modern Linux systems using glibc use the Native POSIX Threads Library, or NPTL, for ordinary POSIX threads. It uses a 1:1 model: each POSIX thread maps to a kernel-schedulable entity. Modern glibc no longer supports the older LinuxThreads implementation. The POSIX threads API is the set of functions a program calls; the threading model describes how that API is implemented underneath.

Linux creates tasks with the clone system call and flags that say which resources they share. For threads, shared resources can include the process's address space (its memory map) and file descriptors (handles for open files). Threads share much process state but keep separate execution stacks and scheduling state.

On Linux, getpid() reports the thread-group ID, commonly shown as the process ID. Individual thread IDs appear under /proc/PID/task. On systems using procps, ps -L -p PID lists a process's threads; top can show them when configured. A busy thread can use a full CPU while other threads wait, and many blocked threads can point to a dependency that is stuck. These views help narrow a problem but do not, by themselves, explain its cause.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful