30
Operating Systems: A Modern Perspective, Chapter 12 Slide 12-1 Copyright © 2004 Pearson Education, Inc. 12 Virtual Memory

Slide 12-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 12 12 Virtual Memory

Embed Size (px)

Citation preview

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-1

Copyright © 2004 Pearson Education, Inc.

12Virtual

Memory

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-2

Copyright © 2004 Pearson Education, Inc.

Virtual Memory Organization

Memory Image for pi

Secondary Memory

Primary Memory

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-3

Copyright © 2004 Pearson Education, Inc.

Names, Virtual Addresses & Physical Addresses

SourceProgram

AbsoluteModule

Name SpaceName Space

Pi’s VirtualAddress Space

Pi’s VirtualAddress Space

ExecutableImage

PhysicalAddress Space

PhysicalAddress Space

Bt: Virtual Address Space Physical Address Space

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-4

Copyright © 2004 Pearson Education, Inc.

Locality

• Address space is logically partitioned– Text, data, stack– Initialization, main, error handle

• Different parts have different reference patterns:

Address Space for pi

Initialization code (used once)Code for 1Code for 2Code for 3Code for error 1Code for error 2Code for error 3Data & stack

30%

20%

35%

15%

<1%

<1%

Execution time

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-5

Copyright © 2004 Pearson Education, Inc.

Virtual Memory• Every process has code and data locality

– Code tends to execute in a few fragments at one time

– Tend to reference same set of data structures

• Dynamically load/unload currently-used address space fragments as the process executes

• Uses dynamic address relocation/binding– Generalization of base-limit registers– Physical address corresponding to a compile-

time address is not bound until run time

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-6

Copyright © 2004 Pearson Education, Inc.

Virtual Memory (cont)

• Since binding changes with time, use a dynamic virtual address map, Bt

VirtualAddressSpace

Bt

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-7

Copyright © 2004 Pearson Education, Inc.

Virtual Memory

Virtual Address Space for pi

Virtual Address Space for pj

Virtual Address Space for pk

Secondary Memory

• Complete virtual address space is stored in secondary memory

Primary Memory

0

n-1

Physical Address Space

• Fragments of the virtual address space are dynamically loaded into primary memory at any given time

• Each address space is fragmented

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-8

Copyright © 2004 Pearson Education, Inc.

Paging

• A page is a fixed size, 2h, block of virtual addresses

• A page frame is a fixed size, 2h, block of physical memory (the same size as a page)

• When a virtual address, x, in page i is referenced by the CPU– If page i is loaded at page frame j, the virtual

address is relocated to page frame j– If page is not loaded, the OS interrupts the

process and loads the page into a page frame

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-9

Copyright © 2004 Pearson Education, Inc.

Page-Based Address Translation

• Let N = {d0, d1, … dn-1} be the pages• Let M = {b0, b1, …, bm-1} be page frames• Virtual address, i, satisfies 0i<G= 2g+h • Physical address, k = U2h+V (0V<G= 2h )

– U is page frame number– V is the line number within the page– Bt:[0:G-1] <U, V> {}– Since every page is size c=2h

• page number = U = i/c• line number = V = i mod c

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-10

Copyright © 2004 Pearson Education, Inc.

Modeling Page Behavior

• Let R = r1, r2, r3, …, ri, … be a page reference stream– ri is the ith page # referenced by the process

– The subscript is the virtual time for the process

• Given a page frame allocation of m, the memory state at time t, St(m), is set of pages loaded– St(m) = St-1(m) Xt - Yt

• Xt is the set of fetched pages at time t

• Yt is the set of replaced pages at time t

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-11

Copyright © 2004 Pearson Education, Inc.

Static Allocation, Demand Paging

• Number of page frames is static over the life of the process

• Fetch policy is demand

• Since St(m) = St-1(m) {rt} - {y}, the replacement policy must choose y -- which uniquely identifies the paging policy

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-12

Copyright © 2004 Pearson Education, Inc.

Address Translation with Paging

Page # Line #Virtual Address

g bits h bits

BtMissing Page

Frame # Line #j bits h bits

Physical Address

MAR

CPU Memory

“page table”

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-13

Copyright © 2004 Pearson Education, Inc.

Random Replacement• Replaced page, y, is chosen from the m

loaded page frames with probability 1/m

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7012

213

213

210

310

310

312

012

032

032

062

462

452

752

203

20

2

• No knowledge of R not perform well• Easy to implement

13 page faults

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-14

Copyright © 2004 Pearson Education, Inc.

Belady’s Optimal Algorithm• Replace page with maximal forward

distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-15

Copyright © 2004 Pearson Education, Inc.

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream,R = 2031203120316457

• Perfect knowledge of R perfect performance• Impossible to implement

10 page faults

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 0 0 0 0 4 4 41 0 0 0 0 0 3 3 3 3 3 3 6 6 6 72 3 1 1 1 1 1 1 1 1 1 1 1 5 5

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-16

Copyright © 2004 Pearson Education, Inc.

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

BKWD4(2) = 3BKWD4(0) = 2BKWD4(3) = 1

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-17

Copyright © 2004 Pearson Education, Inc.

Least Recently Used (LRU)• Replace page with maximal forward

distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 3 2 2 2 2 6 6 6 61 0 0 0 0 0 0 0 0 0 0 0 0 4 4 42 3 3 3 3 3 3 3 3 3 3 3 3 5 53 1 1 1 1 1 1 1 1 1 1 1 1 7

• Backward distance is a good predictor of forward distance -- locality

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-18

Copyright © 2004 Pearson Education, Inc.

Least Frequently Used (LFU)• Replace page with minimum use:

yt = min xeS t-1(m)FREQ(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

FREQ4(2) = 1FREQ4(0) = 1FREQ4(3) = 1

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-19

Copyright © 2004 Pearson Education, Inc.

Least Frequently Used (LFU)• Replace page with minimum use:

yt = min xeS t-1(m)FREQ(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 1 1 12 3 3 3 0

FREQ6(2) = 2FREQ6(1) = 1FREQ6(3) = 1

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-20

Copyright © 2004 Pearson Education, Inc.

First In First Out (FIFO)• Replace page that has been in memory the

longest: yt = max xeS t-1(m)AGE(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-21

Copyright © 2004 Pearson Education, Inc.

First In First Out (FIFO)• Replace page that has been in memory the

longest: yt = max xeS t-1(m)AGE(x)

Let page reference stream,R = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

AGE4(2) = 3AGE4(0) = 2AGE4(3) = 1

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-22

Copyright © 2004 Pearson Education, Inc.

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 31 1 1 12 2 2

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 01 1 1 12 2 23 3

LRU

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-23

Copyright © 2004 Pearson Education, Inc.

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

FIFOFrame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 4 4 41 1 1 1 0 0 0 0 0 2 2 22 2 2 2 1 1 1 1 1 3 3

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 4 4 4 4 3 31 1 1 1 1 1 1 0 0 0 0 42 2 2 2 2 2 2 1 1 1 13 3 3 3 3 3 3 2 2 2

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-24

Copyright © 2004 Pearson Education, Inc.

Windows NT Paging System

PrimaryMemory

Virtual AddressSpace

Supv spaceUser space

Paging Disk(Secondary Memory)

2. Lookup (Page i, Addr k)

3. Translate (Page i, Addr k) to (Page Frame j, Addr k)

4. Reference (Page Frame j, Addr k)

1. Reference to Address k in Page i (User space)

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-25

Copyright © 2004 Pearson Education, Inc.

Windows Address Translation

Page Directory Page Table Byte Index

Virtual page number Line number

Page Directory

Page Tables

TargetPage

Target Byte

c

b

a

A

C

B

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-26

Copyright © 2004 Pearson Education, Inc.

Linux Virtual Address Translation

j.pgdj.pgd j.pmdj.pmdVirtual Address

PageDirectory

PageDirectoryBase

Page

PageTable

Page MiddleDirectory

j.ptej.pte j.offsetj.offset

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-27

Copyright © 2004 Pearson Education, Inc.

Segment Address Translation• Bt: segments x offsets physical address {}

• Bt(i, j) = k• S: segments segment addresses• Bt(S(segName), j) = k• N: offset names offset addresses• Bt(S(segName), N(offsetName)) = k• Read implementation in Section 12.6

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-28

Copyright © 2004 Pearson Education, Inc.

Address Translation in Segmentation

S

<segmentName, offsetName>

N

segment # offset

Bt

Missing segment

Limit Base P

Limit

Relocation

+

?

To Memory Address Register

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-29

Copyright © 2004 Pearson Education, Inc.

NT Memory-mapped Files

Memorymapped

files

Memorymapped

files

Executable memory

Secondary memory

Ordinary file

•Open the file•Create a section object (that maps file)•Identify point in address space to place the file

Sectionobject

Operating Systems: A Modern Perspective, Chapter 12

Slide 12-30

Copyright © 2004 Pearson Education, Inc.

NT Memory-mapped Files(2)

Physical MemoryRepresentation of the file

Mapping Fto Process A

Process A VirtualAddress Space

Process B VirtualAddress Space

Mapping F toProcess B

Mapping B toPhysical Memory

Mapping Ato PhysicalMemory

“paging”