CPU Affinity Calculator
Calculate CPU affinity masks for pinning processes to specific cores with taskset.
About CPU Affinity Calculator
A CPU affinity mask is a bitmap where bit N set to 1 means the process/thread is allowed to run on core N — sched_setaffinity and the taskset command both accept this as a hexadecimal value. This calculator builds that bitmap from a human-readable list of core indices (as you'd read from `lscpu -e` or `nproc`), producing both the raw hex mask and a ready-to-use taskset command, useful for pinning latency-sensitive processes to specific cores (avoiding cross-core cache-line bouncing) or isolating a workload away from cores handling interrupts.
Formula
CPU mask construction
mask = OR over selected cores of (1 << core_index)
- S
- set of selected core indices
- i
- a core index in S
Worked example
With the default values below, CPU Affinity Calculator returns Hexadecimal CPU Mask of 0xf. Change any input above and every figure here is recalculated in your browser.
Inputs used
- Total CPU Cores on Systemaccepts 1–256
- 16
- Cores to Bind To (comma-separated, 0-indexed)
- 0,1,2,3
Results
- Hexadecimal CPU Mask
- 0xf
- taskset Command (core list)
- taskset -c 0,1,2,3 <command>
- Binary Mask (bit N = core N)
- 0000000000001111
- Cores Selected
- 4
- taskset Command (hex mask)
- taskset 0xf <command>
Step by step
- 1Values used
- 2CPU mask construction
- 3Hexadecimal CPU Mask= 0xf
- 4taskset Command (core list)= taskset -c 0,1,2,3 <command>
- 5Binary Mask (bit N = core N)= 0000000000001111
- 6Cores Selected= 4
- 7taskset Command (hex mask)= taskset 0xf <command>
Assumptions and limits
- The result is derived only from the 2 inputs listed above. Anything not among them is not part of the calculation.
- In particular, this calculator does not take inflation, tax, fees or charges or compounding frequency into account.
- Figures are rounded for display to whole numbers, so totals built from several rounded parts can differ slightly from the unrounded arithmetic.
How these calculators are built and tested is described on the Methodology page. If a figure looks wrong, please report it.
Frequently Asked Questions
Why pin a process to specific CPU cores at all?
Pinning avoids the scheduler migrating a process between cores, which causes CPU cache misses (each migration cold-starts the process's data in the new core's L1/L2 cache) and can hurt latency-sensitive or cache-sensitive workloads. It's also used to isolate a workload from cores that handle high interrupt load, or to dedicate cores exclusively to a real-time process.
How do I apply a CPU mask to a running process?
`taskset -p <mask_or_core_list> <pid>` changes affinity for an already-running process, while `taskset <mask_or_core_list> <command>` launches a new process directly with that affinity. Both accept either the hex mask or a comma/range core list (e.g. `0-3,8`).
Does CPU affinity guarantee exclusive use of those cores?
No — it only restricts which cores the process is allowed to run on; other processes (and the kernel) can still schedule work on those same cores unless you also use cgroups/cpuset isolation or kernel boot parameters like isolcpus to reserve cores exclusively.
