Cache Coherence, Explained | Introduction to Operating Systems

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 4.10 Free with a free account

Cache Coherence

Introduction to Operating Systems · Chapter 4: Memory ManagementSection 4.10 · lesson 34 of 837 min · free with a free account

← Cache memoryNext lesson: Memory Allocation and Garbage Collection →

A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

In this lesson

How multicore CPUs keep private caches consistent with protocols like MESI.

  • Explain why private per-core caches can hold stale data
  • Compare write-invalidate and write-update coherence
  • Trace a cache line through the MESI states
  • Identify false sharing and how to avoid it

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,001 words). Read along, or search it.

Opening Scene

Two Phones, Two Stock Lists

Sam, who runs a small online bakery, and Ivy, who looks after the computers at a school, each keep a copy of the bakery's stock list on their phones. Sam sells the last chocolate loaf, and crosses it off. Ivy, looking at her old copy, promises that loaf to a customer. Nobody did anything wrong. Yet the two lists now disagree. A computer with several cores has exactly this problem, many times a second. It needs a fast, strict rule to stay out of trouble.

The Problem

Private Caches and Stale Copies

In lesson four point nine, each core got its own private cache. That's great for speed, and terrible for agreement. Say Sam's stock count x is five in memory. Core zero reads it, and core one reads it. Now each cache holds its own copy of five. Then core zero adds a fresh loaf, and writes six into its own cache. Even if memory gets the six too, core one's copy still says five. That copy is stale: out of date. Without a rule, core one keeps reading five, and never knows.

Definition

One Consistent Value per Location

Cache coherence is the promise that fixes this. All cores see one consistent value for each memory location. So no core can keep reading a stale copy after another core has changed it. The hardware enforces it, so ordinary programs don't have to think about it.

Two Answers

Write-Invalidate Versus Write-Update

When a core writes, the other copies must either vanish, or be refreshed. Those are the two classic answers. Write-invalidate: the writer announces, throw away your copy of x. The others mark it invalid, and if they need x again, they fetch the fresh value. Write-update: the writer sends the new value to everyone who holds a copy, so every copy becomes six at once.

Snooping and Directories

How Caches Hear About Writes

How do cores hear these messages? In small machines, every cache snoops on a shared bus, listening to what the others ask for. Bigger machines use a directory: a table that records which caches hold each line. Then messages go only to the cores that matter. A common mistake is to think coherence makes writes appear everywhere instantly. It doesn't. It guarantees that cores agree on the order of writes to one location. When order across several variables matters, programs still need locks, as in lesson three point two.

MESI Protocol

Modified, Exclusive, Shared, Invalid

The most widely taught write-invalidate protocol is MESI. Each cache line, in each core, is tagged with one of four states. Modified: this core changed the line, and no other copy exists. Memory is out of date. Exclusive: this core is the only holder, and the data is unchanged. Shared: several cores may hold the same unchanged data. Invalid: this copy can't be used. Those four initials spell the name.

Simulator Run

Following Variable X From Exclusive to Shared

Now follow x through a run of a small simulator. At the start, nobody has x, and memory says five. Core zero reads x. It's the only holder, so its line is Exclusive. Core zero writes six. It becomes Modified, and sends no message at all, because nobody else holds a copy. Memory still says five. Then core one reads x. Core zero must hand over its newer value. Both lines become Shared, and memory is updated to six.

Quick Check

Reading Another Core's Modified Line

Quick check, with new numbers. Core one holds x as Modified, with the value twelve. Memory still says ten. Now core zero reads x. What does it see, and what does memory hold afterward? Ten and ten? Twelve and twelve? Or twelve, with memory still at ten? Pause, and decide. Twelve and twelve. Core one holds the only up-to-date copy, so it hands twelve over, and memory is written with twelve too. Both lines end up Shared. Ten would be a stale read, which coherence forbids. And memory can't stay at ten, because Shared means memory is up to date.

Simulator Run

Invalidations on Each Write

Back to the run. Core one writes seven. It sends an invalidate message, so core zero's copy turns Invalid, and core one's line is Modified. When core zero reads again, core one hands over seven, and both are Shared. Core zero writes eight. Another invalidate message: core zero is Modified, and core one is Invalid. Then core zero writes nine. It already holds the only copy, as Modified, so there's nobody to tell. No message at all.

False Sharing

Two Variables, One Cache Line

Here's a surprise. Two cores can slow each other down even when they use two different variables. If both variables sit in the same cache line, every write invalidates the other core's copy, and the line bounces back and forth. This is called false sharing. A line is sixty-four bytes on most PCs, and a hundred twenty-eight bytes on this Mac. Two threads updating neighboring counters can make four cores run slower than one. The fix is to keep busy variables on separate lines.

Quick Check

Counters Zero and Twelve

Quick check. An array of eight-byte counters starts right at the beginning of a cache line. One thread keeps updating counter zero, and another keeps updating counter twelve. Do they false-share on this Mac only, with its hundred-twenty-eight-byte lines? On both machines? Or on a PC only, with sixty-four-byte lines? Pause, and decide. Only on this Mac. Counter twelve starts at byte ninety-six, because twelve times eight is ninety-six. On this Mac, bytes zero to a hundred twenty-seven make one line, so both counters share it, and the line bounces. On a PC, the first line ends at byte sixty-three, so counter twelve sits in the next line. No false sharing there.

Real Case

Invalidating Ivy's Stale List

Back to Sam and Ivy's phones. With a write-invalidate rule, Sam's cross-off would mark Ivy's copy invalid. Ivy would have to fetch the fresh list before promising that loaf. That's coherence: one strict rule, so no one acts on a stale copy.

Key Takeaways

Agreement Across Private Caches

To recap. Private caches can hold stale copies, and cache coherence promises one consistent value for each location. Write-invalidate discards the other copies, and write-update refreshes them. Caches hear about writes by snooping, or through a directory. MESI tags each line Modified, Exclusive, Shared or Invalid. And variables that share a cache line can slow each other down: false sharing.

About Introduction to Operating Systems at UPI Study

Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Cache Coherence

What is cache coherence in multicore processors?
Cache coherence is the property that all cores see one consistent value for each memory location, even though each core keeps private copies in its own cache. Without it, a core could keep reading a stale value after another core changed the data. Hardware enforces coherence with protocols such as MESI, with caches either snooping on a shared bus or consulting a directory.
What do the four MESI states mean?
The four MESI states are Modified, Exclusive, Shared and Invalid. Modified means this core changed the line, no other copy exists, and memory is out of date. Exclusive means this core is the only holder and the data is unchanged. Shared means several cores may hold the same unchanged data. Invalid means the copy cannot be used and must be fetched again.
What is false sharing, and how do you fix it?
False sharing happens when two cores update different variables that sit in the same cache line, so every write invalidates the other core's copy and the line bounces between them. It can make several threads run slower than one. The fix is to keep busy variables on separate cache lines, remembering that a line is often 64 bytes on PCs and 128 bytes on recent Macs.
Does cache coherence make writes visible to all cores instantly?
No, coherence does not make writes appear everywhere instantly. It ensures that cores agree on the order of writes to a single memory location. When a program depends on the order of updates across several different variables, it still needs locks or other synchronization. Coherence is handled by the hardware, but ordering between related variables remains the program's responsibility.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Introduction to Operating Systems.

83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Introduction to Operating Systems
3 credits · NCCRS
Enroll →