CS 3204 OS: Lecture 19 - Virtual Memory, Paging Techniques, and Demand Paging - Prof. Godm, Study notes of Operating Systems

A lecture note from cs 3204 operating systems course taught by godmar back at carnegie mellon university in spring 2007. The lecture covers various paging techniques, including demand paging, lazy loading, and copy-on-write. The document also discusses the concepts of page eviction, managing swap space, and locking frames.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-kjb
koofers-user-kjb šŸ‡ŗšŸ‡ø

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 3204
Operating Systems
Godmar Back
Lecture 19
3/27/2007CS 3204 Spring 2007 2
Announcements
• Midterm March 29
– See announcement + sample midterms on class
website
• Fix remaining project 2 bugs (if any)
– Reminder: by en d of semester, passing students will
have provided a deliverable that achieves >= 90%
test score on project 2 (or a 100% test score on
project 3 or 4’s regression tests.) – multi-oom not part
of these
• Project 3 Design Milestone
– Will retur n before midterm
Virtual Memory
Paging Techniques (cont’d)
3/27/2007CS 3204 Spring 2007 4
Demand paging
• Idea: only keep data in memory that’s being
used
– Needed for virtualization – don’t use up physical
memory for data processes don’t access
• Requires that actual allocation of physical page
frames be delayed until first access
• Many variations
– Lazy loading of text & data, mmapped pages & newly
allocated heap pages
– Copy-on-write
3/27/2007CS 3204 Spring 2007 5
ustack (1)
Lazy Loading
kernel
kernel
kernel
kernel
ucode (1)
kcode
kdata
kbss
kheap
0
C0000000
C0400000
FFFFFFFF
3 GB 1 GB
used
free
user (1)
user (1)
udata (1)
user (1)
Pintos loads the first process …
P1
Pintos then starts the first
process …
Process faults because code
page is not present …
Process faults when
touching address in data
segment …
stack page was allocated eagerly
data + code pages are noted
in page table, but no physical
frame has been allocated
3/27/2007CS 3204 Spring 2007 6
ustack (2)
ustack (1)
Stack Growth
kernel
kernel
kernel
kernel
ucode (1)
kcode
kdata
kbss
kheap
0
C0000000
C0400000
FFFFFFFF
3 GB 1 GB
used
free
user (1)
user (1)
udata (1)
user (1)
Pintos loads the first process …
P1
Pintos then starts the first
process …
Process faults because code
page is not present …
Process calls recursive
function or allocates large
local variable
page fault at about here
pf3
pf4
pf5

Partial preview of the text

Download CS 3204 OS: Lecture 19 - Virtual Memory, Paging Techniques, and Demand Paging - Prof. Godm and more Study notes Operating Systems in PDF only on Docsity!

CS 3204

Operating Systems

Godmar Back

Lecture 19

CS 3204 Spring 2007 3/27/2007 2

Announcements

• Midterm March 29

  • See announcement + sample midterms on classwebsite

• Fix remaining project 2 bugs (if any)

  • Reminder: by end of semester, passing students willhave provided a deliverable that achieves >= 90%

test score on project 2 (or a 100% test score onproject 3 or 4’s regression tests.) – multi-oom not part

of these

• Project 3 Design Milestone– Will return before midterm

Virtual Memory

Paging Techniques (cont’d)

CS 3204 Spring 2007 3/27/2007 4

Demand paging

• Idea: only keep data in memory that’s being

used

  • Needed for virtualization – don’t use up physicalmemory for data processes don’t access

• Requires that actual allocation of physical page

frames be delayed until first access

• Many variations

  • Lazy loading of text & data, mmapped pages & newlyallocated heap pages
  • Copy-on-write

CS 3204 Spring 2007 3/27/2007 5

ustack (1)

Lazy Loading

kernelkernel

kernelkernel ucode (1)

kcodekdata

kheapkbss

C

C

FFFFFFFF

3 GB

1 GB

used

free user (1)user (1) udata (1)

user (1)

P1 Pintos loads the first process …

Pintos then starts the firstprocess … Process faults because codepage is not present … Process faults whentouching address in data segment … stack page was allocated eagerly data + code pages are notedin page table, but no physical frame has been allocated CS 3204 Spring 2007 3/27/2007 6

ustack (2)ustack (1)

Stack Growth

kernelkernel

kernelkernel ucode (1)

kcodekdata

kheapkbss

C

C

FFFFFFFF

3 GB

1 GB

used

free user (1)user (1) udata (1)

user (1)

P1 Pintos loads the first process …

Pintos then starts the firstprocess … Process faults because codepage is not present … Process calls recursivefunction or allocates large local variable page fault at about here

CS 3204 Spring 2007 3/27/2007 7

ustack (1)

mmap()

kernelkernel

kernelkernel ucode (1)

kcodekdata

kheapkbss

C

C

FFFFFFFF

3 GB

1 GB

used

free user (1)user (1) udata (1)

user (1)

P1 Pintos loads the first process …

Pintos then starts the firstprocess … Process faults because codepage is not present … Process opens file, callsmmap(fd, addr)

ummap (1)

Process faults whentouching mapped file Page fault handler allocspage, maps it, reads data from disk: CS 3204 Spring 2007 3/27/2007 8

Lazy Loading & Prefetching

  • Typically want to do some prefetching when faulting inpage
  • Q.: how many pages? which pages?– Reduces latency on subsequent faults
    • Too much: waste time & space fetching unused pages– Too little: pay (relatively large) page fault latency too often
  • Predict which pages the program will access next (how?)
  • Let applications give hints to OS– If applications knows
    • Example: madvise(2)– Usual conflict: what’s best for application vs what’s best for

system as a whole

CS 3204 Spring 2007 3/27/2007 9

Copy-On-Write

  • Sometimes, want to create a copy of a page:– Example: Unix fork() creates copies of all parent’s pages in the
  • Optimization:child
    • Don’t copy pages, copy PTEs – now have 2 PTEs pointing toframe
    • Set all PTEs read-only– Read accesses succeed
    • On Write access, copy the page into new frame, update PTEs topoint to new & old frame
  • Looks like each have their own copy, but postponeactual copying until one is writing the data
    • Hope is at most one will ever touch the data – never have tomake actual copy CS 3204 Spring 2007 3/27/2007 10

Page Eviction

  • Suppose page fault occurs, but no free physical frame is there toallocate
  • Must evict frame– Find victim frame (how – later)
    • – Find & change old page table entry pointing to the victim frameIf data in it isn’t already somewhere on disk, write to special area on
    • disk (ā€œswap spaceā€)Install in new page table entry
  • Requires check on page fault if page has been swapped out – fault–^ Resume
  • in if soSome subtleties with locking:
    • How do you prevent a process from writing to a page some otherprocess has chosen to evict from its frame?
    • What do you do if a process faults on a page that another process is inthe middle of paging out?

CS 3204 Spring 2007 3/27/2007 11

Page Eviction Example

victim frame:phys addr = …

PTE:process id =? (if appl.),

virtual addr = ?,dirty bit = ?,

accessed bit = ?,

Process A needs a framedecides it wants this frame

Q.: how will it find the PTE,if any, that points to it?

Linux uses a so-called ā€œrmapā€ for that that links frames to PTE

CS 3204 Spring 2007 3/27/2007 12

Managing Swap Space

  • Continuous region on disk– Preferably on separate disk, but typically a partition on same
  • Different allocation strategies are possibledisk
    • Simplest: when page must be evicted, allocate swap space forpage; deallocate when page is paged back in
    • Or: allocate swap space upfront– Should page’s position in swap space change? What if same
  • Can be managed via bitmap 0100100000001page is paged out multiple times?
    • Free/used bits for each page that can be stored– Pintos: note 1 page == 8 sectors

CS 3204 Spring 2007 3/27/2007 19

Buddy Example - Allocation

64 KB

16KB 16KB 32KB

16KB 16KB 32KB

16KB 4KB 4KB 8KB 32KB

16KB 4KB 4KB 8KB 32KB

16KB 4KB 4KB4KB4KB 32KB

Alloc (16KB) Alloc (32KB) Alloc (4KB) Alloc (4KB) Alloc (4KB) CS 3204 Spring 2007 3/27/2007 20

Buddy Example - Deallocation

64 KB

16KB

32KB

16KB

16KB 32KB

16KB 32KB

32KB

16KB 4KB 4KB4KB4KB 32KB

Free() Free() Free() Free() Free()

4KB 4KB4KB4KB 32KB

8KB 4KB4KB

CS 3204 Spring 2007 3/27/2007 21

Fragmentation

• Def: The inability to use memory that is

unused.

• Internal fragmentation:

– Not all memory inside an allocated unit is

used; rest can’t be allocated to other users

• External fragmentation:

– Impossible to satisfy allocation request eventhough total amount of memory > size

requested

CS 3204 Spring 2007 3/27/2007 22

Internal Fragmentation

64 KB

12KB 16KB 32KB

16KB 24KB

4KB 4KB 8KB

4KB 4KB 8KB

4KB 4KB 3KB4KB

Alloc (12KB) Alloc (24KB) Alloc (4KB) Alloc (4KB) Alloc (3KB) 12KB

12KB

12KB

12KB

24KB

24KB

24KB

CS 3204 Spring 2007 3/27/2007 23

External Fragmentation

64 KB

16KB

32KB

16KB

16KB 32KB

16KB 32KB

32KB

16KB 4KB 4KB4KB4KB 32KB

Free() Free() Free() Free() Free()

4KB 4KB4KB4KB 32KB

8KB 4KB4KB

Have 8 KB free, but canā€˜t Alloc(8KB) Have 12 KB free, but canā€˜t Alloc(12KB)

No external fragmentation

No external fragmentation

CS 3204 Spring 2007 3/27/2007 24

Buddy Allocator & Fragmentation

• Q.: what is the average internal fragmentation

(per allocated object) for

  • buddy allocator with size 2^n?
  • in bitmap allocator for objects of size n*s, where eachbit represents a unit of size s?
  • in first-fit allocator from project 0?

• Q.: what external fragmentation can you expect

from buddy allocator scheme?

• Q.: what’s a good way to measure fragmentation

in general?

CS 3204 Spring 2007 3/27/2007 25

Page Size & Fragmentation

• How should a system’s architect choose thepage size? – Trade-Off

• Large pages:– Larger internal fragmentation

  • (not an issue if most pages are full…)– Page-in & write-back cost larger

• Small pages:– Higher overhead to store page table (more entries to

maintain)

• Modern architectures provide support for ā€œsuperpagesā€ – 2MB or 4MB