VIRTUAL MEMORY MANAGEMENT
description
Transcript of VIRTUAL MEMORY MANAGEMENT
![Page 1: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/1.jpg)
VIRTUAL MEMORY MANAGEMENT
![Page 2: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/2.jpg)
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 3: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/3.jpg)
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
![Page 4: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/4.jpg)
Steps handling page fault1. Check the page table to determine whether the reference is valid or invalid memory
access2. If the reference was invalid, terminate the process. If it was valid, but have not yet
brought in that page, page in the latter.3. Find a free frame(from the free frame list)4. schedule a disk operation to read the desired page into the newly allocated frame5. 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.6. 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 5: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/5.jpg)
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 6: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/6.jpg)
Page I/O
Page outoperation
Page inoperation
Swap space of the program
PkPage beingWritten out
PiPage beingloadedin
•A program which encounters a page fault becomes blocked tillThe required page is loaded in memory. Hence the execution Performance of the program suffers
![Page 7: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/7.jpg)
Transfer of a Paged Memory to Contiguous Disk Space
![Page 8: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/8.jpg)
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:
During address translation, if valid–invalid bit in page table entry is I page fault
vvv
v
i
ii
….
Frame # valid-invalid bit
page table
![Page 9: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/9.jpg)
Page Table When Some Pages Are Not in Main Memory
![Page 10: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/10.jpg)
Page Fault
• If there is a reference to a page, first reference to that page will trap to operating system:
page fault1. Operating system looks at another table to decide:
– Invalid reference abort– Just not in memory
2. Get empty frame3. Swap page into frame4. Reset tables5. Set validation bit = v6. Restart the instruction that caused the page fault
![Page 11: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/11.jpg)
Steps in Handling a Page Fault
![Page 12: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/12.jpg)
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
![Page 13: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/13.jpg)
Need For Page Replacement
![Page 14: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/14.jpg)
Basic 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
3. Bring the desired page into the (newly) free frame; update the page and frame tables
4. Restart the process
![Page 15: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/15.jpg)
Page Replacement
![Page 16: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/16.jpg)
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,0102,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,1,6,1,6
![Page 17: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/17.jpg)
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
![Page 18: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/18.jpg)
Graph of Page Faults Versus The Number of Frames
![Page 19: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/19.jpg)
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
• Belady’s Anomaly: more frames more page faults
1
2
3
1
2
3
4
1
2
5
3
4
9 page faults
1
2
3
1
2
3
5
1
2
4
5 10 page faults
44 3
![Page 20: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/20.jpg)
FIFO Page Replacement
![Page 21: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/21.jpg)
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
• How do you know this?• Used for measuring how well your algorithm performs
1
2
3
4
6 page faults
4 5
![Page 22: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/22.jpg)
Optimal Page Replacement
![Page 23: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/23.jpg)
Least Recently Used (LRU) Algorithm• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 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
5
2
4
3
1
2
3
4
1
2
5
4
1
2
5
3
1
2
4
3
![Page 24: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/24.jpg)
LRU Page Replacement
![Page 25: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/25.jpg)
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
![Page 26: VIRTUAL MEMORY MANAGEMENT](https://reader035.fdocuments.us/reader035/viewer/2022062408/568138b2550346895da0721b/html5/thumbnails/26.jpg)
Use Of A Stack to Record The Most Recent Page References