47
1 Virtual memory Virtual memory Operations and policies Operations and policies Chapters 3.4. Chapters 3.4. – 3.6 3.6 Week 2: 17.9.2009 Tiina Niklander

Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Embed Size (px)

Citation preview

Page 1: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

1

Virtual memoryVirtual memoryOperations and policiesOperations and policies

Chapters 3.4.Chapters 3.4. –– 3.63.6

Week 2: 17.9.2009 Tiina Niklander

Page 2: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Policies and methodsPolicies and methods

Fetch policyFetch policy (Noutopolitiikka)(Noutopolitiikka)When to load page to memory?When to load page to memory?

Placement policyPlacement policy(Sijoituspolitiikka )(Sijoituspolitiikka )

Where to place the new page?Where to place the new page?

Replacement policyReplacement policy(Korvaus/poistopolitiikka)(Korvaus/poistopolitiikka)

Which page to be evicted andWhich page to be evicted andreplacedreplaced

CCleaningleaning policypolicy (Levylle(Levyllekirjoituskirjoitus ––politiikka)politiikka)

When to evict a modifiedWhen to evict a modifiedpage?page?

Multiprogramming, loadMultiprogramming, loadcontrolcontrol (Moniajoaste)(Moniajoaste)

How many processes in theHow many processes in thesystem at the same time?system at the same time?How much memory, howHow much memory, howmany pages per process?many pages per process?

Working setWorking set (Käyttöjoukko)(Käyttöjoukko)How many page framesHow many page framesallocated for one process?allocated for one process?(resident set)(resident set)Which areas have beenWhich areas have beenreferenced recently?referenced recently?

2

Page 3: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

(Fig 8.8 [Stal05])

Perform pagereplacement

Page 4: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Page faultPage fault ((SivunSivun puutospuutos))

If the requested page is not in memory, MMU causesIf the requested page is not in memory, MMU causesinterruptinterruptOS process the interrupt and notice a page faultOS process the interrupt and notice a page fault

Move the process to blocked stateMove the process to blocked stateAllocate page frame for the new pageAllocate page frame for the new pageStart page load from disk using driver to command controllerStart page load from disk using driver to command controllerSwitch another process for executionSwitch another process for execution

After the device interruptAfter the device interruptOS notices the termination of page transferOS notices the termination of page transferMove process to ready stateMove process to ready stateContinue the process now or later (scheduling decision)Continue the process now or later (scheduling decision)

4

Page 5: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Locking pagesLocking pages

To avoid page fault, page can be locked to memoryTo avoid page fault, page can be locked to memoryKernel pagesKernel pagesKey data structures of OSKey data structures of OSI/O buffers (at least during the transfer)I/O buffers (at least during the transfer)Process pages (at least in real time systems)Process pages (at least in real time systems)

How?How?Page frame table has a lock bit on the framePage frame table has a lock bit on the frame

Per framePer frame

Or page table entry has the lock bitOr page table entry has the lock bitPer processPer process

5

Page 6: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Page replacementPage replacement

When?When?Page faultPage faultMemory too full, no free frames (usually some kept free!)Memory too full, no free frames (usually some kept free!)Not enough free unallocated frames (limit value)Not enough free unallocated frames (limit value)

Which page to replace?Which page to replace?One, that is not needed in the (near) futureOne, that is not needed in the (near) future

Predict based on the past behaviourPredict based on the past behaviourLocality principle helps: no more references to the page, processLocality principle helps: no more references to the page, processpassed that phasepassed that phaseSome time mistakes happenSome time mistakes happen

6

See Fig 8.1 [Stal05]

Page 7: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

7

Fig 8.1 [Sta05]

Fig. 9.19[SGG07]

Page 8: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Page Replacement AlgorithmsPage Replacement Algorithms

Page fault forces choicePage fault forces choicewhich page must be removedwhich page must be removedmake room for incoming pagemake room for incoming page

Modified page must first be savedModified page must first be savedunmodified just overwrittenunmodified just overwritten

Better not to choose an often used pageBetter not to choose an often used pagewill probably need to be brought back in soonwill probably need to be brought back in soon

8

Page 9: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

9

KorvauspolitiikkaKorvauspolitiikka

Page 10: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

OptimalOptimalPage Replacement AlgorithmPage Replacement Algorithm

Replace page needed at the farthest point in futureReplace page needed at the farthest point in futureOptimal but unrealizableOptimal but unrealizableClairvoyant (not possible!)Clairvoyant (not possible!)can be found afterwards from tracecan be found afterwards from trace

Used as a reference point for other algorithmsUsed as a reference point for other algorithmsNothing is better that optimal!Nothing is better that optimal!

Estimate by …Estimate by …logging page use on previous runs of processlogging page use on previous runs of processalthough this is impracticalalthough this is impractical

10

Page 11: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

FIFO (FirstFIFO (First--In FirstIn First--Out)Out)Page Replacement AlgorithmPage Replacement Algorithm

Replace the page that has been in the memory longestReplace the page that has been in the memory longesttimetimePage at beginning of list replacedPage at beginning of list replacedMaintain a linked list of all pagesMaintain a linked list of all pages

In order they came into memoryIn order they came into memoryAlternatively: store the load time on each pageAlternatively: store the load time on each page

DisadvantageDisadvantagePage in memory the longest may be often used and shouldPage in memory the longest may be often used and shouldnot be evictednot be evicted

11

Page 12: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

12

Second ChanceSecond ChancePages sorted in FIFO orderPages sorted in FIFO orderUse reference bit R and modified bit MUse reference bit R and modified bit MIf the page to be evicted has been recently used (R=1) give itIf the page to be evicted has been recently used (R=1) give itsecond chance; move to end of queue and set R=0second chance; move to end of queue and set R=0

Will be evicted, if all other pages are also given second chanceWill be evicted, if all other pages are also given second chance

at time 20R=1

R=0

(Fig 4-16 [Tane01])

Page 13: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Not Recently UsedNot Recently UsedPage Replacement AlgorithmPage Replacement Algorithm

Each page has Reference bit, Modified bitEach page has Reference bit, Modified bitbits are set when page is referenced, modifiedbits are set when page is referenced, modifiedR cleared periodically, M cleared at disk writesR cleared periodically, M cleared at disk writes

Pages are classifiedPages are classified1.1. not referenced, not modifiednot referenced, not modified2.2. not referenced, modifiednot referenced, modified3.3. referenced, not modifiedreferenced, not modified4.4. referenced, modifiedreferenced, modified

NRU removes page at randomNRU removes page at randomfrom lowest numbered non empty classfrom lowest numbered non empty class

13

Page 14: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

The Clock Page Replacement Alg.The Clock Page Replacement Alg.

Go trough all pages in circular fashionGo trough all pages in circular fashionTry to locate unused page with the NRU classification,Try to locate unused page with the NRU classification,used page gets second chanceused page gets second chance

14

Page 15: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

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

Recently used pages assumed to be used again soonRecently used pages assumed to be used again soonEvict page that has been unused for longest timeEvict page that has been unused for longest time

How to find?How to find?

Heavy bookkeepingHeavy bookkeepingUpdate each reference time to the pagesUpdate each reference time to the pagesMaintain pages in the referenced order (linked list)Maintain pages in the referenced order (linked list)

Infeasible if followed strictInfeasible if followed strict

15

Page 16: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

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

Strict alternatives:Strict alternatives:Keep a linked list of pages in reference order, update this list on everyKeep a linked list of pages in reference order, update this list on everymemory reference !!memory reference !!Keep counter in each page table entryKeep counter in each page table entry

UseUse special hardwarespecial hardware to increment the counterto increment the counterchoose page with lowest value counterchoose page with lowest value counterperiodically zero the counterperiodically zero the counter

Simulating algorithms: NFU, AgingSimulating algorithms: NFU, AgingNFUNFU –– Not Frequently usedNot Frequently usedAgingAging

In the adding: shift counter right, add R bit to leftmostIn the adding: shift counter right, add R bit to leftmostForgets the historyForgets the history

16

Page 17: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

LRU hardware using matrixLRU hardware using matrix

17

pages referenced in order 0,1,2,3,2,1,0,3,2,3

On reference: set row all 1s and column all 0sOn reference: set row all 1s and column all 0sAt any time the row with lowest binary value is LFU pageAt any time the row with lowest binary value is LFU page

Page 18: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Aging algorithm simulates LRUAging algorithm simulates LRU

On each clock time process the counters:On each clock time process the counters:Shift right one bitShift right one bitSet leftSet left--most bit to one on pages referenced after previous tickmost bit to one on pages referenced after previous tick

18

Page 19: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

19

Working set modelWorking set model

Page 20: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Resident Set vs. Working SetResident Set vs. Working Set

Working set =Working set = Set of pages the process is currently usingSet of pages the process is currently usingw(w(k,tk,t)) is the size of the working set at time,is the size of the working set at time, tt

Resident set =Resident set = Page frames currently allocated for processPage frames currently allocated for process

Number of allocated page framesNumber of allocated page frames (sivukehysten lkm)(sivukehysten lkm)Too smallToo small

more processes in the memory, more page faultsmore processes in the memory, more page faultsToo many? Trashing?Too many? Trashing? (ruuhkautuminen)(ruuhkautuminen)

Too largeToo largeoccupy space that beneficial for other processesoccupy space that beneficial for other processesCPU utilisationCPU utilisation (käyttöaste)(käyttöaste) too lowtoo low”wasted space””wasted space”

20

Page 21: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Working Set StrategyWorking Set Strategy

Pages used by the most recent k memory referencesPages used by the most recent k memory references(window size)(window size)Common approximationCommon approximation

Pages used during the recent t msec of current virtual time,Pages used during the recent t msec of current virtual time,time that the process has been actually executingtime that the process has been actually executing

Pages not in the working set, can be evictedPages not in the working set, can be evictedFor page replacement:For page replacement:

Hardware set the R and M bitsHardware set the R and M bitsEach page entry has ’time of last use’Each page entry has ’time of last use’

21

),(tW

Page 22: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

The Working Set AlgorithmThe Working Set Algorithm

22

In case all pages in working set,In case all pages in working set,evicts the oldest unreferenced and/or unmodified pageevicts the oldest unreferenced and/or unmodified page

Page 23: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

TheThe WSClockWSClockPage Replacement AlgorithmPage Replacement Algorithm

As clock, but onlyAs clock, but onlyresidence setresidence setEvict unmodified pageEvict unmodified pagenot in working set andnot in working set andSchedule for disk writeSchedule for disk writemodified page not in WSmodified page not in WSIf no candidate pageIf no candidate pagefound during one round,found during one round,wait for writes or evictwait for writes or evictany clean (=unmodified)any clean (=unmodified)WS pageWS page

23

Page 24: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Review of Page ReplacementReview of Page ReplacementAlgorithmsAlgorithms

24

Page 25: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Cleaning PolicyCleaning Policy

Need for a background process, paging daemonNeed for a background process, paging daemonperiodically inspects state of memoryperiodically inspects state of memory

When too few frames are freeWhen too few frames are freeselects pages to evict using a replacement algorithmselects pages to evict using a replacement algorithm

It can use same circular list (clock)It can use same circular list (clock)as regular page replacement algorithm but with differentas regular page replacement algorithm but with differentpointerpointer

25

Page 26: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

When to clean the page?When to clean the page?(write to disk)(write to disk)

Only when changedOnly when changedDemand cleaningDemand cleaning (tarvetalletus)(tarvetalletus)

Write to disk only when the page frame is neededWrite to disk only when the page frame is neededLong delay in freeing, crash?Long delay in freeing, crash?

PrecleaningPrecleaning (ennaltatalletus)(ennaltatalletus)Write to disk periodically in bigger groupsWrite to disk periodically in bigger groups

Cleaning the freed page frameCleaning the freed page frameZeroing might be wasted workZeroing might be wasted workWhat if the page is needed soon again?What if the page is needed soon again?

unmodified free page might be reusedunmodified free page might be reused

What if the disk utilization is low (no work on disk)?What if the disk utilization is low (no work on disk)? 26

Page 27: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Page bufferingPage buffering

Maintain certain number of free page framesMaintain certain number of free page frames~~ page frame cache (of freed pages frames)page frame cache (of freed pages frames)

Fast recovery from page frame buffer, if the page on freedFast recovery from page frame buffer, if the page on freedframe is referencedframe is referenced

Page marked to be cleanedPage marked to be cleanedAdd to free page frame list, if not modifiedAdd to free page frame list, if not modifiedAdd to writeAdd to write--toto--disk list, if modifieddisk list, if modified

Page still on the same allocated page framePage still on the same allocated page frameRemove the reference from page tableRemove the reference from page table

Allocate page frames from the free frames list usingAllocate page frames from the free frames list usingFIFO orderFIFO order 27

Page 28: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

28

UNIX:UNIX: Block cacheBlock cache

Page buffersPage buffers, block caches, block caches -- thethe content in separate areacontent in separate areaThe data structure, hash table and linked list, only has managementThe data structure, hash table and linked list, only has managementinformation and link to the actual locationinformation and link to the actual locationHash key: device#, block#Hash key: device#, block#

Linux has just one buffer for process pages and file blocksLinux has just one buffer for process pages and file blocks

Tan08 4-28

Page 29: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Separation of Policy andSeparation of Policy andMechanismMechanism

Advantages:Advantages:Way to manageWay to managecomplexitycomplexityMore modular codeMore modular code

Disadvantages:Disadvantages:Split between user andSplit between user andkernel spaceskernel spacesExtra overheadExtra overheadMore mode switchesMore mode switchesMore message passingMore message passing

External pager in user modeExternal pager in user modePage mapping, swap areaPage mapping, swap area

Page fault handler in kernelPage fault handler in kernelAsk for page from external pagerAsk for page from external pager

29

Page 30: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Design IssuesDesign Issues

30

Page 31: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

31

Page SizePage Size –– Design IssuesDesign Issues

Reduce internal fragmentationReduce internal fragmentation smallsmall

Reduce page table sizeReduce page table size largelargeProportional (1x, 2x, …) to disk block sizeProportional (1x, 2x, …) to disk block sizeOptimal value is different for different programsOptimal value is different for different programs

Increase TLB hit ratioIncrease TLB hit ratio largelargeDifferent page size for different applications?Different page size for different applications?How does MMU know the page size?How does MMU know the page size?

Tbl 8.2 [Stal05]

Page 32: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

32

Page 33: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

33

PagePage SizeSizeOverhead due to page table and internal fragmentationOverhead due to page table and internal fragmentation

WhereWheres = average process size in bytess = average process size in bytesp = page size in bytesp = page size in bytese = page entrye = page entry

2s e poverheadp

page table space

internalfragmentation

Optimized when

2p se

s = 1 MB e = 8B popt = 4 KBs = 100 MB e = 8B popt = 40 KB

Page 34: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

34

Page fault ratePage fault rateMore small pages to the sameMore small pages to the samememory spacememory spaceReferences from large pagesReferences from large pagesmore probable to go to a pagemore probable to go to a pagenot yet in memorynot yet in memoryReferences from small pagesReferences from small pagesoften to other pagesoften to other pages --> often> oftenused pages selected to theused pages selected to thememorymemory

Too small working set sizeToo small working set size -->>larger page fault ratelarger page fault rateLarge working set sizeLarge working set size -->>nearly all pages in the memorynearly all pages in the memory

Page 35: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

WorkingWorking set sizeset size

Set of pages used duringSet of pages used duringlast k references (windowlast k references (windowsize)size)Pages not in the workingPages not in the workingset are candidates to beset are candidates to bereleased or replacedreleased or replacedSuitable size for theSuitable size for theworking set? Estimateworking set? Estimatebased on page fault ratebased on page fault rate

35

),min(),(1 ntW

Page 36: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Page Fault RatePage Fault Rate

Page fault rate as a function of the number of page frames assignedPage fault rate as a function of the number of page frames assignedPage Fault Frequency algorithm (PFF):Page Fault Frequency algorithm (PFF):

Keep page fault rate between A and B for each processKeep page fault rate between A and B for each processIncrease residence set size at A and reduce at BIncrease residence set size at A and reduce at B

36

Page 37: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Load ControlLoad Control

Despite good designs, system may still thrashDespite good designs, system may still thrash

When Page Fault Frequency (PFF) algorithm indicatesWhen Page Fault Frequency (PFF) algorithm indicatessome processes need more memorysome processes need more memorybut no processes need lessbut no processes need less

Solution :Solution :Reduce number of processes competing for memoryReduce number of processes competing for memory

swap one or more to disk, divide up pages they heldswap one or more to disk, divide up pages they heldreconsider degree of multiprogrammingreconsider degree of multiprogramming

37

Page 38: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Allocation and replacementAllocation and replacement

38Tbl 8.4 [Stal05]

Page 39: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

LocalLocal versus Global Allocation Policiesversus Global Allocation Policies

39

(a) Original(a) Original configurationconfiguration(b) Local(b) Local page replacementpage replacement(c) Global(c) Global page replacementpage replacement

Page 40: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Shared pages and librariesShared pages and libraries

Several processes use, just one copy in memorySeveral processes use, just one copy in memoryShared pages readShared pages read--onlyonlySame page frame pointed by multiple page tablesSame page frame pointed by multiple page tables

Code must be positionCode must be position--independentindependentCode must be reCode must be re--entrant, does not change duringentrant, does not change duringexecutionexecution

A special case ofA special case ofmemorymemory--mapped filesmapped files

40

Page 41: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Sharing librariesSharing libraries

Old style: Link the library with program staticallyOld style: Link the library with program staticallyNew style: Link the library at run time dynamicallyNew style: Link the library at run time dynamicallyWindows: DLLWindows: DLL –– dynamic link librarydynamic link librarySave spaceSave space

Just one copy in the memoryJust one copy in the memory

Removing bugsRemoving bugsJust make one correction in the shared libraryJust make one correction in the shared library

Save recompilation timeSave recompilation timeLibrary change do not require new linkingLibrary change do not require new linking

41

Page 42: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Sharing: example editorSharing: example editor

42

Page 43: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Sharing a memorySharing a memory--mapped filemapped file

Mapped file is an alternative of I/O, file is accessed as a bigMapped file is an alternative of I/O, file is accessed as a bigcharacter array in memorycharacter array in memoryCan be used as shared memory between processesCan be used as shared memory between processes 43

Tan08 Fig 10-3

Page 44: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Swap area, VM backup storeSwap area, VM backup store(Heittovaihtoalue)(Heittovaihtoalue)

StaticStaticReserved in advanceReserved in advanceSpace for whole processSpace for whole process

Copy the code/text at start orCopy the code/text at start orReserve space, but swapReserve space, but swapgradually based on needgradually based on need

PCB has swap location infoPCB has swap location info

DynamicDynamicReserved space for each pageReserved space for each pagewhen neededwhen neededPage table has swap blockPage table has swap blocknumber for the pagenumber for the page

OR separate disk map to storeOR separate disk map to storepage locations on swappage locations on swap

No space reservation forNo space reservation forpages that are never storedpages that are never storedon swapon swap

44

Swap location:Swap location:Windows: pagefile.sys, win386.swpWindows: pagefile.sys, win386.swpLinux: swap partitionLinux: swap partition

Page 45: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Backing Store / Swap areaBacking Store / Swap area

45

(a) Paging to static swap area(a) Paging to static swap area(b) Backing up pages dynamically(b) Backing up pages dynamically

Page 46: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Page FaultPage Fault HandlingHandling ––more detailsmore details

1.1. Hardware traps to kernelHardware traps to kernel2.2. General registers savedGeneral registers saved3.3. OS determines which virtual page neededOS determines which virtual page needed4.4. OS checks validity of address, seeks page frameOS checks validity of address, seeks page frame5.5. If selected frame is dirty, write it to diskIf selected frame is dirty, write it to disk6.6. OS schedules disk operation to bring new page in from diskOS schedules disk operation to bring new page in from disk7.7. Page tables updatedPage tables updated8.8. Faulting instruction backed up to when it beganFaulting instruction backed up to when it began9.9. Faulting process scheduledFaulting process scheduled10.10. Registers restoredRegisters restored

11.11. Program continuProgram continueses46

Page 47: Virtual memory Operations and policies - cs. · PDF fileVirtual memory Operations and policies ... Memory too full, ... Pages sorted in FIFO order Use reference bit R and modified

Operating System Involvement with PagingOperating System Involvement with Paging

Process creationProcess creationdetermine program sizedetermine program sizecreate page tablecreate page table

Process executionProcess executionMMU reset for new processMMU reset for new processTLB flushedTLB flushed

Page fault timePage fault timedetermine virtual address causing faultdetermine virtual address causing faultswap target page out, needed page inswap target page out, needed page in

Process termination timeProcess termination timerelease page table, pagesrelease page table, pages 47