Fundamentals 12 min read

Mastering CPU Affinity and Hyper‑Threading on Linux

This guide explains the concepts of Hyper‑Threading, physical vs logical CPUs, how to query CPU information on Linux, and provides detailed instructions for using the taskset command and related programming APIs to set and retrieve CPU affinity for processes and threads.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering CPU Affinity and Hyper‑Threading on Linux

Hyper‑Threading (HT) is Intel's technique that presents two logical cores for each physical core, allowing a single processor to execute multiple threads simultaneously and improve utilization. Physical CPUs refer to the actual chips installed on the motherboard, while logical CPUs are the threads exposed by HT.

Logical CPU count can be calculated as physical CPU count × cores per CPU × 2 (if HT is enabled). Example: a 4‑core CPU with HT yields 8 logical CPUs.

Linux stores CPU details in /proc/cpuinfo. Useful commands include:

Show physical CPU count: cat /proc/cpuinfo | grep "physical id" | sort -u | wc -l Show cores per physical CPU: cat /proc/cpuinfo | grep "cpu cores" | uniq Show logical CPU count: cat /proc/cpuinfo | grep "processor" | wc -l Show CPU model name: cat /proc/cpuinfo | grep "name" | cut -f2 -d: | uniq CPU affinity is a scheduler property that binds a process or thread to a specific set of CPUs, reducing migration and improving performance. Linux supports natural affinity, which tries to keep a task on the same CPU.

Affinity is expressed as a bitmask where each bit represents a CPU; a set bit means the task may run on that CPU. Common hexadecimal masks:

0x00000001  // CPU #0 only
0x00000003  // CPUs #0 and #1
0xFFFFFFFF  // All CPUs (up to #31)

The taskset command manipulates affinity:

# Run a command with a specific mask
taskset [-c] mask command [args]
# Show affinity of an existing PID
taskset -p pid
# Change affinity of an existing PID
taskset -p [-c] mask pid

Options include -a to affect all threads (TIDs) of a process, -c to specify CPUs as a list (e.g., 0,5,7,9-11), and standard -h / -V for help and version.

Programmatically, affinity is managed via the sched_setaffinity and sched_getaffinity syscalls, as well as pthread-specific functions ( pthread_setaffinity_np, pthread_getaffinity_np, etc.). The API provides macros such as CPU_ZERO, CPU_SET, CPU_CLR, and their _S variants for dynamically allocated CPU sets (using CPU_ALLOC and CPU_ALLOC_SIZE).

When the first argument pid is 0, the calls affect the calling thread itself. The same interfaces can be used for both processes and individual threads, with the -a option of taskset exposing the TID concept.

Source code for the original taskset utility can be found in the util‑linux package (e.g.,

ftp://ftp.kernel.org/pub/linux/utils/util-linux/vX.YZ/util-linux-X.YZ-xxx.tar.gz

).

Linuxmultithreadingcpu_affinitytasksetsched_setaffinityproc cpuinfo
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.