• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2024
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

What is and How does Setting CPU Affinity Improve Performance?

Aniruddha Chaudhari/34551/0
COACSE SubjectOperating System

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.

What is 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.

Why does setting CPU Affinity require?

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.

How to use and setting CPU Affinity in Windows OS?

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.

  • Go to the Task Manager.
  • Click on the Processes tab.
  • Select and right click on the process that you want for Setting CPU Affinity.
  • Click on Go to details.Setting CPU Affinity Go to detail
  • Detail of the process will be displayed.
  • Select the process for setting CPU affinity. You can differentiate different processes using different process ID (PID).Setting CPU Affinity Set Affinity
  • You can see the list of available CPUs for setting CPU Affinity.
  • As I am using the Core 2 Duo processor, there are 4 processors are available. By default, all the processes will be selected. It indicates the process can run on any of the available processors.Setting CPU Affinity for process

 

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!

CPUCPU Affinity
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Interview Questions



You can share your interview experience.

Subscribe for FREE Newsletter

Do you want me to send you programing updates for FREE?

Subscribe below…

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard