$linuxjunkies
>

taskset(1)

Retrieve or set the CPU affinity (processor mask) of running processes.

UbuntuDebianFedoraArch

Synopsis

taskset [OPTION]... [mask] [pid | command [arg]...]

Description

taskset allows you to bind a process or process group to a specific CPU or set of CPUs. This is useful for performance tuning, ensuring a task runs on particular cores, or preventing cache thrashing by isolating workloads to dedicated processors.

You can either query the CPU affinity of an existing process by its PID, or set affinity for a new command. The CPU mask can be specified in hexadecimal (0xABCD) or as a comma-separated list of CPU numbers.

Common options

FlagWhat it does
-p, --pidQuery or set affinity of existing process (specify PID); useful without launching a new command
-c, --cpu-listSpecify CPUs as a comma-separated list (e.g., 0,2,4 or 0-3) instead of hexadecimal mask
-a, --all-tasksApply affinity to all tasks/threads of the process, not just the main thread
-e, --excludeExclude specified CPUs from the mask (inverse operation; used with -c)
-v, --verboseDisplay the affinity mask before executing the command
-h, --helpPrint help message and exit

Examples

Display the CPU affinity mask of process 1234

taskset -p 1234

Bind process 1234 to run only on CPUs 0, 2, and 4

taskset -p -c 0,2,4 1234

Launch myapp restricted to run on CPU core 0 only

taskset -c 0 ./myapp

Run stress-ng on cores 0-3 for 60 seconds

taskset -c 0-3 stress-ng --cpu 4 --timeout 60s

Set affinity of process 5678 to all CPUs (hex mask 0xff = first 8 cores)

taskset -p 0xff 5678

Bind all threads of process 4321 to CPUs 1 and 3

taskset -a -p -c 1,3 4321

Related commands