🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

taskset Command

Advanced Performance & Debugging man(1)

Set or retrieve CPU affinity of a process

👁 10 views 📅 Updated: Mar 15, 2026
SYNTAX
taskset [OPTION]... [MASK | -c LIST] [PID | CMD]

What Does taskset Do?

taskset sets or retrieves the CPU affinity of a process. CPU affinity determines which specific CPU cores a process is allowed to run on.

taskset is useful for isolating workloads to specific cores, preventing processes from migrating between cores (improving cache performance), and reserving cores for critical tasks.

CPU affinity is specified as a bitmask or CPU list. taskset can set affinity when launching a new process or change it for a running process.

Options & Flags

OptionDescriptionExample
-c Specify CPU list (easier than bitmask) taskset -c 0,1 ./program
-p Set affinity for running process taskset -cp 0-3 1234
MASK CPU bitmask taskset 0x3 ./program

Practical Examples

#1 Run on specific cores

Runs the task only on CPU cores 0 and 1.
$ taskset -c 0,1 ./cpu_intensive_task

#2 Check process affinity

Shows which cores nginx is allowed to run on.
$ taskset -cp $(pgrep nginx | head -1)
Output: pid 1234's current affinity list: 0-7

#3 Set for running process

Restricts process 1234 to cores 0 through 3.
$ sudo taskset -cp 0-3 1234
Output: pid 1234's current affinity list: 0-3

#4 Isolate to single core

Runs benchmark on core 7 only for consistent results.
$ taskset -c 7 ./benchmark

#5 Using bitmask

Runs on cores 0-3 (bitmask: 1111 = 0xF).
$ taskset 0xF ./program

Tips & Best Practices

Use -c for clarity: taskset -c 0,1,2 is clearer than bitmask 0x7. Use -c for CPU lists unless you need bitmask syntax.
Benchmarking: Pin benchmarks to a single core with taskset -c 0 for consistent, comparable results across runs.
Does not reserve cores: taskset limits WHERE a process can run. It does not prevent OTHER processes from using those cores. Use cgroups for isolation.

Frequently Asked Questions

How do I pin a process to specific CPU cores?
taskset -c 0,1 command runs the command on cores 0 and 1. For running processes: taskset -cp 0,1 PID.
Why would I set CPU affinity?
Better cache performance, isolating workloads, consistent benchmarks, and preventing scheduling jitter.
What is a CPU affinity mask?
A bitmask where each bit represents a CPU core. 0x3 = 0011 = cores 0,1. Use -c CPU_LIST for easier syntax.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →