taskset(1)
Retrieve or set the CPU affinity (processor mask) of running processes.
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
| Flag | What it does |
|---|---|
-p, --pid | Query or set affinity of existing process (specify PID); useful without launching a new command |
-c, --cpu-list | Specify CPUs as a comma-separated list (e.g., 0,2,4 or 0-3) instead of hexadecimal mask |
-a, --all-tasks | Apply affinity to all tasks/threads of the process, not just the main thread |
-e, --exclude | Exclude specified CPUs from the mask (inverse operation; used with -c) |
-v, --verbose | Display the affinity mask before executing the command |
-h, --help | Print help message and exit |
Examples
Display the CPU affinity mask of process 1234
taskset -p 1234Bind process 1234 to run only on CPUs 0, 2, and 4
taskset -p -c 0,2,4 1234Launch myapp restricted to run on CPU core 0 only
taskset -c 0 ./myappRun stress-ng on cores 0-3 for 60 seconds
taskset -c 0-3 stress-ng --cpu 4 --timeout 60sSet affinity of process 5678 to all CPUs (hex mask 0xff = first 8 cores)
taskset -p 0xff 5678Bind all threads of process 4321 to CPUs 1 and 3
taskset -a -p -c 1,3 4321