VIRTUAL MEMORY MANAGEMENT DEMAND PAGING Pages loaded on

  • Slides: 26
Download presentation
VIRTUAL MEMORY MANAGEMENT

VIRTUAL MEMORY MANAGEMENT

DEMAND PAGING • • • Pages loaded on demand is known as demand paging

DEMAND PAGING • • • Pages loaded on demand is known as demand paging – Never bring a page into memory until it is required VM handler loads only one page of a program to start with. Three concepts are important in understanding operation of demand paging – Page fault – Page in and page out – Page replacement

Page fault • When a program tries to access locations that are not in

Page fault • When a program tries to access locations that are not in memory, the hardware traps to operating system (page fault). • The operating system reads desired page into memory and restarts the process as though the page had always been in memory

Steps handling page fault 1. 2. 3. 4. 5. 6. Check the page table

Steps handling page fault 1. 2. 3. 4. 5. 6. Check the page table to determine whether the reference is valid or invalid memory access If the reference was invalid, terminate the process. If it was valid, but have not yet brought in that page, page in the latter. Find a free frame(from the free frame list) schedule a disk operation to read the desired page into the newly allocated frame When the disk read is complete, modify the internal table kept with the process and the page table to indicate that the page is now in memory. Restart the instruction that was interrupted by the illegal address trap. The process can now access the page as though it had always been in memory

Page In & Page Out • • • While initiating execution of a program,

Page In & Page Out • • • While initiating execution of a program, an area is allocated on the paging device for its legal address space and its code and data are copied into the space. This space is known as swap space of a program. When a page fault occurs during reference to a page say Pi, the VM handler finds a free page frame in memory and loads Pi in it. This is known as Page In operation for Pi. If no free frame exist in memory some page Pk existing in memory is written out into the swap space to free its page frame. This is a Page Out operation for Pk. Page I/O. The term Page Traffic is used to describe movements of pages in and out of memory.

Page I/O Swap space of the program Pk Page being Written out Pi Page

Page I/O Swap space of the program Pk Page being Written out Pi Page being loadedin Page out operation Page in operation • A program which encounters a page fault becomes blocked till The required page is loaded in memory. Hence the execution Performance of the program suffers

Transfer of a Paged Memory to Contiguous Disk Space

Transfer of a Paged Memory to Contiguous Disk Space

Valid-Invalid Bit • • • With each page table entry a valid–invalid bit is

Valid-Invalid Bit • • • With each page table entry a valid–invalid bit is associated (v in-memory, i not-in-memory) Initially valid–invalid bit is set to i on all entries Example of a page table snapshot: Frame # valid-invalid bit v v During address translation, if valid–invalid bitiin page table entry is I page fault …. i i page table

Page Table When Some Pages Are Not in Main Memory

Page Table When Some Pages Are Not in Main Memory

Page Fault • If there is a reference to a page, first reference to

Page Fault • If there is a reference to a page, first reference to that page will trap to operating system: page fault 1. Operating system looks at another table to decide: – Invalid reference abort – Just not in memory 2. 3. 4. 5. 6. Get empty frame Swap page into frame Reset tables Set validation bit = v Restart the instruction that caused the page fault

Steps in Handling a Page Fault

Steps in Handling a Page Fault

Page Replacement • Prevent over-allocation of memory by modifying page-fault service routine to include

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 transfers – only modified pages are written to disk • Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory

Need For Page Replacement

Need For Page Replacement

Basic Page Replacement 1. Find the location of the desired page on disk 2.

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 victim frame algorithm to select a 3. Bring the desired page into the (newly) free frame; update the page and frame tables 4. Restart the process

Page Replacement

Page Replacement

Page Replacement Algorithms • Want lowest page-fault rate • Evaluate algorithm by running it

Page Replacement Algorithms • Want lowest page-fault rate • 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 0100, 0432, 0101, 0612, 0103, 0104, 0101, 0611, 0102, 0103, 0104, 0101, 0610, 0103, 0104, 0101, 0609 100 bytes per page is reduced to following reference string 1, 4, 1, 6, 1, 6

Belady’s anomaly • It is found that under FIFO page replacement, certain page reference

Belady’s anomaly • It is found that under FIFO page replacement, certain page reference pattern cause more page faults when the number of page frames allocated to a process is increased • By Belady, Shedler and Nelson • This phenomenon is known as FIFO anomaly or Belady’s anomaly • Try doing the following eg: • Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Page fault for 4 frames and 3 frames

Graph of Page Faults Versus The Number of Frames

Graph of Page Faults Versus The Number of Frames

First-In-First-Out (FIFO) Algorithm • • Reference string: 1, 2, 3, 4, 1, 2, 5,

First-In-First-Out (FIFO) Algorithm • • Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process) 4 frames 1 1 4 5 2 2 1 3 3 3 2 4 1 1 5 4 2 2 1 5 3 3 2 4 4 3 9 page faults 10 page faults Belady’s Anomaly: more frames more page faults

FIFO Page Replacement

FIFO Page Replacement

Optimal Algorithm • The principle of optimality states that to obtain optimum performance, replace

Optimal Algorithm • The principle of optimality states that to obtain optimum performance, replace the page that will not be used for a longest period of time • 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 4 2 6 page faults 3 4 • • 5 How do you know this? Used for measuring how well your algorithm performs

Optimal Page Replacement

Optimal Page Replacement

Least Recently Used (LRU) Algorithm • Reference string: 1, 2, 3, 4, 1, 2,

Least Recently Used (LRU) Algorithm • 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 • Counter implementation – Every 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

LRU Page Replacement

LRU Page Replacement

LRU Algorithm- using stack • Stack implementation – keep a stack of page numbers

LRU Algorithm- using stack • Stack implementation – keep a stack of page numbers in a double link form: – Page referenced: • move it to the top • requires 6 pointers to be changed – No search for replacement

Use Of A Stack to Record The Most Recent Page References

Use Of A Stack to Record The Most Recent Page References