Defining affinity of a process to a given CPU core in Linux

CPU Affinity is a system management property that lets you manually route one or more specific processor cores to a particular process or program, learn more about this mechanism in the industry.

Affinity CPU

See how to assign a certain process to a certain processor core

We are talking this week with some friends of the Diolinux Facebook group and this question has been raised, whether it would be possible not only to change the priority of a particular process but also to assign a process to a particular processor core.

Basically what you need to have to be able to perform such an action to have an unrestricted multi-core processor by make or model, can be both Intel and AMD, though remember that the more cores your processor has the more options you have.

I will try to simplify a complicated thing

Normally the function of assigning a process to a core assigned to the operating system, going down a little more, kernel function and being even more specific a kernel process called "Scheduler", which will define the affinity of a process and assign it to a core, to understand what usually happens let's use an example:

Gimp When opening the GIMP image editing program The system will take care of assigning the GIMP process, when you open another program it will have a higher priority momentarily and will take the place of GIMP in this core, but as the system manages the processes intelligently it will play GIMP to another core, thus maintaining the performance of both programs.

If you manually assign GIMP to a particular core, using the same example above, when you open another program and it requires attention to the core where GIMP is possible for the GIMP process to stop until the processor "has time to speak" with him again ", ie in this case is not very advantageous.

Why would you do that then?

First, why can you! Secondly, it may be interesting for power management, you can use just one core to run certain programs and thus make your battery last longer.

In the end I think this subject is for knowledge rather than use, most often the best option to let the system work for you.

How the taskset works

To send a direct process to be executed by a given core or cores we will use the taskset, which is a feature that already comes bundled with the Linux kernel.

Ubuntu System Monitor

If you look at the application that comes with most systems, in the case of Ubuntu, the System Monitor where you can see the name of all processes, in the "Resources" tab you can see how many cores your processor has.

Processor Cores

There are two ways to use the tasksetby opening a program that was closed or by modifying a process that is already running, for example:

To direct an already open process to a processor core at the terminal we do this:

taskset -Praça 0 2432

Being that:

Blue: command name used to set the process to a particular CPU

Orange: the parameter using to modify an already opened program.

Black: the processor core number, 0 for the first core, 1 for the second, 2 for the third, and so on.

Red: Red is the PID number, if you look at the system monitor image above where you can show the processes in the PIDS column, this number is used in the example GIMP.

If you want to open a program already directing it to a certain core do so:

taskset -c 0 gimp

Since the first command is the same, "-c" serves to direct the process to a core, "0" once again the core number you want to send the program to, and "gimp" in the example, the program I want to open .

If you want to direct the process to two cores do this:

taskset -c 0.1 gimp

Note the "0.1" this indicates which cores will work with GIMP (in the example), the same can be done for the first option.

Basically that, if you have any questions leave it in the comments below.

————————————————– ————————————————– ———————- See any errors or would you like to add any suggestions to this article? Collaborate, click here.