1. Home
  2. Linux
  3. Set process affinity on linux

How to set process affinity on Linux

Process affinity (also known as CPU pinning) is the process of assigning running programs to a single thread (virtual core), rather than allowing it to run with all CPU threads. Setting process affinity is advantageous, as it will enable users to decide exactly how much resources a program uses.

In this guide, we’ll go over how to set the affinity of running programs on your Linux PC. We’ll also go over other ways you can limit system resources to programs on your Linux OS.

Finding your CPU information

Before we can go over how to pin individual programs to specific threads, we need to find out how many threads you have available on your Linux system. There are a few ways to find out this information. We will cover two ways.

The first way to find out your CPU thread count is with the nproc command. This command gives out a raw number of processors that are available for your Linux system to utilize.

To run the nproc command to determine how many threads you have available, you must open up a terminal window. To open up a terminal window, press Ctrl + Alt + T or Ctrl + Shift + T on the keyboard.

With the terminal window open, execute nproc.

nproc

You’ll notice after running the command; a number appears in the prompt. The number is the thread (virtual cores) count of your Linux system. To save this information for later, do the following.

nproc >> ~/cpu-count.txt

If the nproc command simply isn’t enough information for you, there is a better command-line tool that gives a whole lot more information about your CPU threads. It is called CPU Info. Here’s how to install it.

First, ensure you have a terminal window open. Then, enter the installation commands below that correspond with the Linux operating system you use.

Ubuntu

On Ubuntu Linux, you’ll be able to install CPU Info using the Apt package manager command below.

sudo apt install cpuinfo

Debian

Those on Debian Linux can get CPU Info up and running with the following Apt-get command.

sudo apt-get install cpuinfo

Arch Linux

Arch Linux can easily install the CPU Info tool on Linux using the following Pacman command in a terminal window.

sudo pacman -S python-py-cpuinfo

Fedora

Are you using Fedora Linux? Get your hands on CPU Info by executing the following Dnf command in a terminal.

sudo dnf install python3-cpuinfo

OpenSUSE

An OpenSUSE Linux user? Get CPU Info up and running with the Zypper command below.

sudo zypper install python3-py-cpuinfo

Once the CPU Info program is set up on your Linux PC, it is time to use it to find out CPU information so that we can determine exactly how many threads there are to work with.

Using the cpu-info command below, get a readout of your CPU.

Note: you may need to run cpuinfo rather than cpu-info if on Arch Linux, Fedora, or OpenSUSE Linux.

cpu-info

After running the command, you will see both a core count and a logical count. The logical count is the number of threads you have to work with. Logical information is the most important when it comes to this guide. Feel free to save the CPU information to a text file by running the following command.

cpu-info >> ~/cpu-count.txt

How to set process affinity on Linux

To set process affinity on your Linux PC, you will need to make use of the built-in program Taskset. Open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, from there, follow the step-by-step instructions below to learn how to affine a running process.

Step 1: Find the process ID of a running program by executing pidof, followed by the name of the app. For example, to find Thunderbird’s process ID, you’d run the example command below.

pidof thunderbird

Step 2: Take note of the app’s process ID. Then, plug it into the following taskset command example.

Note: you must change thread_number to the CPU thread you’d like to put the program on. You must also change process_id to the process ID found with pidof that you would like to affine with taskset.

sudo taskset -cp thread_number process_id &

Need to check on your newly affined program? Run taskset -p against the program’s process ID to confirm that it is running on the CPU thread you specified in step 2.

taskset -p process_id

Learn more about Taskset

To learn more about Taskset, you will need to read the manual. Execute the man taskset command.

man taskset

Running man taskset command will present you with a detailed manual all about the Taskset application. Look over it as it will help you understand how the app works. When done, press q to quit.