Module 5.2 VirtualMemory
Module 5.2 VirtualMemory
2
Virtual M e m o r y
Outline
• Virtual memory
• Page faults
• Page replacement
• Frame allocation
Virtual m e m o r y
• Virtual addressing allows us to introduce the idea of virtual memory
• Already have valid or invalid page translations; introduce “non-
resident” designation and put such pages on a non-volatile backing
store
• Processes access non-resident memory just as if it were “the real
thing”
• Separates program logical memory from physical memory, allowing
logical address space to be much larger than physical address space
• Implemented via demand paging and demand segmentation
Virtual m e m o r y b e n e f i t s
• Portability
• Programs work regardless of how much physical
memory, can be larger than physical memory,
and can start executing before fully loaded
• Convenience
• Less of the program needs to be in memory at
once, thus potentially more efficient
multi-programming, less I/O loading/swapping program
into memory, large sparse data-structures easily supported
• Efficiency
• No need to waste (real) memory on code or data which isn’t
used (e.g., error handling or infrequently called routines)
Virtual a d d r e s s s p a c e
• Virtual address space gives the logical view of how process is stored in memory
• Usually start at address 0, contiguous addresses until end of space
• Physical memory organized in page frames
• MMU must map logical to physical
• Usually stack starts at maximum logical address and grows “down” while heap grows “up”
• Maximizes address space use
• Unused address space between stack and heap is the hole
• No physical memory needed until heap or stack
grows to a new page
• Enables sparse address spaces with holes left for growth,
dynamically linked libraries, etc
• System libraries shared via mapping into virtual
address space
• Shared memory by mapping pages read-write into virtual
address space
• Pages can be shared during fork(), speeding process creation
Pa g e faults
• When an invalid page is referenced, it
causes a trap to the OS – a page fault
• E.g., when referenced for the first time
• OS handles the trap by examining
another table
• If invalid memory reference, then abort
• If valid but not resident, find a free
frame and swap the page in
• Entry is now marked valid as page is in memory
• After handing the fault, restart the instruction that
caused the fault
Instruction restart
• E.g., fetch and add two numbers from memory, and store the result back
• Fetch and decode instruction (add), then fetch operands A and B, perform the addition, and store
result to C
• If store to C faults, need to handle the fault and then restart from the beginning (fetch and
decode instruction, etc)
• Locality of reference helps: unlikely to have multiple faults per instruction
• More complex: an instruction that could access several different locations
• E.g., move a block of memory where source and destination can overlap, and either source or
destination (or both) straddle a page boundary
• As the instruction executes, the source might be modified – so it can’t be restarted from scratch
• Handle by, e.g., microcode for instruction strides across block, touching every page to ensure valid
so no fault can occur
• Double fault: if the page fault handler itself triggers a fault – just give up…
Locality of reference
• In a short time interval, the locations referenced by a process tend to
group into a few regions of its address space
• E.g.,
• Procedure being executed
• Sub-procedures
• Data access
• Stack variables
Demand paging
• Could bring entire process into memory at load time,
or bring pages into memory as needed
• Reduces I/O and memory needed and response time
• Supports more running processes
• Pure demand paging starts with every page marked
invalid
• Hardware support required
• Page table with valid / invalid bit
• Secondary memory (swap device with swap space)
• Ability to restart instructions
• Lazy swapper (or pager) never swaps a page into
memory unless page will be needed
• But what to swap in and out?
D e m a n d p a g i n g p e r f o r m a n c e – worst
case
1. Trap to the OS 6. Reallocate CPU to another program
2. Save the user registers and process state 7. Receive an interrupt when disk I/O
completes
3. Determine that the interrupt was a page
fault 8. Save the registers and process state for the
other program
4. Check the page reference was legal and
9. Determine that the interrupt was from the
find the page on disk disk
5. Issue a read from the disk into a free 10. Correct page table and other tables to show
frame page is now in memory
1. Wait in a queue for this device until the 11. Wait for the CPU to be allocated to this
read request is serviced
process again
2. Wait for the device seek and/or latency
time 12. Restore the user registers, process state,
3. Begin the transfer of the page to a free and new page table, and then resume the
frame interrupted instruction
Demand p a g i n g performance
• Assume memory access time is 200ns, average page-fault service time 8ms, and
page fault rate
• 0 ≤ p ≤ 1: if p = 0, no page faults; if p = 1, every reference causes a fault
• Effective Access Time (EAT)
= (1-p) × 200ns + p × 8ms
= (1-p) × 200 + p × 8,000,000 = 200 + 7,999,800 p
• If one access in 1,000 causes a page fault, EAT = 8.2μsecs — a 40× slowdown!
• For performance degradation below 10% require
220 ≥ EAT = 200 + 7,999,800 p
• Solving for p gives p < 0.0000025
i.e., less than one page fault per 400,000 accesses
D e m a n d p a g i n g o p t i m i sat io ns
• Swap space I/O can be faster than file system I/O even on the same device
• Allocate swap in larger chunks requiring less management than file system
• Copy entire process image to swap space at process load time and then page in/out of swap space
• Demand page program from binary on disk – discard when freeing unmodified frame
• Copy-on-Write (COW)
• Both parent and child processes initially share the same pages in memory
• Only when a process actually modifies a shared page is the page copied
• COW allows more efficient process creation as only modified pages are copied
• Allocate free pages from a pool of zero-fill-on-demand pages
• Pool should always have free frames for fast demand page execution
• Don’t want to have to free a frame as well as other processing on page fault
• vfork variation of fork has child created as copy-on-write address space of parent
• Very efficient when the child just calls exec
Pa g e replacement
• Paging in from disk requires a free frame — but physical memory is limited
• Either discard unused pages if total demand for pages exceeds physical memory size
• Or swap out an entire process to free some frames
• Page fault handler must
1. Locate the desired replacement page on disk
2. Select a free frame for the incoming page:
1. If there is a free frame use it, else select a victim
page to free
2. Write the victim page back to disk
3. Mark it as invalid in its process’ page tables
3. Read desired page into the now free frame
4. Restart the faulting process
• No free frames ~ doubles page fault service time
P a g e replacement algorithms
• Want the lowest page fault on both first and subsequent accesses
• Evaluate using a sequence of page numbers, noting repeated access to same
page does not trigger a fault
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• Assume three frames available