Virtual Memory Management 2
Virtual Memory Management 2
Ref:- Galvin
Virtual Memory
• What is Virtual Memory?
• Demand Paging
• Copy-on-Write
• Page Replacement
• Allocation of Frames
• Thrashing
• Memory-Mapped Files
• Allocating Kernel Memory
What is Virtual Memory?
• Technique that allows the execution of
processes that are not completely in memory.
• Abstracts main memory into an extremely
large, uniform array of storage.
• Allows processes to share files easily and to
implement shared memory.
• Virtual memory is a memory management
technique that computers use to keep systems
running quickly and efficiently. It's also known
as virtual storage.
i
i
page table
• During address translation, if valid–invalid bit in page table entry
is I page fault
Page Table When Some Pages Are Not in Main Memory
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. Find free frame
3. Swap page into frame via scheduled disk operation
4. Reset tables to indicate page now in memory
Set validation bit = v
5. Restart the instruction that caused the page fault
Steps in Handling a Page Fault
Page Fault
Access to a page marked invalid causes a page fault .
Procedure for handling page faults.
1.Check whether the reference is valid or invalid memory
access.
2.If reference was invalid, terminate the process. If valid,
but the page not in memory , page it in
3.Get empty frame
4.Schedule a disk operation to read the desired page into
the newly allocated frame
5.Reset tables
6.Restart the instruction that caused the page fault
Performance of Demand Paging
• Stages in Demand Paging (worse case)
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the page on the disk
5. Issue a read from the disk to a free frame:
1. Wait in a queue for this device until the read request is serviced
2. Wait for the device seek and/or latency time
3. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then resume the
interrupted instruction
Performance of Demand Paging
(Cont.)
• Three major activities
– Service the interrupt – careful coding means just several
hundred instructions needed
– Read the page – lots of time
– Restart the process – again just a small amount of time
• Page Fault Rate 0 p 1
– if p = 0 no page faults
– if p = 1, every reference is a fault
Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault overhead
+ swap page out
+ swap page in)
Copy-on-Write
• 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
• COW allows more efficient process creation as only
modified pages are copied
• When is a page going to be duplicated using copy-on-write?
– Depends on the location from where a free page is
allocated
• OS uses Zero-fill-on-demand technique to allocate these
pages.
Before Process 1 Modifies Page C
After Process 1 Modifies Page C
What Happens if There is no Free Frame?
Page faults:15
Page replacements:12
Adv and Disadv of FIFO
Adv
Easy to understand and program
Disadv
• Performance not always good
• The older pages may be initialization files
which would be required throughout
• Increases the page fault rate and slows
process execution.
What is belady’s anomaly
123412512345
Compute using 4 frames
Compare the page faults by using frame size 3
Difference is because of belady’s anomaly
FIFO Illustrating Belady’s Anomaly
Optimal Algorithm
• Result of discovery of Belady’s anomaly was
optimal page replacement algorithm
• Has the lowest page-fault rate of all
algorithms
• Algorithm does not exist. Why?
Optimal Page Replacement
• Lease Frequently Used (LFU) Algorithm: replaces page with smallest count
• Most Frequently Used (MFU) Algorithm: based on the argument that the page with
the smallest count was probably just brought in and has yet to be used
LFU
si size of process pi
S si
m total number of frames
si
ai allocation for pi m
S
Priority Allocation
• Use a proportional allocation scheme using
priorities rather than size
• Local replacement – each process selects from only its own set of
allocated frames
– More consistent per-process performance
– But possibly underutilized memory
Thrashing
Important questions
1. What is paging? Explain the structure of page table
2. What is belady’s algorithm? Explain LRU, FIFO, OPR
algos. Which algorithm suffers from Belady’s anomaly?
3. Short note on page fault handling
4. Explain virtual memory and demand paging
5. Draw and explain paging hardware with TLB
6. Explain paging in detail. Describe how logical address
converted to physical address
7. Explain how memory management takes place in Linux