Course contentsShow
← Computer Science
Lesson 1265 of 2,87229. CPU SchedulingFree lesson

Processor Affinity

Understanding cache-warm benefits of keeping processes on the same CPU and soft versus hard affinity policies.

Processor Affinity

What you'll learn: How operating systems can keep processes bound to specific CPUs to improve performance through cache warmth and affinity policies.


The Core Idea

Imagine you've organized your desk perfectly for a task—papers, pens, and references all within arm's reach. Now imagine someone forcing you to switch desks randomly every few minutes. You'd waste time reorganizing each time!

Processor affinity is the tendency (or requirement) for a process or thread to run on the same CPU core it previously ran on. When a process executes on a CPU, that CPU's cache fills with the process's data and instructions—we say the cache becomes "cache-warm." If the scheduler moves the process to a different CPU, the new CPU's cache is "cold," and the data must be fetched from main memory again (a costly operation). By keeping the process on the same CPU, we maintain cache warmth and avoid this penalty.

Soft vs Hard Affinity

Operating systems implement affinity in two ways:

  • Soft affinity: The scheduler tries to keep a process on the same CPU when possible, but may migrate it for load balancing. This is the default in most systems—a preference, not a rule.

  • Hard affinity: The process is locked to specific CPUs and will never run elsewhere. System administrators or applications can explicitly set hard affinity (e.g., binding a critical database process to cores 0-3).

Soft affinity balances performance with flexibility; hard affinity maximizes cache benefits but may cause load imbalance if not managed carefully.


Key Takeaway:
Processor affinity keeps processes running on the same CPU to maintain cache-warm data, improving performance. Soft affinity is a flexible guideline; hard affinity is an enforced constraint.