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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Memory Allocation and Garbage Collection →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What do the four MESI states mean?
What is false sharing, and how do you fix it?
Does cache coherence make writes visible to all cores instantly?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →