Chapter 9 Virtual Memory Operating System Concepts Silberschatz
Chapter 9: Virtual Memory Operating System Concepts Silberschatz, Galvin and Gagne
Objectives n To describe the benefits of a virtual memory system n To explain the concepts of demand paging, page -replacement algorithms, and allocation of page frames n To discuss the principle of the working-set model Operating System Concepts Silberschatz, Galvin and Gagne
Background Operating System Concepts Silberschatz, Galvin and Gagne
Background In chapter of memory management , n various memory-management strategies used in computer systems were discussed and all of these strategies have the same goal: to keep many processes in memory simultaneously to allow multiprogramming. n However, they tend to require that an entire process be in memory before it can execute. Virtual memory is a technique that allows the execution of processes that are not completely in memory. Operating System Concepts Silberschatz, Galvin and Gagne
Background Advantages o that programs can be larger than physical memory. o Further, virtual memory abstracts main memory into an extremely large, uniform array of storage, separating logical memory as viewed by the user from physical memory. o This technique frees programmers from the concerns of memorystorage limitations. Operating System Concepts Virtual Memory That is Larger Than Physical Memory Silberschatz, Galvin and Gagne
Virtual-address Space n we allow the heap to grow upward in memory as it is used for dynamic memory allocation. Similarly, we allow the stack to grow downward in memory through successive function calls. n The large blank space (or hole) between the heap and the stack is part of the virtual address space Operating System Concepts Silberschatz, Galvin and Gagne
Background n Advantages (cont) o Virtual memory also allows processes to share files easily and to implement shared memory. Shared Library Using Virtual Memory Operating System Concepts 9. 7 Silberschatz, Galvin and Gagne
Background n disadvantage l Virtual memory is not easy to implement, however, and may substantially decrease performance if it is used carelessly Operating System Concepts 9. 8 Silberschatz, Galvin and Gagne
Background n Virtual memory can be implemented via: 1. Demand paging 2. Demand segmentation Operating System Concepts 9. 9 Silberschatz, Galvin and Gagne
1 -Demand Paging Consider how an executable program might be loaded from disk into memory. n One option is to load the entire program in physical memory at program execution time. However, a problem with this approach n is that we may not initially need the entire program in memory. Ex: Suppose a program starts with a list of available options from which the user is to select. Loading the entire program into memory results in loading the executable code for all options, regardless of whether an option is ultimately selected by the user or not. n An alternative strategy is to load pages only as they are needed during program excution. This technique is known as demand paging and is commonly used in virtual memory systems. Operating System Concepts 9. 10 Silberschatz, Galvin and Gagne
1 -Demand Paging n A demand-paging system is similar to a paging system with swapping n But rather than swapping the entire process into memory, we use a lazy swapper. n lazy swapper never swaps a page into memory unless that page will be needed. n Since we are now viewing a process as a sequence of pages, rather than as one large contiguous address space, use of the term swapper is technically incorrect. n A swapper manipulates entire processes, n whereas a pager is concerned with the individual pages of a process. We thus use pager, rather than swapper, in connection with demand paging. Operating System Concepts 9. 11 Silberschatz, Galvin and Gagne
1 -Demand Paging n Bring a page into memory only when it is needed l Less I/O needed l Less memory needed l Faster response l More users n Page is needed reference to it l invalid reference abort l not-in-memory bring to memory Operating System Concepts 9. 12 Silberschatz, Galvin and Gagne
Transfer of a Paged Memory to Contiguous Disk Space Operating System Concepts 9. 13 Silberschatz, Galvin and Gagne
1 -Demand Paging n With this scheme, we need some form of hardware support to distinguish between 1. the pages that are in memory 2. the pages that are on the disk. n The valid–invalid bit scheme can be used for this purpose. Ø “valid, ” : the 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 System Concepts 9. 14 Silberschatz, Galvin and Gagne
1 -Demand Paging Valid-Invalid Bit n With each page table entry a valid– invalid bit is associated (v in-memory, i not-in-memory) n Initially valid–invalid bit is set to i on all entries page table Operating System Concepts 9. 15 Silberschatz, Galvin and Gagne
Page Table When Some Pages Are Not in Main Memory Operating System Concepts 9. 16 Silberschatz, Galvin and Gagne
Page Fault n If there is a reference to a page, first reference to 1. 2. 3. 4. 5. 6. that page will trap to operating system: page fault Operating system looks at another table to decide: l Invalid reference abort l Just not in memory Get empty frame Swap page into frame Reset tables Set validation bit = v Restart the instruction that caused the page fault Operating System Concepts 9. 17 Silberschatz, Galvin and Gagne
Steps in Handling a Page Fault Operating System Concepts 9. 18 Silberschatz, Galvin and Gagne
Performance of Demand Paging n Demand paging can significantly affect the performance of a computer system. why ? • if no page faults, the effective access time is equal to the memory access time. • If, however, a page fault occurs, we must first read the relevant page from disk and then access the desired word. n Let p be the probability of a page fault (0 ≤ p ≤ 1). expect p to be close to only a few page faults. effective access time =[ (1 − p) × ma ]+ [p × page fault time]. Operating System Concepts 9. 19 Silberschatz, Galvin and Gagne
Demand Paging Example n Memory access time = 200 nanoseconds n Average page-fault service time = 8 milliseconds n EAT = (1 – p) x 200 + p (8 milliseconds) = (1 – p) x 200 + p x 8, 000 = 200 + [p x 7, 999, 800] n If one access out of 1, 000 causes a page fault, then EAT = 8. 2 microseconds. This is a slowdown by a factor of 40!! Operating System Concepts 9. 20 Silberschatz, Galvin and Gagne
Process Creation n Virtual memory allows other benefits during process creation: - Copy-on-Write - Memory-Mapped Operating System Concepts Files (later) 9. 21 Silberschatz, Galvin and Gagne
Copy-on-Write n Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory If either process modifies a shared page, only then is the page copied n COW allows more efficient process creation as only modified pages are copied n Free pages are allocated from a pool of zeroed-out pages (i. e. pages that have been erased) Operating System Concepts 9. 22 Silberschatz, Galvin and Gagne
Before Process 1 Modifies Page C After Process 1 Modifies Page C Operating System Concepts 9. 23 Silberschatz, Galvin and Gagne
What happens if there is no free frame? n Page replacement – find some page in memory, but not really in use, swap it out n Same page may be brought into memory several times Operating System Concepts 9. 24 Silberschatz, Galvin and Gagne
Page Replacement n If we increase our degree of multiprogramming, we are over- allocating memory. n Ex: If we run (6) processes, each of which is (10) pages in size but actually uses only (5) pages, we have higher CPU utilization and throughput, with ten frames to spare. l It is possible, however, that each of these processes, for a particular data set, may suddenly try to use all (10) of its pages, resulting in a need for (60) frames when only(40)are available. n Further, consider that system memory is not used only for holding program pages. Buffers for I/O also consume a considerable amount of memory. This use can increase the strain on memoryplacement algorithms. Operating System Concepts 9. 25 Silberschatz, Galvin and Gagne
Need For Page Replacement Operating System Concepts 9. 26 Silberschatz, Galvin and Gagne
Basic Page Replacement 1. 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 3. Bring the desired page into the (newly) free frame; update the page and frame tables 4. Restart the process Operating System Concepts 9. 27 Silberschatz, Galvin and Gagne
Page Replacement Operating System Concepts 9. 28 Silberschatz, Galvin and Gagne
Page Replacement algorithm n The simplest page-replacement algorithm is a first-in, first-out (FIFO) n FIFO each page has memory load start time or create FIFO queue to hold all pages in memory n When a page must be replaced, the oldest page is chosen. Operating System Concepts 9. 29 Silberschatz, Galvin and Gagne
FIFO Page Replacement Operating System Concepts 9. 30 Silberschatz, Galvin and Gagne
Optimal Algorithm n Replace page that will not be used for longest period of time n 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 4 2 3 4 5 n How do you know this? n Used for measuring how well your algorithm performs Operating System Concepts 9. 31 Silberschatz, Galvin and Gagne
Optimal Page Replacement Operating System Concepts 9. 32 Silberschatz, Galvin and Gagne
Least Recently Used (LRU) Algorithm n Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 5 2 2 2 3 5 5 4 4 3 3 3 n Counter implementation l Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter l When a page needs to be changed, look at the counters to determine which are to change Operating System Concepts 9. 33 Silberschatz, Galvin and Gagne
LRU Page Replacement Operating System Concepts 9. 34 Silberschatz, Galvin and Gagne
LRU Algorithm (Cont. ) n Stack implementation – keep a stack of page numbers in a double link form: l Page referenced: 4 move it to the top 4 requires l 6 pointers to be changed No search for replacement Operating System Concepts 9. 35 Silberschatz, Galvin and Gagne
Use Of A Stack to Record The Most Recent Page References Operating System Concepts 9. 36 Silberschatz, Galvin and Gagne
LRU Approximation Algorithms n Reference bit With each page associate a bit, initially = 0 l When page is referenced bit set to 1 l Replace the one which is 0 (if one exists) 4 We do not know the order, however l n Second chance Need reference bit l Clock replacement l If page to be replaced (in clock order) has reference bit = 1 then: 4 set reference bit 0 4 leave page in memory 4 replace next page (in clock order), subject to same rules l Operating System Concepts 9. 37 Silberschatz, Galvin and Gagne
Second-Chance (clock) Page. Replacement Algorithm Operating System Concepts 9. 38 Silberschatz, Galvin and Gagne
Counting Algorithms n Keep a counter of the number of references that have been made to each page n LFU Algorithm: replaces page with smallest count n MFU Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used Operating System Concepts 9. 39 Silberschatz, Galvin and Gagne
End of Chapter 9 Operating System Concepts 9. 40 Silberschatz, Galvin and Gagne
- Slides: 40