In this article, we will discuss how to run any process on single core or set CPU affinity in Linux. We will also discuss the different ways to set CPU affinity in Linux. Let’s get started.

What is CPU affinity?

CPU affinity is a process scheduling policy that allows a process to run on a specific CPU core. It is also known as CPU pinning. It is used to run a process on a specific CPU core. It is useful when you want to run a process on a specific CPU core. For example, if you want to run a process on a specific CPU core, you can use the taskset command to set CPU affinity.

Why use CPU affinity?

CPU affinity is useful when you want to run a process on a specific CPU core. For example, if you want to run a process on a specific CPU core, you can use the taskset command to set CPU affinity.

There are the application like VLC which only user single core by default while playing video though you can change the number of core to use in VLC setting. Recently, I was working project related to video playback in bigger screen and while experimenting with processor and its uses, the result show that cpu’s with lower processing power can play video with great performance while using only one core of CPU. While playing multiple video at the same time it might affect the performance.

Taskset is used to set or retrieve the CPU affinity of a running process given its pid, or to launch a new command with a given CPU affinity. CPU affinity is a scheduler property that “bonds” a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs.

How to set CPU affinity in Linux using taskset command?

This command will run the program on 0th core of CPU.

taskset -c 0 program

To run the program on 0th and 1st core of CPU.

taskset -c 0,1 program

This will return current affinity mask.

taskset -p 1234