chapter objectives virtual memory 介绍虚拟内存系统的益处 to describe the benefits of a...

75

Click here to load reader

Upload: nicholas-dennis

Post on 30-Dec-2015

297 views

Category:

Documents


12 download

TRANSCRIPT

1.01

9.1 BackgroundIn many cases, the entire program is not needed. The ability to execute a program that is only partially in memory would confer many benefits: A program would no longer be constrained by the amount of physical memory.More programs could be run at the same time, with a corresponding increase in CPU utilization and throughput.Less I/O would be needed to load or swap each user program into memory, so each user program would run faster.

Operating SystemsX.J.Lee 20149.49.2 Demand Paging Transfer of a Paged Memory to Contiguous Disk Space

Operating SystemsX.J.Lee 20149.89.3 Page Replacement Prevent over-allocation of memory by modifying page-fault service routine to include page replacement.Use modify (dirty) bit to reduce overhead of page transfersonly modified pages are written to disk.Page replacement completes separation between logical memory and physical memorylarge virtual memory can be provided on a smaller physical memory.

Operating SystemsX.J.Lee 20149.269.4 Allocation of Frames

Operating SystemsX.J.Lee 20149.569.5 Thrashing /Thrashinga process is busy swapping pages in and out.

Operating SystemsX.J.Lee 20149.65Virtual memory can be implemented via:Demand paging Demand segmentation However, segment-replacement algorithms are more complex than are page-replacement algorithms because the segments have variable sizes.paged segmentation Operating SystemsX.J.Lee 20149.6Figure Virtual Memory That is Larger Than Physical Memory

Operating SystemsX.J.Lee 20147A demand-paging system is similar to a paging system with swapping. But we use a lazy swapperpager ()Swapper vs PagerA pager never swaps a page into memory unless that page will be needed.A swapper manipulates entire processes.Operating SystemsX.J.Lee 20149.99Basic Concepts Bring a page into memory only when it is needed. Less I/O neededLess memory needed Faster responseMore usersPage is needed reference to itinvalid reference abortnot-in-memory bring to memoryOperating SystemsX.J.Lee 20149.10Valid-Invalid Bit validthe associated page is both legal and in memory.invalid the page either is not valid(that is ,not in the logical address space of the process),or is valid but is currently on the disk.Operating SystemsX.J.Lee 20149.1111Initially validinvalid bit is set to 0 on all entries.During address translation, if validinvalid bit in page table entry is 0 page fault.1111000Frame #valid-invalid bitpage tableOperating SystemsX.J.Lee 20149.12Page Table When Some Pages Are Not in Main Memory

Operating SystemsX.J.Lee 201413Page Fault If there is ever a reference to a page, first reference will trap to OS page faultOperating SystemsX.J.Lee 20149.14OS checks an internal table to decide: OS Invalid reference abort(terminate the process).Valid,Just not in memory page it in.Get empty frame. Swap page into frame. Reset tables, validation bit = 1. 1Restart instruction Operating SystemsX.J.Lee 20149.15Steps in Handling a Page Fault

Operating SystemsX.J.Lee 201416Pure demand paging Never bring a page into memory until it is required.Operating SystemsX.J.Lee 20149.17Theoretically, some programs may access several new pages of memory with each instruction execution one page for the instruction and many for datapossibly causing multiple page faults per instruction.Fortunately, programs tend to have locality of referenceOperating SystemsX.J.Lee 20149.18The hardware to support demand pagingPage table valid-invalid bit, protection bits Secondary memory swap space Operating SystemsX.J.Lee 20149.19What happens if there is no free frame?Page replacement find some page in memory, but not really in use, swap it out.Algorithm Performance want an algorithm which will result in minimum number of page faults.Operating SystemsX.J.Lee 20149.20Same page may be brought into memory several times.Operating SystemsX.J.Lee 20149.21Performance of Demand PagingPage Fault Rate 0 p 1.0if p = 0 no page faults if p = 1, every reference is a faultOperating SystemsX.J.Lee 20149.22Effective Access Time (EAT) EAT = (1 p) x ma + p x Page-fault service timeMathe memory access time, now ranges from 10 to 200 nanoseconds.Page-fault service timepage fault overhead + [swap page out ] + swap page in + restart overheadOperating SystemsX.J.Lee 20149.23ExampleMemory access time = 100 nanosecondsAn average page-fault service time=25 millisecondsEAT(in nanoseconds) = (1 p) x ma + p x Page-fault service time = (1 p) x 100 + p x 25,000,000 =100+24,999,900 x p The EAT is directly proportional to the page-fault rate.If we want less than 10-percent degradation,we need 110 > 100+25,000,000 x p 10 > 25,000,000 x p p < 0.0000004Operating SystemsX.J.Lee 20149.24Disk I/O to swap space is generally faster than that to the file system. I/Obecause swap space is allocated in much larger blocks,and file lookups and indirect allocation methods are not used.

Operating SystemsX.J.Lee 20149.25Need For Page Replacement

Operating SystemsX.J.Lee 201427Basic Page ReplacementPage ReplacementX.J.Lee 201428Operating Systems

The page-fault service routine including page replacement1. Find the location of the desired page on disk.2. Find a free frame:- If there is a free frame, use it.- If there is no free frame, use a page replacement algorithm to select a victim frame.-write the victim page to the disk; change the page and frame tables accordingly.Operating SystemsX.J.Lee 20149.293. Read the desired page into the (newly) free frame. Update the page and frame tables.4. Restart the process.Operating SystemsX.J.Lee 20149.30modify (dirty) bit The modify bit for a page is set by the hardware whenever any word or byte in the page is written into.When we select a victim, examine its modify bit.set: must write that page to disk. not set: if the copy of the page on the disk has not been overwritten (by some other page, for example), then we can avoid writing the page to the disk.Operating SystemsX.J.Lee 20149.31Two major problems for demand pagingFrame-allocation algorithmdecide how many frames to allocate to each process.Page-replacement algorithmselect the frames that are to be replaced.Want lowest page-fault rate.Operating SystemsX.J.Lee 20149.32Operating SystemsX.J.Lee 201433Graph of Page Faults Versus The Number of Frames

Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string.In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5Operating SystemsX.J.Lee 20149.34FIFO Page ReplacementReference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 51231234125349 page faults1231235124510 page faults443Operating SystemsX.J.Lee 20149.353 frames4 framesFIFO Replacement Beladys Anomalymore frames less page faults(not always true)X.J.Lee 20149.36Operating Systems

FIFO Illustrating Beladys AnamolyFIFO Page Replacement

Operating SystemsX.J.Lee 201437Optimal Page ReplacementReplace page that will not be used for longest period of time.4 frames example: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 512346 page faults45Operating SystemsX.J.Lee 20149.38Optimal Page Replacement

Operating SystemsX.J.Lee 201439How do you know this?Used for measuring how well your algorithm performs.X.J.Lee 201440Operating SystemsLeast Recently Used (LRU) Page ReplacementReference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 512354435Operating SystemsX.J.Lee 20149.41LRU Page Replacement

Operating SystemsX.J.Lee 201442Counter implementationEvery page entry has a counter; every time page is referenced through this entry, copy the clock into the counter.When a page needs to be changed, look at the counters to determine which are to change.Operating SystemsX.J.Lee 20149.43Stack implementation keep a stack of page numbers in a double link form:Page referenced:move it to the toprequires 6 pointers to be changedNo search for replacementOperating SystemsX.J.Lee 20149.44Use Of A Stack to Record The Most Recent Page References

Operating SystemsX.J.Lee 201445LRU Approximation AlgorithmsLRUAdditional-Reference-Bits AlgorithmWith each page associate a bit, initially = 0When page is referenced bit set to 1.Replace the one which is 0 (if one exists). We do not know the order, however.Operating SystemsX.J.Lee 20149.46Second-Chance AlgorithmNeed reference bit.Clock replacement.If page to be replaced (in clock order) has reference bit = 1. then:set reference bit 0.leave page in memory.replace next page (in clock order), subject to same rules.Operating SystemsX.J.Lee 20149.47Second-Chance (clock) Page-Replacement Algorithm

Operating SystemsX.J.Lee 201448Enhanced Second-Chance Algorithm by considering both the reference bit and modify bit as an ordered pair.1. (0,0)---best page to replace.2. (0,1)---not quite as good..3. (1,0)---it probably will be used again soon.4. (1,1)---worst page to replace.Operating SystemsX.J.Lee 20149.49Counting-Based Page ReplacementKeep a counter of the number of references that have been made to each page.LFULeast Frequently Used Algorithmreplaces page with smallest count.(An actively used page should have a large reference count.)MFU Most Frequently Used Algorithmbased on the argument that the page with the smallest count was probably just brought in and has yet to be used.Operating SystemsX.J.Lee 20149.50Page-Buffering Algorithms Used in addition to a specific page-replacement algorithm. Keep a pool of free framesWhen a page fault occurs, a victim frame is chosen as before. How ever, the desired page is read into a free frame from the pool before the victim is written out. When the victim is later written out, its frame is added to the free-frame pool.Operating SystemsX.J.Lee 20149.51Maintain a list of modified pagesWhenever the paging device is idle, a modified page is selected and is written to the disk.Its modify bit is then reset. (It will be clean when it is selected as a victim.)Operating SystemsX.J.Lee 20149.52Keep a pool of free frames, but to remember which page was in each frameSince the frame contents are not modified when a frame is written to the disk, the old page can be reused directly from the free-frame pool if it is needed before that frame is reused. No I/O is needed in this case.Operating SystemsX.J.Lee 20149.53Applications and Page ReplacementIn certain cases, applications accessing data through the operating systems virtual memory perform worse than if the operating system provided no buffering at all.Databaseprovides its own memory management and I/O bufferingdata warehousesMFU would actually be more efficient than LRUX.J.Lee 20149.54Operating SystemsBecause of such problems, some operating systems give special programs the ability to use a disk partition as a large sequential array of logical blocks, without any file-system data structures.Raw disk & raw I/Omost applications perform better when they use the regular file-system servicesX.J.Lee 20149.55Operating Systems

Minimum Number of Frames Each process needs minimum number of pages.This minimum number is defined by the instruction-set architecture.Example: IBM 370 6 pages to handle SS MOVE instruction:instruction is 6 bytes, might span 2 pages.2 pages to handle from.2 pages to handle to.Operating SystemsX.J.Lee 20149.57The minimum number of frames per process is defined by the architecture,Whereas the maximum number is defined by the amount of available physical memory.In between, we are still left with significant choice in frame allocation.Operating SystemsX.J.Lee 20149.58Allocation AlgorithmsTwo major allocation schemes.fixed allocation priority allocation Operating SystemsX.J.Lee 20149.59Fixed Allocation Equal allocatione.g., if 100 frames and 5 processes, give each 20 pages.Operating SystemsX.J.Lee 20149.60Proportional allocation Allocate according to the size of process.

Operating SystemsX.J.Lee 20149.61Priority Allocation Use a proportional allocation scheme using priorities rather than size.If process Pi generates a page fault,select for replacement one of its frames.select for replacement a frame from a process with lower priority number.Operating SystemsX.J.Lee 20149.62Global vs. Local AllocationGlobal replacement process selects a replacement frame from the set of all frames; one process can take a frame from another.Local replacement each process selects from only its own set of allocated frames.Operating SystemsX.J.Lee 20149.63Non-Uniform Memory AccessSystems in which memory access times vary significantly are known collectively as non-uniform memory access (NUMA) systemsX.J.Lee 20149.64Operating Systems

Cause of Thrashing If a process does not have enough pages, the page-fault rate is very high. This leads to:low CPU utilization.operating system thinks that it needs to increase the degree of multiprogramming.another process added to the system.Operating SystemsX.J.Lee 20149.66Thrashing

Operating SystemsX.J.Lee 20149.67Why does paging work?Locality modelProcess migrates from one locality to another.Localities may overlap.Why does thrashing occur? size of locality > total memory sizeOperating SystemsX.J.Lee 20149.68Locality In A Memory-Reference Pattern

Operating SystemsX.J.Lee 201469Working-Set Model working-set window working-set window a fixed number of page references Example: 10,000 instructionOperating SystemsX.J.Lee 20149.70

Working-set modelworking setWSi (working set of Process Pi) =total number of pages referenced in the most recent (varies in time)if too small will not encompass entire locality.if too large will encompass several localities.if = will encompass entire program.D = WSSi total demand frames if D > m ThrashingPolicy: if D > m, then suspend one of the processes.Operating SystemsX.J.Lee 20149.71Page-Fault Frequency Establish acceptable page-fault rate.If actual rate too low, process loses frame.If actual rate too high, process gains frame.

Operating SystemsX.J.Lee 20149.72Concluding RemarksThrashing and the resulting swapping have a disagreeably large impact on performance. The current best practice in implementing a computer facility is to include enough physical memory, whenever possible, to avoid thrashing and swapping.From smartphones through mainframes, providing enough memory to keep all working sets in memory concurrently, except under extreme conditions, gives the best user experience.X.J.Lee 20149.73Operating Systems

Exercises9.29.199.27Operating SystemsX.J.Lee 20149.74

End of Chapter 9