Whenever you run any application on your computer, it creates an instance of that application, called a process. And one process may contain multiple threads.
These process and threads get executed on CPU cores.
If you are reading this article, you might be already aware of the process and threads in the operating system.
There is one more term very important to know about the execution of the program- CPU affinity.
The CPU affinity is a setting provided by the operating system. It helps to reserve particular core of CPU for a particular thread to be executed on.
If you don’t set affinity, a thread can be run on any core of CPU. So it will be helpful as it doesn’t wait for a particular core of CPU to be available. Threads get to run on any of the core whichever available at that time.
It chooses the best CPU core to be used to execute a particular thread. Sometimes, it denies some threads to execute on some CPU cores.
This is also called as Process Affinity or Processor Affinity.
In certain cases, it is good to set CPU affinity.
For instance, there are two threads which use cache intensively. So to prevent cache coherence problem, it is preferable to run these two threads on separate cores that don’t share the common cache.
Suppose particular cache is shared by two cores. If a cache-intensive thread is running on two cores, execution of both threads on two cores can depend on the availability of shared cache resource. So it is preferred to use these two threads on two cores that do not share common CPU.
Like a cache, the same situation can have occurred for other shared resources like memory banks, IO devices…
To be on the safer side, CPU affinity is used.
In case if there are multiple “hungrier” threads are running. If the important thread comes, Affinity is useful to set and reserve a core.
Affinity helps to ensure that thread always runs on the “best” core.
Sometimes it is worth to deny a process to run on the particular core of CPUs or CPUs itself.
In this case, even if the CPU core is available to run the process, it will not run. The Process even will not be put on the ready list of denied CPUs.
Many times it is good to wait for particular CPUs and get the work done well.
Setting Affinity is nothing but scheduling a process on a certain subset of CPUs.
Setting CPU affinity is also called as CPU pinning.
This setting will allow running any of your application process on the selected CPU core.
Follow the simple steps.
Note: All the screenshots has taken on Windows 8.1 OS with core 2 duo processor. Similar steps you have to follow for Windows 7 or Windows 10.
If you have any questions for Setting CPU Affinity, feel free to write a query in the comment section below. Keep learning!