Virtual Memory A program can be executed only
Virtual Memory • A program can be executed only if it has been loaded into the main memory (mm) because the CPU interacts with the mm only. • What should be done if the available mm is not big enough to accommodate the program? – A part of the program is loaded in the mm and the remaining part remains on the secondary storage. – In this way a program whose size is larger than the available mm can be executed. – This technique is called Virtual Memory Technique
Virtual Memory (contd. ) • The term virtual memory is normally associated with the ability to address a memory space much larger than that available in the RAM of a computer. The basic idea behind virtual memory is that: – the combined size of a program, data and stack may exceed the amount of RAM available for it. – The O. S. keeps the parts of the program currently in use, in the RAM and the rest on the disk; e. g. a 4 MB program can run in a 1 MB partition by carefully choosing which 1 MB to keep in the partition of the RAM at each instant with the pieces of the program being swapped between the RAM and the disk as needed. • Virtual memory also works in multiprogramming environment. E. g. eight 1 MB programs can each be allocated 256 k partition in a 2 MB RAM, with each program operating as though it has its own 256 k private machine. In this way, virtual memory and multiprogramming fit very well.
Mechanics of Virtual Memory • We shall consider a virtual memory system based on paging. • When a process starts, at least one page containing the execution start point must be in the main memory. • The process continues as long as the next referenced instruction lies in this page. • If the page containing the next referenced instruction has not been brought in the main memory this is called Page Fault. • The Page fault generates an interrupt which asks the system loader (part of O. S. ) to load the referenced page in the main memory. This technique is called Demand Paging. • On demand, the system loader loads the desired page in the mm. and execution starts. • In response to page faults a subset of the pages of a process accumulates in the mm. This is called Resident Subset. • When a process terminates the O. S. releases all the page frames so that they are available to the other processes.
Page Replacement • In multiprogramming environment many processes compete for the main memory. Consequently, the real memory is full of pages belonging to the active processes. • Next slide illustrates simplified example of three processes A, B, C, each with a number of pages resident in the real memory. Here, all the available page frames have been utilized. • It must be noticed that total virtual address space is much larger than the total real address space, therefore, all the pages of virtual address space cannot be accommodated in the real address space simultaneously. • If a page fault occurs and no page frame is vacant in the real memory, the O. S. removes a page from the real memory and loads the desired page in its place. This technique is called Page replacement. • We shall consider it in more detail.
Page Replacement Main Memory B 4 A 4 B 0 A 0 Process A B 2 A 0 C 0 A 1 Process B A 2 B 0 A 3 B 1 Process C A 4 B 2 C 0 A 5 B 3 A 6 B 4 A 7 B 5 A 1 C 2 C 3 B 3 A 2 C 1 A 3 A 5 B 1 A 4
Relocation / Address Mapping CPU p d f d RAM Page Table Register Virtual address v = (p, d) P M Frame # . . r . . f. . . Address Mapping in V. M. r = f * page-size + d
P and M Bits Present or P bit • indicates whether the corresponding virtual page is in real memory or not. • If a page is referenced / accessed when its P bit is zero the page fault occurs. Modified or M bit • indicates whether or not the concerned page has been modified during its current stay in the real memory. A modified page is called dirty page and is more likely to contain data rather than instructions. These pages have implications for the page replacement strategies.
Page Replacement Strategies/Algorithms • If a page fault occurs and no page frame is vacant in the real memory, the O. S. removes a page from the real memory to load the desired page. • If the page to be removed has been modified, it must be rewritten to the disk to make the disk copy up to date. This happens only in the case of data pages. A modified page is called dirty page. • If the page has not been modified ( e. g. page contains instructions), no rewrite to the disk is required; the desired page just overwrites the page to be evicted. The page to be replaced can be selected at random but performance can be improved if sensible decisions are made.
Page Replacement Strategies/Algorithms 1. Random Page Replacement Strategy: The pages to be replaced are selected at random. Disadvantage: o It could select a page which was to be referenced next (the worst page to replace). This is not a good scheme and is rarely used. 2. First-in First-out (FIFO) Page Replacement Strategy: o A page with longest stay in mm is selected for removal because this page had a chance and it is time to give chance to another page. For this purpose, each page is time stamped on entering the mm. Disadvantage: o It may replace a heavily used page because a page has been in the mm due to its constant use; e. g. in time sharing systems many users share a copy of the text editor as they edit files. FIFO may select a heavily used editor page for replacement.
Page Replacement Strategies/Algorithms 3. (contd. ) Least Recently Used (LRU) Page Replacement Strategy: o That page is selected for replacement whose time since the last reference is the greatest. o LRU requires each page to be time stamped whenever it is referenced. This is a considerable overhead. o Alternative is to put the currently referenced page at the head of a queue but this is also time consuming. Disadvantage: o The LRU page may be next to be used as a program cycles around a large loop using several pages.
Page Replacement Strategies/Algorithms (contd. ) 4. Not Used Recently (NUR) Page Replacement Strategy: o This scheme approximates LRU with a little overhead. o Motivation behind this scheme is that the pages not used recently are not likely to be used in near future. o This scheme is implemented with two hardware bits P & M. P (present bit) =1, if a page has been brought in the response to a page fault = 0, otherwise. M (Modified bit) =1, if the page has been modified during its present stay in mm. = 0, otherwise This strategy works as follows: o Initially, all P and M bits are set to 0. As a page is referenced and brought to mm its P bit is set to 1. As a page is modified its M bit is also set to 1. o After a while most of the P bits would be set to 1 and we lose our ability to find a most appropriate page to replace. One solution to this problem is to periodically set all the P bits to zero to get a fresh start. Then P bits are again set to 1 as the pages are referenced. (This makes active pages vulnerable to replacement but for a short while after the bits are reset).
NUR (cont) Class P M Description 0 0 0 Not present not modified 1 0 1 Not present but modified 1 2 1 0 Present but not modified 3 1 1 Present and modified o The pages from the lower class are replaced at first (at random). From this algorithm, it is clear that it is better to remove a modified page that has not been referenced in at least one clock tick (20 msec) than an un-modified page that is being used very heavily. o Benefits: § Easy to understand § Efficient to implement § Gives adequate (if not optimal) performance. Note: At first glance class 1 seems to be impossible. This situation occurs when P bit of a class 3 clock interrupt. page has been cleared by the
- Slides: 12