OS_11__Virtual_Memory
OS_11__Virtual_Memory
OS_11__Virtual_Memory
Outline
• Background
• Demand Paging
• Copy-on-Write
• Page Replacement
• Allocation of Frames
• Thrashing
Background
• Code needs to be in memory to execute, but entire program rarely
used
• Error code, unusual routines, large data structures
• Entire program code not needed at same time
• Consider ability to execute partially-loaded program
• Program no longer constrained by limits of physical memory
• Each program takes less memory while running -> more programs run at
the same time
• Increased CPU utilization and throughput with no increase in response time or
turnaround time
• Less I/O needed to load or swap programs into memory -> each user
program runs faster
Virtual memory
• Virtual memory – separation of user logical memory from
physical memory
• Only part of the program needs to be in memory for execution
• Logical address space can therefore be much larger than physical
address space
• Allows address spaces to be shared by several processes
• Allows for more efficient process creation
• More programs running concurrently
• Less I/O needed to load or swap processes
Virtual memory (Cont.)
• Virtual address space – logical view of how process is
stored in memory
• Usually start at address 0, contiguous addresses until end of
space
• Meanwhile, physical memory organized in page frames
• MMU must map logical to physical
• Virtual memory can be implemented via:
• Demand paging
• Demand segmentation
Virtual Memory That is Larger Than Physical Memory
Virtual-address Space
Usually design logical address space for
the stack to start at Max logical address
and grow “down” while heap grows “up”
• Maximizes address space use
• Unused address space between
the two is hole
No physical memory needed
until heap or stack grows to a
given 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
Shared Library Using Virtual Memory
Demand Paging
• Could bring entire process into memory at load
time
• Or bring a page into memory only when it is
needed
• Less I/O needed, no unnecessary I/O
• Less memory needed
• Faster response
• More users
• Similar to paging system with swapping (diagram
on right)
• invalid reference abort
• Not-in-memory bring to memory
• Lazy swapper – never swaps a page into memory
unless page will be needed
• Swapper that deals with pages is a pager
Basic Concepts
• With swapping, the pager guesses which pages will be used
before swapping them out again
• How to determine that set of pages?
• Need new MMU functionality to implement demand paging
• If pages needed are already memory resident
• No difference from non demand-paging
• If page needed and not memory resident
• Need to detect and load the page into memory from storage
• Without changing program behavior
• Without programmer needing to change code
• Use page table with valid-invalid bit (see chapter 9)
Page table with 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:
Note now potentially 2 page transfers for page fault – increasing EAT
Page Replacement
Page and Frame Replacement Algorithms
• Frame-allocation algorithm determines
• How many frames to give each process
• Which frames to replace
• Page-replacement algorithm
• Want lowest page-fault rate on both first access and re-access
• Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page
faults on that string
• String is just page numbers, not full addresses
• Repeated access to the same page does not cause a page fault
• Results depend on number of frames available
• In all our examples, the reference string of referenced page
numbers is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
Graph of Page Faults Versus the Number of Frames
First-In-First-Out (FIFO) Algorithm
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)
15 page faults
• To avoid trashing:
• Calculate the size of locality
• Policy:
• if size of locality > total memory size suspend or swap out one of the
processes
• Issue: how to calculate “ size of locality”
Locality In A Memory-Reference Pattern
Working-Set Model
• working-set window a fixed number of page references
Example: 10,000 instructions
• WSSi (working set of Process Pi) = total number of pages
referenced in the most recent (varies in time)
• if too small will not encompass the entire locality
• if too large will encompass several localities
• if = will encompass entire program