Paging, Explained | Introduction to Operating Systems

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

Paging

Introduction to Operating Systems · Chapter 4: Memory ManagementSection 4.8 · lesson 32 of 839 min · free with a free account

← Virtual MemoryNext lesson: Cache memory →

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 page tables and the TLB map logical pages to physical frames.

  • Explain how paging places fixed-size pages into any free frames
  • Split a logical address into a page number and offset and translate it to a physical address
  • Calculate effective access time from a TLB hit ratio
  • Describe what a page table entry holds besides the frame number

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,275 words). Read along, or search it.

Opening Analogy

A Library Catalog for Book Parts

A library doesn't keep a big book's parts in one long row. Part one might sit on shelf five, part two on shelf nine, and part three on shelf two. A card in the catalog tells you where each one is. Computers do the same with programs. They chop memory into equal pieces, put the pieces wherever there's room, and keep a catalog. The trick is how fast that catalog can be read.

The Problem

Enough Free Space, No Single Block

Sam, who runs a small online bakery, has a program that needs twenty thousand bytes. RAM has enough free space in total, but only in small, scattered gaps between other programs. If the program had to sit in one unbroken block, it wouldn't fit anywhere. Paging fixes that, by cutting everything into equal pieces.

Pages and Frames

Any Page Fits Any Free Frame

The program's logical memory is cut into pages. Physical RAM is cut into frames, and each frame is exactly the size of a page. Any page can go into any free frame, in any order. Sam's five pages land in frames five, nine, three, seven and one, between other programs' pages. Nobody had to find five free frames side by side.

Page Table

One Entry per Page

The OS keeps one page table for each program. It's a list with one entry per page, and each entry holds the frame where that page lives. The program sees a tidy row of pages, zero to four. The hardware quietly looks each one up. So paging ends the hunt for one hole big enough. Lesson four point eleven shows that trouble.

Leftover Space

Unused Bytes in the Last Page

Paging doesn't remove all waste. A program that needs ten thousand bytes still gets whole pages. With pages of four kibibytes, that's three pages: twelve thousand two hundred eighty-eight bytes. So two thousand two hundred eighty-eight bytes of the last page go unused. On this Mac, with its sixteen kibibyte pages, it's one page, and six thousand three hundred eighty-four bytes go unused. Bigger pages mean smaller page tables, but more waste in the last page.

Address Split

Page Number and Offset

So how does the hardware find one byte? The CPU sends a logical address: a single number. Think of a hotel room number, like three twelve. The three is the floor, and twelve is the room on that floor. A logical address splits the same way: into a page number, which page, and an offset, how far into that page. With four kibibyte pages, divide the address by four thousand ninety-six. The quotient is the page number, and the remainder is the offset. Take thirteen thousand. It's three times four thousand ninety-six, with seven hundred twelve left over. So page three, offset seven hundred twelve.

Binary Shortcut

The Low Twelve Bits Are the Offset

In binary, it's even easier, because four thousand ninety-six is two to the power twelve. The low twelve bits are the offset, and all the bits above them are the page number. Thirteen thousand in sixteen bits: the top four bits say three, and the low twelve say seven hundred twelve. No division needed. On this Mac, pages are sixteen kibibytes, two to the fourteen. So there, the low fourteen bits are the offset.

Translation

Frame Times Page Size Plus Offset

Now the page table does its job. Page three lives in frame seven. The physical address is the frame number times the page size, plus the offset. Seven times four thousand ninety-six is twenty-eight thousand six hundred seventy-two. Add seven hundred twelve, and you get twenty-nine thousand three hundred eighty-four. Only the page number was swapped for a frame number. The offset is copied unchanged. A common mistake is to add the page number to the offset, or to forget to multiply the frame by the page size.

Quick Check

Translating Logical Address 9,000

Quick check, with the same page table, where page two lives in frame three. A program uses logical address nine thousand, and pages are four kibibytes. Which physical address does it reach? Thirteen thousand ninety-six? Nine thousand? Or eight hundred eleven? Pause, and decide. Thirteen thousand ninety-six. Nine thousand is two times four thousand ninety-six, plus eight hundred eight. So it's page two, offset eight hundred eight. Page two is in frame three. Three times four thousand ninety-six, plus eight hundred eight, is thirteen thousand ninety-six. Nine thousand forgets to swap the page for the frame. Eight hundred eleven adds the frame to the offset, without multiplying.

Hidden Cost

Two Memory Trips per Access

There's a hidden cost. The page table itself sits in RAM. So every memory access now takes two trips: one to read the page table entry, and one to read the data. That would make every program run at about half speed. So something has to cut out the extra trip.

TLB

The Translation Lookaside Buffer

The cure is a small, very fast memory inside the CPU, called the TLB: the translation lookaside buffer. It remembers the latest page-to-frame answers. The CPU looks aside at it first. On a TLB hit, the frame comes back at once, and the CPU goes straight to the data. On a TLB miss, the CPU reads the page table in RAM, and stores the answer in the TLB for next time. Programs reuse the same pages again and again, as lesson four point nine explains. So hits are common.

Effective Access Time

Weighting Hits and Misses

How much does it help? The effective access time is the average time per access, weighted by how often each path happens. Take illustrative numbers: one hundred nanoseconds for a RAM access, and ten for a TLB lookup. A hit costs ten plus one hundred: one hundred ten. A miss costs ten plus one hundred plus one hundred: two hundred ten. With no TLB at all, every access costs two hundred nanoseconds. At a hit ratio of point nine, the average is one hundred twenty. At point nine eight, it's one hundred twelve, close to the ideal one hundred ten.

Quick Check

Access Time at a 0.8 Hit Ratio

Quick check. Same numbers: one hundred nanoseconds for RAM, and ten for the TLB. But this program's hit ratio is only point eight. What's its effective access time? One hundred twenty nanoseconds? One hundred sixty? Or one hundred thirty? Pause, and decide. One hundred thirty. Eight tenths of the accesses cost one hundred ten each, which gives eighty-eight. Two tenths cost two hundred ten each, which gives forty-two. Eighty-eight plus forty-two is one hundred thirty. One hundred sixty just averages the two costs, and ignores how often each happens. One hundred twenty is the answer for point nine.

Page Table Entries

Present Bits, Protection Bits and Levels

A page table entry holds more than a frame number. It also carries a few small flags. A present bit says whether the page is in RAM at all. If it's clear, the CPU raises the page fault from lesson four point seven. Protection bits say whether the page may be read, written, or run. So one program can't scribble on another's pages, or change its own code by accident. Tables can be big. With thirty-two bit addresses and four kibibyte pages, there are two to the twenty pages: over a million entries for each program. So real systems split the table into levels, and keep only the parts in use.

Real Case

Two Boxes on a Napkin

Ivy, who looks after the computers at a school, once explained all this to Sam on a napkin. She drew two boxes: a small one labeled TLB, and a big one labeled RAM. Nine times out of ten, she said, the answer is in the small box. Only when it isn't do we walk to the big one.

Key Takeaways

Pages, Frames, Tables and the TLB

To recap. Paging cuts a program into pages, and RAM into frames of the same size. Any page fits any free frame. A page table maps each page to its frame. A logical address splits into a page number and an offset. The physical address is the frame times the page size, plus the offset. And the TLB remembers recent answers, so most accesses skip the extra trip.

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 Paging

How do you translate a logical address to a physical address in paging?
Divide the logical address by the page size: the quotient is the page number and the remainder is the offset. Look up that page's frame in the page table, then compute frame times page size plus offset. With 4,096-byte pages, address 13,000 is page 3, offset 712. If page 3 is in frame 7, the physical address is 28,672 plus 712, or 29,384.
What is a TLB in an operating system?
A TLB, or translation lookaside buffer, is a small, very fast memory inside the CPU that remembers recent page-to-frame translations. On a TLB hit, the frame number is available at once and the CPU goes straight to the data. On a miss, the CPU reads the page table in RAM and stores the answer in the TLB. Because programs reuse the same pages, hits are common.
How do you calculate effective access time with a TLB?
Effective access time is the hit ratio times the hit cost plus the miss ratio times the miss cost. With a 10 nanosecond TLB lookup and a 100 nanosecond RAM access, a hit costs 110 nanoseconds and a miss costs 210, since a miss also reads the page table. At a 0.8 hit ratio, the result is 88 plus 42, or 130 nanoseconds.
Does paging eliminate memory fragmentation?
Paging removes external fragmentation but not internal fragmentation. Any page can go in any free frame, so a program never needs one large unbroken hole. Memory is still handed out in whole pages, though, so the last page is usually partly empty. A 10,000-byte program with 4 KiB pages gets three pages, or 12,288 bytes, leaving 2,288 bytes unused.

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 →