Install taskset on Linux
The taskset tool is part of « util-linux » package in Linux, and most Linux distros come with the package pre-installed by default. If taskset is not available on your Linux system, install it as follows.
To install taskset on Debian, Ubuntu or Linux Mint:
$ sudo apt-get install util-linux
To install taskset on Fedora, CentOS or RHEL:
$ sudo yum install util-linux
View the CPU Affinity of a Running Process
To retrieve the CPU affinity information of a process, use the following format. taskset returns the current CPU affinity in a hexadecimal bitmask format.
taskset -p <PID>
For example, to check the CPU affinity of a process with PID 2915:
$ taskset -p 2915
pid 2915's current affinity mask: ff
In this example, the returned affinity (represented in a hexadecimal bitmask) corresponds to « 11111111 » in binary format, which means the process can run on any of eight different CPU cores (from 0 to 7).
The lowest bit in a hexadecimal bitmask corresponds to core ID 0, the second lowest bit from the right to core ID 1, the third lowest bit to core ID 2, etc. So for example, a CPU affinity « 0x11 » represents CPU core 0 and 4.
taskset can show CPU affinity as a list of processors instead of a bitmask, which is easier to read. To use this format, run taskset with « -c » option. For example:
$ taskset -cp 2915
pid 2915's current affinity list: 0-7
Pin a Running Process to Particular CPU Core(s)
Using taskset, you can « pin » (or assign) a running process to particular CPU core(s). For that, use the following format.
$ taskset -p <COREMASK> <PID> $ taskset -cp <CORE-LIST> <PID>
For example, to assign a process to CPU core 0 and 4, do the following.
$ taskset -p 0x11 9030
pid 9030's current affinity mask: ff pid 9030's new affinity mask: 11
Or equivalently:
$ taskset -cp 0,4 9030