taskset: set or retrieve a process's CPU affinity
taskset [options] mask command [argument...]
taskset [options] -p [mask] pid
What it does
taskset shows or sets the CPUs a process may run on, as a hexadecimal mask (1 = CPU 0, 3 = CPUs 0 and 1) or with -c a list (0,2-3).
Options you will use
-p, --pid- operate on an existing PID instead of starting a command.
-c, --cpu-list- a CPU list instead of a mask.
-a, --all-tasks- every thread of the PID.
Examples
$ taskset -cp 1210which CPUs a process may use
$ sudo taskset -cp 0,1 1210let it use both CPUs again
Gotchas
- Pinning a multi-threaded process to one CPU makes that CPU its bottleneck: mpstat -P ALL shows one CPU at 100%.
- nproc follows the affinity.
Try taskset in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.