Presentation on theme: "Virtual Memory Operating System Concepts chapter 9 CS 355"— Presentation transcript:
1 Virtual Memory Operating System Concepts chapter 9 CS 355 Operating SystemsDr. Matthew Wright
2 BackgroundOften, only part of a running program actually needs to be in memory.The ability to execute a program only partially in memory would provide many benefits:A program would not be constrained by the size of the physical memory.More programs could run at the same time.Programs could be loaded into memory more quickly.Virtual memory: separation of logical memory as perceived by users from physical memoryVirtual address space: the logical (or virtual) view of how a program is stored in memory
4 Demand PagingDemand paging involves only loading pages as they are needed.When a process requests the contents of a logical memory address:If the address is on a page in memory (marked as valid in the page table), the contents are retrieved.Otherwise, the address is on a page not in memory (marked as invalid in the page table), and a page fault occurs.If the address is in the process’s memory space, then summon the pager to bring the page into memory.Otherwise, the address is not in the process’s memory space, then raise an error condition.Thus, demand paging uses a lazy pager, that only brings pages into memory when required.
6 Page Fault Handling a page fault requires several steps: Determine whether or not the process is allowed to access the requested memory location. If so, continue to step 2. Otherwise, terminate the process.Find a free frame (e.g. take one from the free-frame list).Schedule a disk operation to read the desired page into the newly allocated frame.Modify the page table to indicate that the page is in memory.Restart the process at the instruction that requested the memory location.Complications:Can an instruction always be restarted?What if a single instruction modifies many different memory locations?
8 Performance of Demand Paging Memory access time: 10 to 200 nanoseconds, denoted mPage fault time: 5 to 10 milliseconds, denoted fLet p be the probability of a page fault.The effective access time is:effective access time = (1 – p) × m + p × fExample: suppose m = 150 ns, f = 8,000,000 ns, p = 0.001effective access time = (0.999) × × 8,000,000= 8150 nsDemand paging has slowed the system by a factor of 54For good performance, we need a page fault rate of much less than 1 page fault in 1000 memory accesses.Of course, demand paging also saves us from loading into memory pages that are never needed, which improves performance.
9 Copy on WriteRecall: the fork() system call creates a child process that is a duplicate of its parentSince the child might not modify its parents pages, we can employ the copy-on-write technique:The child initially shares all pages with the parent.If either process modifies a page, then a copy of that page is created.
10 Page ReplacementIf a process requests a new page and there are no free frames, the operating system must decide which page to replace.The OS must use a page-replacement algorithm to select a victim frame.The OS must then write the victim frame to disk, read the desired page into the frame, and update the page tables.This requires double the disk access time.To reduce page-replacement overhead, we can use a modify bit (or dirty bit) to indicate whether each page has been modified.If a page has not been modified, there is no need to write it back to disk when it is being replaced (it is already on disk).
11 FIFO Page Replacement Always replace the oldest page Easy to program and implementPoor performance, since pages might be in constant useExample: consider reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 53 Frames4 Frames1234512345112123234431412125512512523534532112123123413421243234553415142521321344325indicates page fault9 page faults for 3 frames10 page faults for 4 frames???
12 Belady’s Anomaly Expected Graph of Frames and Page Faults With FIFO page replacement, the number of page faults can increase with more frames of memory!Expected Graph of Frames and Page FaultsGraph for Reference String 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
13 Optimal Page Replacement Replace the page that will not be used for the longest period of timeGuarantees the lowest possible page-fault rateDifficult to implement, since it requires knowledge of the futureExample:12354112312312312513315354354345345314341341411422412412419 page faults for this reference string
14 15 page faults for this reference string LRU Page ReplacementLRU: Least Recently UsedReplace the page that has not been used for the longest period of timeNot so easy to implement: must associate a time stamp to each page or maintain a stackRequires hardware assistance, since extra data must be maintained with every memory referenceExample:1235411231231231252315215414313453451451341343424242141215 page faults for this reference string
15 LRU-Approximation Algorithms Many systems don’t provide hardware support for true LRUSome systems provide a reference bit for each page, which helps determine if the page was recently usedExample: suppose the OS periodically sets all reference bits to 0, and when a page is accessed, its reference bit is set to 1Second-Chance Algorithm:Use a FIFO replacement algorithm to select a pageIf the page’s reference bit is 1, give the page a second chance by setting its reference bit to 0 and selecting the next pageAdditional-Reference-Bits AlgorithmAt regular intervals, record and then reset the reference bitsThis establishes a rough ordering of how recently pages were used
16 Allocation of FramesHow do we allocate the fixed amount of free memory among various processes?Each process needs some minimum number of available frames, depending on how many levels of indirect addressing are allowed.Example: If a load instruction on page 0 refers to an address on page 1, which is an indirect reference to page 2, then the process must have at least 3 frames.Equal allocation: allocate the same number of frames per processA small process could waste frames while a big process might not have enoughProportional allocation: allocate frames according to some criteriaAllocate more frames to a big process?Allocate more frames to a high-priority process?
17 Allocation of FramesIs the set of frames allocated to a process fixed?Global replacement: a process can select a replacement frame currently allocated to another processLocal replacement: a process can only select replacement frames from its own set of allocated framesIs all memory accessed at the same speed?In some multiprocessor systems, a given CPU can access some sections of memory faster than other sections.Such systems are non-uniform memory access (NUMA) systemsNUMA systems introduce further complications for scheduling and paging
18 ThrashingA process that spends more time paging than executing is thrashing.Thrashing occurs when a process doesn’t have enough frames:The process has more frequently-used pages than frames.The process experiences frequent page faults.With each page fault, the process must replace some frequently-used page.
19 Preventing Thrashing Locality model A process generally requires a set of pages that are used together, called a locality.As a process runs, it moves from locality to locality.Working-set modelUse a parameter, ∆, to approximate localityWorking set: the pages in the most recent ∆ referencesThe OS monitors the working set for each process, ensuring that each process has enough frames for its working set.This requires an appropriate choice of ∆.Using the working-set model to prevent thrashing requires extra overhead from the OS.
20 Preventing Thrashing Page-fault frequency A simple strategy to prevent thrashing is to monitor the page-fault frequency.If page faults are too frequent, then the process needs more frames.If a process has very few page faults, then has too many frames.
21 Memory-Mapped FilesA memory-mapped file allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory.Mechanism:A file is initially read using demand paging.A page-sized portion of the file is read from the file system into a physical page.Subsequent reads/writes to/from the file are treated as ordinary memory accesses.If the file is modified, modified pages are eventually (or periodically) copied back to the disk.This simplifies file access by treating file I/O through memory rather than read() and write() system calls.It also allows several processes to map the same file, allowing the pages in memory to be shared.
23 Allocating Kernel Memory Memory for kernel is often allocated from a different pool than that used for user processes.Kernel requests memory for data structures of varying sizes, which may be much smaller than a page and which might not be pagedCertain hardware devices interact directly with physical memory, requiring contiguous blocks of memory (rather than pages)Kernel memory could be allocated by a power-of-2 allocator, which rounds memory requests up to the next power of 2.Kernel memory could also be allocated by a slab allocator, which creates a cache in memory for each different type of kernel data structure.
24 Other Considerations for Paging Prepaging:Attempts to reduce the large number of page faults that occur when a process starts upBrings some pages into memory before they are neededCould be wasteful if pages aren’t actually neededPage size:The particular choice of page size affects system performanceConsiderations: fragmentation, table size, I/O overhead, localityTLB reach: the amount of memory accessible from the TLBTLB reach is TLB size times page sizeIdeally, the TLB would store the working set for a process
25 Other Considerations for Paging Program structure:A program can be written and compiled in a way that increases locality and reduces page faultsI/O interlock:Suppose we instruct an I/O device to write to a certain page in memory. We don’t want that page to be replaced before the I/O operation is completed.Some systems allow pages to be locked in memory.Pages waiting for I/O can be locked to particular frames until the I/O operation completes.
Your consent to our cookies if you continue to use this website.