Chapter 9: Virtual Memory

88
1 Chapter 9: Chapter 9: Virtual Memory Virtual Memory

description

Chapter 9: Virtual Memory. Sections Covered in Chapter. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel Memory Other Considerations (Slide 73 only) Operating-System Examples - PowerPoint PPT Presentation

Transcript of Chapter 9: Virtual Memory

Page 1: Chapter 9:  Virtual Memory

11

Chapter 9: Virtual Chapter 9: Virtual MemoryMemory

Page 2: Chapter 9:  Virtual Memory

Sections Covered in ChapterSections Covered in Chapter

BackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other Considerations Other Considerations (Slide 73 only)(Slide 73 only)

Operating-System ExamplesOperating-System Examples

NoteNote: Skipped slides also indicated in slide notes.: Skipped slides also indicated in slide notes.22

Page 3: Chapter 9:  Virtual Memory

33

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 4: Chapter 9:  Virtual Memory

44

ObjectivesObjectives

To describe the benefits of a virtual To describe the benefits of a virtual memory systemmemory system

To explain the concepts of demand To explain the concepts of demand paging, page-replacement algorithms, and paging, page-replacement algorithms, and the allocation of page framesthe allocation of page frames

To discuss the principle of the To discuss the principle of the working-setworking-set modelmodel

Page 5: Chapter 9:  Virtual Memory

55

BackgroundBackgroundVirtual memoryVirtual memory – separation of user logical memory – separation of user logical memory from physical memory.from physical memory. Only part of the program needs to be in memory for Only part of the program needs to be in memory for

executionexecution Logical address space can therefore be much larger than Logical address space can therefore be much larger than

physical address spacephysical address space Allows address spaces to be shared by several processesAllows address spaces to be shared by several processes Allows for more efficient process creationAllows for more efficient process creation

Virtual memory can be implemented via:Virtual memory can be implemented via: Demand pagingDemand paging Demand segmentationDemand segmentation

Page 6: Chapter 9:  Virtual Memory

66

Virtual Memory That is Larger Than Virtual Memory That is Larger Than Physical MemoryPhysical Memory

Page 7: Chapter 9:  Virtual Memory

77

Virtual-address SpaceVirtual-address Space

Page 8: Chapter 9:  Virtual Memory

88

Shared Library Using Virtual Shared Library Using Virtual MemoryMemory

Page 9: Chapter 9:  Virtual Memory

99

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 10: Chapter 9:  Virtual Memory

1010

Demand PagingDemand PagingBring a page into memory only when neededBring a page into memory only when needed Less I/O neededLess I/O needed Less memory needed Less memory needed Faster responseFaster response More usersMore users

Page is needed Page is needed reference to it reference to it invalid reference invalid reference abort abort not-in-memory not-in-memory bring to memory bring to memory

Lazy swapperLazy swapper – never swaps a page into memory – never swaps a page into memory unless page will be neededunless page will be needed Swapper that deals with pages is a Swapper that deals with pages is a pagerpager

Page 11: Chapter 9:  Virtual Memory

1111

Transfer of a Paged Memory to Transfer of a Paged Memory to Contiguous Disk SpaceContiguous Disk Space

Page 12: Chapter 9:  Virtual Memory

1212

Valid-Invalid BitValid-Invalid BitWith each page table entry a valid–invalid bit exists (With each page table entry a valid–invalid bit exists (vv in-memory, in-memory, ii not-in-memory) not-in-memory)Initially the valid–invalid bit is set toInitially the valid–invalid bit is set to ii on all entrieson all entries

The above is an example of a page table snapshotThe above is an example of a page table snapshot ..

During address translation, if valid–invalid bit in page table entryDuring address translation, if valid–invalid bit in page table entry isis I I page fault page fault

vvvvi

ii

….

Frame #

valid-invalid bit

page table

Page 13: Chapter 9:  Virtual Memory

1313

Page Table When Some Pages Are Not Page Table When Some Pages Are Not in Main Memoryin Main Memory

Page 14: Chapter 9:  Virtual Memory

1414

Page FaultPage FaultIf there is a reference to a page, the first reference If there is a reference to a page, the first reference will trap to the operating system:will trap to the operating system:

1.1.Operating system looks at another table to decide:Operating system looks at another table to decide: Invalid reference Invalid reference abort abort Just not in memoryJust not in memory

2.2.Get empty frameGet empty frame3.3.Swap page into frameSwap page into frame4.4.Reset tablesReset tables5.5.Set validation bit = Set validation bit = vv6.6.Restart the instruction that caused the page faultRestart the instruction that caused the page fault

Page 15: Chapter 9:  Virtual Memory

1515

Page Fault (Cont.)Page Fault (Cont.)Restart instructionRestart instruction block moveblock move

auto increment/decrement locationauto increment/decrement location

Page 16: Chapter 9:  Virtual Memory

1616

Steps in Handling a Page FaultSteps in Handling a Page Fault

Page 17: Chapter 9:  Virtual Memory

1717

Performance of Demand PagingPerformance of Demand PagingPage Fault Rate 0 Page Fault Rate 0 pp 1.0 1.0 if if pp = 0 no page faults = 0 no page faults if if pp = 1, every reference is a fault = 1, every reference is a fault

Effective Access Time (EAT)Effective Access Time (EAT)

EAT = (1 – EAT = (1 – pp) x memory access) x memory access

+ + pp (page fault overhead (page fault overhead

+ swap page out+ swap page out

+ swap page in+ swap page in

+ restart overhead)+ restart overhead)

Page 18: Chapter 9:  Virtual Memory

1818

Demand Paging ExampleDemand Paging ExampleMemory access time = 200 nanosecondsMemory access time = 200 nanoseconds

Average page-fault service time = 8 Average page-fault service time = 8 millisecondsmilliseconds

EAT = (1 – p) x 200 + p (8 milliseconds) EAT = (1 – p) x 200 + p (8 milliseconds)

= (1 – p x 200 + p x 8,000,000 = (1 – p x 200 + p x 8,000,000

= 200 + p x 7,999,800= 200 + p x 7,999,800

If one access out of 1,000 causes a page fault, If one access out of 1,000 causes a page fault, thenthen

EAT = 8.2 microseconds. EAT = 8.2 microseconds.

This is a slowdown by a factor of 40!!This is a slowdown by a factor of 40!!

Page 19: Chapter 9:  Virtual Memory

1919

Process CreationProcess Creation

Virtual memory allows other benefits Virtual memory allows other benefits during process creation:during process creation:

- Copy-on-Write- Copy-on-Write

- Memory-Mapped Files (later)- Memory-Mapped Files (later)

Page 20: Chapter 9:  Virtual Memory

2020

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 21: Chapter 9:  Virtual Memory

2121

Copy-on-WriteCopy-on-WriteCopy-on-Write (COW) allows both parent and child Copy-on-Write (COW) allows both parent and child processes to initially processes to initially shareshare the same pages in the same pages in memorymemory

If either process modifies a shared page, only then If either process modifies a shared page, only then is the page copiedis the page copied

COW allows more efficient process creation as only COW allows more efficient process creation as only modified pages are copiedmodified pages are copied

Free pages are allocated from a Free pages are allocated from a poolpool of zeroed-out of zeroed-out pagespages

Page 22: Chapter 9:  Virtual Memory

2222

Before Process 1 Modifies Page CBefore Process 1 Modifies Page C

Page 23: Chapter 9:  Virtual Memory

2323

After Process 1 Modifies Page CAfter Process 1 Modifies Page C

Page 24: Chapter 9:  Virtual Memory

2424

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 25: Chapter 9:  Virtual Memory

2525

What happens if there is no free What happens if there is no free frame?frame?

Page replacement – find some page in Page replacement – find some page in memory, but not really in use, swap it outmemory, but not really in use, swap it out algorithmalgorithm performance – want an algorithm which will performance – want an algorithm which will

result in minimum number of page faultsresult in minimum number of page faults

Same page may be brought into memory Same page may be brought into memory several timesseveral times

Page 26: Chapter 9:  Virtual Memory

2626

Page ReplacementPage ReplacementPrevent over-allocation of memory by modifying Prevent over-allocation of memory by modifying page-fault service routine to include page page-fault service routine to include page replacementreplacement

Use Use modify (dirty) bitmodify (dirty) bit to reduce overhead of to reduce overhead of page transfers – only modified pages are page transfers – only modified pages are written to diskwritten to disk

Page replacement completes separation Page replacement completes separation between logical memory and physical memory between logical memory and physical memory – a large virtual memory can be provided on a – a large virtual memory can be provided on a smaller physical memorysmaller physical memory

Page 27: Chapter 9:  Virtual Memory

2727

Need For Page ReplacementNeed For Page Replacement

Page 28: Chapter 9:  Virtual Memory

2828

Basic Page ReplacementBasic Page Replacement

1.1. Find the location of the desired page on diskFind the location of the desired page on disk

2.2. Find a free frame:Find a free frame: - If there is a free frame, use it - If there is a free frame, use it - If there is no free frame, use a page - If there is no free frame, use a page replacement replacement algorithm to select a algorithm to select a victimvictim frameframe

3.3. Bring the desired page into the (newly) free Bring the desired page into the (newly) free frame; update the page and frame tablesframe; update the page and frame tables

4.4. Restart the processRestart the process

Page 29: Chapter 9:  Virtual Memory

2929

Page ReplacementPage Replacement

Page 30: Chapter 9:  Virtual Memory

3030

Page Replacement AlgorithmsPage Replacement AlgorithmsWant lowest page-fault rateWant lowest page-fault rate

Evaluate algorithm by running it on a particular Evaluate algorithm by running it on a particular string of memory references (reference string) string of memory references (reference string) and computing the number of page faults on and computing the number of page faults on that stringthat string

In all our examples, the reference string is In all our examples, the reference string is

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 51, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Page 31: Chapter 9:  Virtual Memory

3131

Graph of Page Faults Versus The Graph of Page Faults Versus The Number of FramesNumber of Frames

Page 32: Chapter 9:  Virtual Memory

3232

First-In-First-Out (FIFO) AlgorithmFirst-In-First-Out (FIFO) AlgorithmReference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

3 frames (3 pages can be in memory at a time 3 frames (3 pages can be in memory at a time per process)per process)

4 frames4 framesBelady’s Anomaly: more frames Belady’s Anomaly: more frames more page faults more page faults

1

2

3

1

2

3

4

1

2

5

3

4

9 page faults

1

2

3

1

2

3

5

1

2

4

510 page faults

44 3

Page 33: Chapter 9:  Virtual Memory

3333

FIFO Page ReplacementFIFO Page Replacement

Page 34: Chapter 9:  Virtual Memory

3434

FIFO Illustrating Belady’s FIFO Illustrating Belady’s AnomalyAnomaly

Page 35: Chapter 9:  Virtual Memory

3535

Optimal AlgorithmOptimal Algorithm

Replace page that will not be used for longest period of timeReplace page that will not be used for longest period of time

4 frames example4 frames example

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 51, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

How do you know this?How do you know this?

Used for measuring how well your algorithm performsUsed for measuring how well your algorithm performs

1

2

3

46 page faults

4 5

Page 36: Chapter 9:  Virtual Memory

3636

Optimal Page ReplacementOptimal Page Replacement

Page 37: Chapter 9:  Virtual Memory

3737

Least Recently Used (LRU) Least Recently Used (LRU) AlgorithmAlgorithm

Reference string: 1, 2, 3, 4, 1, 2, Reference string: 1, 2, 3, 4, 1, 2, 55, 1, 2, , 1, 2, 33, , 44, , 55

Counter implementationCounter implementation Every page entry has a counter; every time page is referenced Every page entry has a counter; every time page is referenced

through this entry, copy the clock into the counterthrough this entry, copy the clock into the counter When a page needs to be changed, look at the counters to When a page needs to be changed, look at the counters to

determine which are to changedetermine which are to change

5

2

4

3

1

2

3

4

1

2

5

4

1

2

5

3

1

2

4

3

Page 38: Chapter 9:  Virtual Memory

3838

LRU Page ReplacementLRU Page Replacement

Page 39: Chapter 9:  Virtual Memory

3939

LRU Algorithm (Cont.)LRU Algorithm (Cont.)

Stack implementation – keep a stack of Stack implementation – keep a stack of page numbers in a double link form:page numbers in a double link form: Page referenced:Page referenced:

move it to the topmove it to the top

requires 6 pointers to be changedrequires 6 pointers to be changed No search for replacementNo search for replacement

Page 40: Chapter 9:  Virtual Memory

4040

Use Of A Stack to Record The Most Recent Use Of A Stack to Record The Most Recent Page ReferencesPage References

Page 41: Chapter 9:  Virtual Memory

4141

LRU Approximation AlgorithmsLRU Approximation Algorithms

Reference bitReference bit With each page associate a bit, initially = 0With each page associate a bit, initially = 0 When page is referenced bit set to 1When page is referenced bit set to 1 Replace the one which is 0 (if one exists)Replace the one which is 0 (if one exists)

We do not know the order, howeverWe do not know the order, however

Page 42: Chapter 9:  Virtual Memory

4242

LRU Approximation AlgorithmsLRU Approximation Algorithms

Second chanceSecond chance Need reference bitNeed reference bit Clock replacementClock replacement If page to be replaced (in clock order) has If page to be replaced (in clock order) has

reference bit = 1 then:reference bit = 1 then:

set reference bit 0set reference bit 0

leave page in memoryleave page in memory

replace next page (in clock order), subject to replace next page (in clock order), subject to same rulessame rules

Page 43: Chapter 9:  Virtual Memory

4343

Second-Chance (clock) Page-Second-Chance (clock) Page-Replacement AlgorithmReplacement Algorithm

Page 44: Chapter 9:  Virtual Memory

4444

Counting AlgorithmsCounting Algorithms

Keep a counter of the number of references Keep a counter of the number of references that have been made to each pagethat have been made to each page

LFU AlgorithmLFU Algorithm: replaces page with smallest : replaces page with smallest countcount

MFU AlgorithmMFU Algorithm: based on the argument that : based on the argument that the page with the smallest count was probably the page with the smallest count was probably just brought in and has yet to be usedjust brought in and has yet to be used

Page 45: Chapter 9:  Virtual Memory

4545

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of FramesAllocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 46: Chapter 9:  Virtual Memory

4646

Allocation of FramesAllocation of FramesEach process needs a Each process needs a minimumminimum number of pages number of pages

Example: IBM 370 – 6 pages to handle SS MOVE Example: IBM 370 – 6 pages to handle SS MOVE instruction:instruction: instruction is 6 bytes, might span 2 pagesinstruction is 6 bytes, might span 2 pages 2 pages to handle 2 pages to handle fromfrom 2 pages to handle 2 pages to handle toto

Two major allocation schemesTwo major allocation schemes fixed allocationfixed allocation priority allocationpriority allocation

Page 47: Chapter 9:  Virtual Memory

4747

Fixed AllocationFixed Allocation

Equal allocation – For example, if Equal allocation – For example, if there are 100 frames and 5 processes, there are 100 frames and 5 processes, give each process 20 frames.give each process 20 frames.

Proportional allocation – Allocate Proportional allocation – Allocate according to the size of processaccording to the size of process

mSs

pa

m

sS

ps

iii

i

ii

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

a

a

s

s

m

i

Page 48: Chapter 9:  Virtual Memory

4848

Priority AllocationPriority Allocation

Use a proportional allocation scheme Use a proportional allocation scheme using priorities rather than sizeusing priorities rather than size

If process If process PPii generates a page fault, generates a page fault, select for replacement one of its framesselect for replacement one of its frames select for replacement a frame from a select for replacement a frame from a

process with lower priority numberprocess with lower priority number

Page 49: Chapter 9:  Virtual Memory

4949

Global vs. Local AllocationGlobal vs. Local Allocation

Global replacementGlobal replacement – process selects a – process selects a replacement frame from the set of all frames; replacement frame from the set of all frames; one process can take a frame from anotherone process can take a frame from another

Local replacementLocal replacement – each process selects – each process selects from only its own set of allocated framesfrom only its own set of allocated frames

Page 50: Chapter 9:  Virtual Memory

5050

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 51: Chapter 9:  Virtual Memory

5151

ThrashingThrashingIf a process does not have “enough” pages, If a process does not have “enough” pages, the page-fault rate is very high. This leads to:the page-fault rate is very high. This leads to: low CPU utilizationlow CPU utilization operating system thinks that it needs to operating system thinks that it needs to

increase the degree of multiprogrammingincrease the degree of multiprogramming another process is added to the systemanother process is added to the system

ThrashingThrashing a process is kept busy swapping a process is kept busy swapping pages in and outpages in and out

Page 52: Chapter 9:  Virtual Memory

5252

Thrashing (Cont.)Thrashing (Cont.)

Page 53: Chapter 9:  Virtual Memory

5353

Demand Paging and Demand Paging and Thrashing Thrashing

Why does demand paging work?Why does demand paging work?Locality modelLocality model Process migrates from one locality to anotherProcess migrates from one locality to another Localities may overlapLocalities may overlap

Why does thrashing occur?Why does thrashing occur? size of locality > total memory size size of locality > total memory size

Page 54: Chapter 9:  Virtual Memory

5454

Locality In A Memory-Reference Locality In A Memory-Reference PatternPattern

Page 55: Chapter 9:  Virtual Memory

5555

Working-Set ModelWorking-Set Model working-set window working-set window a fixed number of page a fixed number of page references references Example: 10,000 instructionExample: 10,000 instruction

WSSWSSii (working set of Process (working set of Process PPii) = total number of ) = total number of

pages referenced in the most recent pages referenced in the most recent (varies in time) (varies in time) if if too small will not encompass entire locality too small will not encompass entire locality if if too large will encompass several localities too large will encompass several localities if if = = will encompass entire program will encompass entire program

DD = = WSSWSSii total demand frames total demand frames

if if DD > > mm Thrashing - (m is nr of available frames) Thrashing - (m is nr of available frames)

Policy if Policy if DD > m, then suspend one of the processes > m, then suspend one of the processes

Page 56: Chapter 9:  Virtual Memory

5656

Working-set modelWorking-set model

Page 57: Chapter 9:  Virtual Memory

5757

Keeping Track of the Working SetKeeping Track of the Working SetApproximate with interval timer + a reference bitApproximate with interval timer + a reference bit

Example: Example: = 10,000 = 10,000 Timer interrupts after every 5000 time unitsTimer interrupts after every 5000 time units Keep in memory 2 bits for each pageKeep in memory 2 bits for each page Whenever a timer interrupts copy and sets the values of Whenever a timer interrupts copy and sets the values of

all reference bits to 0all reference bits to 0 If one of the bits in memory = 1 If one of the bits in memory = 1 page in working set page in working set

Why is this not completely accurate?Why is this not completely accurate?

Improvement = 10 bits and interrupt every 1000 Improvement = 10 bits and interrupt every 1000 time unitstime units

Page 58: Chapter 9:  Virtual Memory

5858

Page-Fault Frequency SchemePage-Fault Frequency SchemeEstablish “acceptable” page-fault rateEstablish “acceptable” page-fault rate If actual rate too low, process loses frameIf actual rate too low, process loses frame If actual rate too high, process gains frameIf actual rate too high, process gains frame

Page 59: Chapter 9:  Virtual Memory

5959

Working Sets and Page Fault Working Sets and Page Fault RatesRates

Page 60: Chapter 9:  Virtual Memory

6060

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 61: Chapter 9:  Virtual Memory

6161

Memory-Mapped FilesMemory-Mapped Files

Memory-mapped file I/O allows file I/O to be Memory-mapped file I/O allows file I/O to be treated as routine memory access by treated as routine memory access by mappingmapping a disk block to a page in memorya disk block to a page in memory

A file is initially read using demand paging. A A file is initially read using demand paging. A page-sized portion of the file is read from the page-sized portion of the file is read from the file system into a physical page. Subsequent file system into a physical page. Subsequent reads/writes to/from the file are treated as reads/writes to/from the file are treated as ordinary memory accesses.ordinary memory accesses.

Page 62: Chapter 9:  Virtual Memory

6262

Memory-Mapped FilesMemory-Mapped Files

Simplifies file access by treating file I/O Simplifies file access by treating file I/O through memory rather than through memory rather than read()read() write()write() system calls system calls

Also allows several processes to map the Also allows several processes to map the same file allowing the pages in memory to same file allowing the pages in memory to be sharedbe shared

Page 63: Chapter 9:  Virtual Memory

6363

Memory Mapped FilesMemory Mapped Files

Page 64: Chapter 9:  Virtual Memory

6464

Memory-Mapped Shared Memory Memory-Mapped Shared Memory in Windowsin Windows

Page 65: Chapter 9:  Virtual Memory

6565

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 66: Chapter 9:  Virtual Memory

6666

Allocating Kernel MemoryAllocating Kernel Memory

Treated differently from user memoryTreated differently from user memory

Often allocated from a free-memory poolOften allocated from a free-memory pool Kernel requests memory for structures of Kernel requests memory for structures of

varying sizesvarying sizes Some kernel memory needs to be contiguousSome kernel memory needs to be contiguous

Page 67: Chapter 9:  Virtual Memory

6767

Buddy SystemBuddy SystemAllocates memory from fixed-size segment Allocates memory from fixed-size segment consisting of physically-contiguous pagesconsisting of physically-contiguous pages

Memory allocated using Memory allocated using power-of-2 allocatorpower-of-2 allocator Satisfies requests in units sized as power of 2Satisfies requests in units sized as power of 2 Request rounded to next highest power of 2Request rounded to next highest power of 2 When smaller allocation needed than is When smaller allocation needed than is

available, current chunk split into two buddies of available, current chunk split into two buddies of next-lower power of 2next-lower power of 2

Continue until appropriate sized chunk Continue until appropriate sized chunk availableavailable

Page 68: Chapter 9:  Virtual Memory

6868

Buddy System AllocatorBuddy System Allocator

Page 69: Chapter 9:  Virtual Memory

6969

Slab AllocatorSlab Allocator

Alternate strategyAlternate strategy

SlabSlab is one or more physically contiguous pagesis one or more physically contiguous pages

CacheCache consists of one or more slabsconsists of one or more slabs

Single cache for each unique kernel data structureSingle cache for each unique kernel data structure Each cache filled with Each cache filled with objectsobjects – instantiations of – instantiations of

the data structurethe data structure

Page 70: Chapter 9:  Virtual Memory

7070

Slab AllocatorSlab Allocator

When cache is created, it is filled with objects When cache is created, it is filled with objects marked as marked as freefree

When structures are stored, objects marked as When structures are stored, objects marked as usedused

If slab is full of used objects, the next object is If slab is full of used objects, the next object is allocated from an empty slaballocated from an empty slab If there are no empty slabs, a new slab If there are no empty slabs, a new slab

allocatedallocated

Benefits include no fragmentation, fast memory Benefits include no fragmentation, fast memory request satisfactionrequest satisfaction

Page 71: Chapter 9:  Virtual Memory

7171

Slab AllocationSlab Allocation

Page 72: Chapter 9:  Virtual Memory

7272

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 73: Chapter 9:  Virtual Memory

7373

Other Issues -- PrepagingOther Issues -- PrepagingPrepaging Prepaging To reduce the large number of page faults that occurs To reduce the large number of page faults that occurs

at process startupat process startup Prepage all or some of the pages a process will need, Prepage all or some of the pages a process will need,

before they are referencedbefore they are referenced But if prepaged pages are unused, I/O and memory But if prepaged pages are unused, I/O and memory

was wastedwas wasted Assume Assume ss pages are prepaged and pages are prepaged and αα of the pages is of the pages is

usedused

Is cost of Is cost of s * s * αα save pages faults > or < than the cost save pages faults > or < than the cost of prepagingof prepaging s * (1- s * (1- αα) ) unnecessary pagesunnecessary pages? ?

αα near zero near zero prepaging loses prepaging loses

Page 74: Chapter 9:  Virtual Memory

7474

Other Issues – Page SizeOther Issues – Page Size

Page size selection must take into Page size selection must take into consideration:consideration: fragmentationfragmentation table size table size I/O overheadI/O overhead localitylocality

Page 75: Chapter 9:  Virtual Memory

7575

Other Issues – TLB Reach Other Issues – TLB Reach

TLB ReachTLB Reach - The amount of memory accessible from - The amount of memory accessible from the TLBthe TLB

TLB Reach = (TLB Size) X (Page Size)TLB Reach = (TLB Size) X (Page Size)

Ideally, the working set of each process is stored in Ideally, the working set of each process is stored in the TLBthe TLB Otherwise there is a high degree of page faultsOtherwise there is a high degree of page faults

Page 76: Chapter 9:  Virtual Memory

7676

Increase the Page SizeIncrease the Page Size This may lead to an increase in fragmentation This may lead to an increase in fragmentation

as not all applications require a large page as not all applications require a large page sizesize

Provide Multiple Page SizesProvide Multiple Page Sizes This allows applications that require larger This allows applications that require larger

page sizes the opportunity to use them page sizes the opportunity to use them without an increase in fragmentationwithout an increase in fragmentation

Page 77: Chapter 9:  Virtual Memory

7777

Other Issues – Program Other Issues – Program StructureStructure

Program structureProgram structure Int[128,128] data;Int[128,128] data; Each row is stored in one page Each row is stored in one page Program 1 Program 1

for (j = 0; j <128; j++)for (j = 0; j <128; j++) for (i = 0; i < 128; i++) for (i = 0; i < 128; i++) data[i,j] = 0; data[i,j] = 0; 128 x 128 = 16,384 page faults 128 x 128 = 16,384 page faults

Page 78: Chapter 9:  Virtual Memory

7878

Other Issues – Program Other Issues – Program StructureStructure

Program 2 Program 2

for (i = 0; i < 128; i++)for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) for (j = 0; j < 128; j++) data[i,j] = 0; data[i,j] = 0;

128 page faults in contrast to 128 x 128 128 page faults in contrast to 128 x 128 = 16,384 page faults != 16,384 page faults !

Page 79: Chapter 9:  Virtual Memory

7979

Other Issues – I/O interlockOther Issues – I/O interlock

I/O InterlockI/O Interlock – Pages must sometimes be – Pages must sometimes be locked into memorylocked into memory

Consider I/O - Pages that are used for copying Consider I/O - Pages that are used for copying a file from a device must be locked from being a file from a device must be locked from being selected for eviction by a page replacement selected for eviction by a page replacement algorithmalgorithm

Page 80: Chapter 9:  Virtual Memory

8080

Reason Why Frames Used For I/O Reason Why Frames Used For I/O Must Be In MemoryMust Be In Memory

Page 81: Chapter 9:  Virtual Memory

8181

Chapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundBackground

Demand PagingDemand Paging

Copy-on-WriteCopy-on-Write

Page ReplacementPage Replacement

Allocation of Frames Allocation of Frames

ThrashingThrashing

Memory-Mapped FilesMemory-Mapped Files

Allocating Kernel MemoryAllocating Kernel Memory

Other ConsiderationsOther Considerations

Operating-System ExamplesOperating-System Examples

Page 82: Chapter 9:  Virtual Memory

8282

Operating System ExamplesOperating System ExamplesWindows XPWindows XP

Solaris Solaris

Page 83: Chapter 9:  Virtual Memory

8383

Windows XPWindows XP

Uses demand paging with Uses demand paging with clusteringclustering. . Clustering brings in pages surrounding the Clustering brings in pages surrounding the faulting pagefaulting page

Processes are assigned a Processes are assigned a working setworking set minimumminimum and and working set maximumworking set maximum

Working set minimum is the minimum Working set minimum is the minimum number of pages the process is guaranteed number of pages the process is guaranteed to have in memoryto have in memory

Page 84: Chapter 9:  Virtual Memory

8484

A process may be assigned as many A process may be assigned as many pages up to its working set maximumpages up to its working set maximum

When the amount of free memory in the When the amount of free memory in the system falls below a threshold, system falls below a threshold, automaticautomatic working set trimmingworking set trimming is performed to is performed to restore the amount of free memoryrestore the amount of free memory

Working set trimming removes pages from Working set trimming removes pages from processes that have pages in excess of processes that have pages in excess of their working set minimumtheir working set minimum

Page 85: Chapter 9:  Virtual Memory

8585

Solaris Solaris

Maintains a list of free pages to assign Maintains a list of free pages to assign faulting processesfaulting processes

LotsfreeLotsfree – threshold parameter (amount of – threshold parameter (amount of free memory) to begin pagingfree memory) to begin paging

DesfreeDesfree – threshold parameter to increasing – threshold parameter to increasing pagingpaging

MinfreeMinfree – threshold parameter to being – threshold parameter to being swappingswapping

Page 86: Chapter 9:  Virtual Memory

8686

Paging is performed by a Paging is performed by a pageoutpageout process process

PageoutPageout scans pages using modified clock scans pages using modified clock algorithmalgorithm

ScanrateScanrate is the rate at which pages are is the rate at which pages are scanned. This ranges from scanned. This ranges from slowscanslowscan to to fastscanfastscan

Pageout is called more frequently Pageout is called more frequently depending upon the amount of free depending upon the amount of free memory availablememory available

Page 87: Chapter 9:  Virtual Memory

8787

Solaris 2 Page ScannerSolaris 2 Page Scanner

Page 88: Chapter 9:  Virtual Memory

8888

End of Chapter 9End of Chapter 9