principles of operating systems: design & applications

52
Principles of Operating Systems: Design & Applications Chapter 9 Principles of Memory Management

Upload: odetta

Post on 10-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Principles of Operating Systems: Design & Applications. Chapter 9 Principles of Memory Management. Objectives. understand the relationships among the various types of memory in the memory hierarchy understand the purpose and general idea of address translation - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Principles of Operating Systems: Design & Applications

Principles of Operating Systems:Design & Applications

Chapter 9

Principles of Memory Management

Page 2: Principles of Operating Systems: Design & Applications

Objectives understand the relationships among the various

types of memory in the memory hierarchy understand the purpose and general idea of

address translation understand segmented address translation understand paged address translation be able to identify the application of each of the

memory services

Page 3: Principles of Operating Systems: Design & Applications

Objectives have a solid mental image of the various

approaches to memory layout, both globally and per-process

be able to compare free space representations and apply them at appropriate times

understand the difference between internal and external fragmentation

understand the various memory allocation techniques

understand the various page replacement policies

Page 4: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 4

The Memory Hierarchy

Engineering tradeoff: Cost and volume

versus Speed and capacity

For a given point in time, increased speed and capacity require greater cost and volume

Page 5: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 5

Memory Hierarchy (cont.)

Page 6: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 6

Address Translation

Relocation Protection Convert virtual addresses to physical addresses Needs to be fast: done in hardware

Page 7: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 7

Base/Limit Translation

Page 8: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 8

Segmented Translation

Page 9: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 9

Page Table

Page 10: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 10

Two-Level Page Table

Page 11: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 11

Page Table Entry

Page Frame Number (PFN): becomes upper bits of physical address

Protection bits: determine allowed accesses Present bit (P): indicates whether page is

resident in memory Dirty bit (D): indicates whether page has been

written to Accessed bit (A): indicates whether page has

been referenced

Page 12: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 12

Translation Lookaside Buffer

Page table lookups are usually memory references

Slow memory references by factor of 2, 3, 4, or even 5

TLB caches most recent PTEs

Page 13: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 13

Inverted Page Table

Full page table can be prohibitively large Often page frames in use are a small fraction of

virtual address space Alternative: translate page frame numbers to

page numbers Use TLB for recent translations

Page 14: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 14

Memory Services

Explicit allocation: process specifies virtual address range

Implicit allocation: kernel decides virtual range Garbage collection: identify and free unused

blocks Memory mapped file: file whose contents are

directly associated with a region in the virtual address space

Page 15: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 15

Typical System Memory Layout

Page 16: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 16

Typical Process Memory Layout

Page 17: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 17

Free Space Management

Free bit map: Good for fixed-sized blocks, like pages Low overhead

Free list: Sometimes tree or hash table Standard trick: store overhead structures in free

blocks themselves

Page 18: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 18

Free List Example

Page 19: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 19

Fragmentation

External fragmentation: wasted space outside allocation units

Internal fragmentation: wasted space within allocation units

Page 20: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 20

Partitions

Statically sized Allocated in whole Usually set up at boot time Usually administratively determined

Page 21: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 21

Variable-Sized Allocation

Among free blocks at least as large as the request: First fit: pick the first Next fit: pick the next Best fit: pick the smallest Worst fit: pick the biggest

Page 22: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 22

First Fit Example

Page 23: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 23

Next Fit Example

Page 24: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 24

Best Fit Example

Page 25: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 25

Worst Fit Example

Page 26: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 26

Buddy System Allocation algorithm:1. If n is less than the smallest allocation unit, set n to that smallest size

2. Round n up to the nearest power of 2 times the smallest allocation unit

3. If there is no block of the needed size, recursively allocate the next larger size and split it into two

4. Return the first block of the required size

Page 27: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 27

Buddy System (cont.)

Page 28: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 28

Overallocation Swapping: copying full processes between

main memory and backing store Segment swapping: copying individual

segments between main memory and backing store

Paging (page swapping): copying individual pages between main memory and backing store

Page 29: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 29

Paging Concepts Demand paging: reading pages into memory in

response to page faults Page fault: an interrupt that occurs when a

translation is not valid, often because the page is not resident in memory

Prepaging: loading pages in anticipation of later needing them

Page 30: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 30

Paging Concepts (cont.) Page reference string: list of page numbers in

the order in which they are referenced Global page replacement policy: page

replacement policy that selects pages without regard to the processes that own them

Local page replacement policy: page replacement policy that selects pages only from a given process

Page 31: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 31

Belady's Min Optimal page replacement policy Pick the page for which it will be longest before

it's used again: Unrealizable—noncausal Standard of comparison for realizable policies

p=argmax i t i

Page 32: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 32

First In, First Out Simple page replacement policy Select the page that has been resident for the

longest period of time Implemented with a queue Can “jump the gun” and swap out frequently

used pages just because they've been there a long time

Page 33: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 33

Second Chance Address FIFO weakness If the process at the head of the queue has

A=1, clear A and reinsert it at the tail, otherwise, swap it out

Allows frequently used pages to stay in memory for a long time

If all pages have A=1, then all get cleared and the oldest is swapped out—reduces to FIFO

Page 34: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 34

Clock Algorithm Implementation of second chance Pages are arranged in a circle and a “clock

hand” sweeps them At each step, if A=1, clear A and advance,

otherwise, copy the page out

Page 35: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 35

Two Hand Clock If there are many pages, nearly all may have

A=1 Reduce time between clearing A and testing Two “clock hands” move in lockstep: first clears,

second tests

Page 36: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 36

Not Recently Used If D=0 (M=0), the page need not be written; the

new one can be copied directly in to the frame Pages with D=0, can be swapped faster Let AD (AM) be a two bit number: 00, 01, 10, 11

(0, 1, 2, 3) Select a page from the set with the lowest

number

Page 37: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 37

Least Recently Used Pages not used for a long time may not be

needed for a long time Least recently used is a realizable

approximation to Belady's min Rarely implemented directly—hardware rarely

records access time Approximate by periodically clearing P and

recording the time of the page fault before resetting P

Page 38: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 38

Not Frequently Used Approximate LRU by looking at recent

frequency of usage For each sweep of pages compute:

where Pick a page with the lowest value of c

c '= c 1− A

0 1

Page 39: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 39

Paging Policies Comparison

Page 40: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 40

Paging Policies Comparison (cont.)

Page 41: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 41

The Working Set Set of pages currently “in use” by a process Keeping more pages is unneeded Keeping fewer pages leads to a large number

of page faults Attempt to keep the number of pages between

two thresholds Page locally if above the upper Swap out whole processes that are below the lower

Page 42: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 42

Page Fault Frequency Technique to adjust working set thresholds Keep an average of the process's page fault

frequency If too high, increase the working set size If very low, decrease the working set size

Page 43: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 43

Copy on Write (COW) Technique to avoid copying pages

unnecessarily Map both process's pages to same frames Make the “copied” pages read-only When a process writes, the fault triggers:

Make a copy of the page Map the two process's pages to separate copies Change the access back to its original value

Page 44: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 44

Average Access Time Average access time is given by:

where is the time to access memory, is the time to access the disk, and is the fraction of accesses causing page faults

must be very small to keep average time reasonable

t=tm ft dtdtm

f

f

Page 45: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 45

Thrashing Effect of active working sets being larger than

the physical memory The system spends more time swapping than

computing System performance intolerably slow Can be partially ameliorated with two-level

scheduling

Page 46: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 46

Belady's Anomaly Phenomenon where adding more page frames

leads to more page faults Some policies, such as FIFO, are susceptible Some policies, such as LRU, are immune

Page 47: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 47

Belady's Anomaly

Page 48: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 48

Summary Memory hierarchy gives perspective on

tradeoffs of speed and size Address translation is needed for relocation and

protection Translation can be in terms of segments, using

base and limit registers Translation can be in term of pages using page

tables containing page table entries

Page 49: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 49

Summary (cont.) TLB caches recently used PTEs Both system and per-process memory layouts

start at address 0 Typically process data segments grow up

toward stack and stack down toward data Free memory can be represented by bitmaps

and by lists Free lists typically keep pointers and sizes

inside the free blocks

Page 50: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 50

Summary (cont.) Fragmentation reduces usable memory Partitioning is a simple and old technique being

used again with virtualization Variable-sized blocks can be allocated by first

fit, next fit, best fit, worst fit, or buddy system Memory can be overallocated by using

swapping, segment swapping, or page swapping

Page 51: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 51

Summary (cont.) Page replacement policies attempt to get close

to performance of optimal Belady's min Common policies include FIFO, second chance

(in one and two hand clock implementations), NRU, LRU, and NFU

Try to keep process's working set in memory Adjust working set size with page fault

frequency Performance can be tricky

Page 52: Principles of Operating Systems: Design & Applications

Principles of Operating Systems: Design & Applications 52