ArticleslgStudy

computer science

Processor affinity

Processor affinity is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Processor affinity rather than just read about it. In short: In computer science, processor affinity, also called CPU pinning or cache affinity, enables the binding and unbinding of a process or a thread to a central processing unit (CPU) or a range of CPUs, so that the process or thread will execute only on the designated CPU or CPUs rather than any CPU. This can be viewed as a modification of the native central queue scheduling algorithm in a symmetric multiprocessing opera…

Key takeaways

  • Processor affinity belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Processor affinity to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Processor affinity from memory before moving on to harder problems.

Reference excerpt

In computer science, processor affinity, also called CPU pinning or cache affinity, enables the binding and unbinding of a process or a thread to a central processing unit (CPU) or a range of CPUs, so that the process or thread will execute only on the designated CPU or CPUs rather than any CPU. This can be viewed as a modification of the native central queue scheduling algorithm in a symmetric multiprocessing operating system. Each item in the queue has a tag indicating its kin processor. At the time of resource allocation, each task is allocated to its kin processor in preference to others. Scheduling-algorithm implementations vary in adherence to processor affinity. Under certain circumstances, some implementations will allow a task to change to another processor if it results in higher efficiency. For example, when two processor-intensive tasks (A and B) have affinity to one processor while another processor remains unused, many schedulers will shift task B to the second processor in order to maximize processor use. Task B will then acquire affinity with the second processor, while task A will continue to have affinity with the original processor. On most operating systems, the set of processors a process or thread is allowed (or preferred) to run on is expressed as an affinity mask, which is a bit mask corresponding to the system's cores.

Purpose There are several reasons for processor affinity to be used.

Locality of cache and memory The execution of a thread may be interrupted by the OS scheduler to make space for other programs or threads during an interrupt. If the thread was later dispatched to the processor it was previously running on, there may be some remaining data in the CPU cache that can be reused, allowing for fewer cache misses. Setting the processor affinity would make sure the thread always runs on the same processor(s), but at the same time forces it to wait for the processor(s) to become available again. This feature is especially useful for CPU-intensive processes with few interrupts. Doing the same to an ordinary program might instead slow it down as they tend to be interrupted more frequently and end up waiting more. A practical example of processor affinity is executing multiple instances of a single-threaded application, such as some graphics-rendering software. On CPUs with simultaneous multi-threading (SMT, also loosely known as hyper-threading, a genericized trademark of Intel), the two or more "threads" (logical processors, "virtual cores") on a physical core share the L1 and L2 caches. As far as affinity for locality purposes is concerned, they are identical. On non-uniform memory access (NUMA) systems a similar problem exists, except the latency comes not from L1/L2 cache misses, but from L3 misses and cross-node memory access. Constraining all threads of a program to the same NUMA node (or at least the same CPU socket) would ensure they can share their L3 caches. Additional configuration may be necessary to ensure that memory is allocated from the local NUMA node.

Division of resources Processor affinity also enforces a static division of processing resources. As a result, it can be used to limit the amount of CPU cores used by a CPU-intensive process, leaving other cores available for the other programs to use. This is, of course, not optimal, as it would leave resources completely unused when there are no other programs running as well as still allow other programs to compete with the CPU-intensive program for resources on the few cores it's allowed to run on. More advanced methods to divide resources include CPU priority settings, CPU utilization shares, and hard utilization percentage limits. Again on CPUs with SMT, non-SMT-aware schedulers might make the mistake of scheduling work on a non-busy core with a busy partner when non-busy physical cores are present. This would cause unnecessary competition for resources between the two threads. As a result, multithreaded CPU-intensive programs often manually assign the affinity of its threads to make sure they do not end fighting over the same physical core.

Specific operating systems

Unix-like On Linux, the CPU affinity of a process can be altered with the taskset(1) program and the sched_setaffinity(2) system call. The affinity of a thread can be altered with one of the library functions: pthread_setaffinity_np(3) or pthread_attr_setaffinity_np(3). On SGI systems, dplace binds a process to a set of CPUs. On NetBSD 5.0, FreeBSD 7.2, DragonFly BSD 4.7 and later versions can use pthread_setaffinity_np and pthread_getaffinity_np. In NetBSD, the psrset utility to set a thread's affinity to a certain CPU set. In FreeBSD, cpuset utility is used to create CPU sets and to assign processes to these sets. On DragonFly BSD 1.9 (2007) and later versions, usched_set system call can be used to control the affinity of a process. In DragonFly BSD 3.1 (2012) and later, usched utility can be used for assigning processes to a certain CPU set. On Solaris it is possible to control bindings of processes and LWPs to processor using the pbind(1) program. To control the affinity programmatically processor_bind(2) can be used. There are more generic interfaces available such as pset_bind(2) or lgrp_affinity_get(3LGRP) using processor set and locality groups concepts. On AIX it is possible to control bindings of processes using the bindprocessor command and the bindprocessor() API. The AIX scheduler is SMT-aware and is able to switch the SMT states of the POWER7/8/9 cores from 1 to 8 threads to maximize throughput.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Processor affinity

Start with the simplest possible case. Write down what Processor affinity claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Processor affinity before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Processor affinity ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Processor affinity

In research
Processor affinity appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Processor affinity in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Processor affinity is common in secondary-school and first-year university syllabi. It links to neighbouring topics Job scheduling, Load balancing (computing), Processor scheduling algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Processor affinity outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Processor affinity” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Processor affinity in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Processor affinity means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Processor affinity out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Processor affinity in simple terms?

In computer science, processor affinity, also called CPU pinning or cache affinity, enables the binding and unbinding of a process or a thread to a central processing unit (CPU) or a range of CPUs, so that the process or thread will execute only on the designated CPU or CPUs rather than any CPU. Th…

Why does Processor affinity matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Processor affinity?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Processor affinity.

Tags

  • Job scheduling
  • Load balancing (computing)
  • Processor scheduling algorithms

Keep exploring