Title: Chapter 8: Virtual Memory
1Chapter 8 Virtual Memory
- CS 472 Operating Systems
- Indiana University Purdue University Fort Wayne
2Virtual memory
- Load on demand paging and/or segmentation
- Allows more processes to be active at a time,
since only part of each process is in memory at a
time - One process may be larger than all of main memory
- The resident set is the set of pages or segments
currently loaded in memory - Virtual memory is possible because of the
principle of locality
3Principle of locality
4Principle of locality
- References to program instructions and data
within a process tend to cluster - Only a few pages of a process are needed over a
short period of time - It is possible to make intelligent guesses about
which pages will be needed in the future - This suggests that virtual memory may work
efficiently
5Virtual memory with paging
- A valid bit must be added to each PTE to indicate
if the page has been loaded into a page frame - A page fault is an internal interrupt caused when
there is a reference to a program page that has
not been loaded into a page frame - A page fault causes the OS to load the needed
page into an available page frame
6Thrashing
- Thrashing is when there is excessive page fault
activity - . . . particularly when pages are frequently
replaced just before they are needed - The processor spends most of its time swapping
pages rather than executing user instructions
7Page fault activity
- The page fault handler first moves the running
process to the blocked state - It then identifies a page frame to hold the
needed page - Typically, a loaded page needs to be overwritten
- The fault handler then directs the DMA to load
the needed page from disk into the available
frame - When the DMA finishes, the interrupt handler
moves the process that caused the fault from the
blocked state to the ready queue
8Hardware support for virtual memory
- Additional hardware support is needed in addition
to that for simple paging - The valid bit in each PTE mentioned earlier
- A modify bit in each PTE to indicate if the page
has been altered since it was last loaded into
main memory - If no change has been made, the page does not
need to be written back to disk before it is
overwritten by a new page
9VAX system PTE
31 30 27 26 25
21 20
0
V m
frame number
Holds 26-bit disk address if page not loaded
protection (each page)
- Valid bit (v) If the page is not loaded,
generate a page fault and get disk address from
bits 0-25 - Modify bit (m) Set the bit with the first write
to the page in memory - Avoids write-back of clean page to disk
10VAX system PTE
- Virtual memory size 232 bytes 4 GB
- Page size frame size 512 bytes (29 bytes)
- Real memory with up to 221 frames is possible
- 221 frames x 29 bytes/frame 230 bytes 1 GB
user processes
0 230
231
232
user space stack space system
space unused
shared
11- Note that each logical memory reference now
requires two physical references.
12Page tables real or virtual?
- The entire page table may take up too much main
memory - VAX example for each process
- Potentially, 221 PTEs (4 bytes each) 8 MB
- Therefore, page tables are also stored in virtual
memory - When a process is running, only part of its page
table is in main memory
13Page tables real or virtual?
- The system root page table for the process page
tables must be locked in real memory - The root page table may never swapped out
- Address translation now becomes a 2-step lookup
- Get the PTE for the process page table
- Get the PTE for the reference page
- Get the instruction or data
- Note that each logical memory reference now
requires three physical references - A hardware solution is needed to keep things from
running to slow
14Translation Lookaside Buffer (TLB)
- A high-speed cache for page table entries
- Contains PTEs that have been most recently used
- Hardware algorithm
- Given a virtual address, processor examines the
TLB - If PTE is present (TLB hit), retrieve frame
number and form real address - If PTE is not found (TLB miss), do the 2-step
lookup and update the TLB with the missing PTE
for next time - Note on step 1 of the 2-step lookup
- This uses the TLB again for the process page
table lookup - This might result in an additional page fault on
the process page table page (independent of using
a TLB). If so, block waiting for the process
page table page and then resume
15Page s PTEs
16Inverted page table
- Found on PowerPC, UltraSPARC, and IA-64
architecture - Page number portion of a virtual address is
hashed - Hash value is an index into the inverted page
table - There is one PTE per available frame, in contrast
to one for each virtual page (many fewer PTEs) - There is only one table for all processes
- Inverted page table is of fixed size
- If hash succeeds, use frame , else generate page
fault
17(No Transcript)
18Other issues
- Cache memory
- PTE may be in TLB cache, real memory, or disk
- Instructions and data may be in real memory
cache, real memory, or disk - Page size
- Large
- more internal fragmentation on the last page
- resident set less able to adapt to principle of
locality - Small
- larger page tables
- more page faults
19(No Transcript)
20Example page sizes
21Segmented virtual memory
- Segments may be of unequal, dynamic size
- Simplifies handling of growing data structures
- Allows a segment to be altered, recompiled, and
relinked without linking and loading all segments - Lends itself to sharing data among processes
- Lends itself to protection
22Segmented virtual memory tables
- Segment entry for each segment
- Each entry contains the length of the segment
along with the base address - A valid bit is needed to determine if segment
is already in main memory - A modify bit is needed to determine if the
segment has been modified since it was loaded
23(No Transcript)
24Combined paging/segmentation
- Paging is transparent to the programmer
- Segmentation is visible to the programmer
- Each segment is broken into fixed-size pages
25(No Transcript)
26OS policies for virtual memory
27Fetch policy
- Determines when a page should be brought into
memory - Demand paging brings a page into main memory when
a reference is made to a location on the page - This results in many page faults when process
first started - Prepaging anticipates pages will be needed
- It is efficient to bring in pages that reside
contiguously on the disk - Pointless if the pages are never referenced
- Prepaging is usually not warranted
28Placement and replacement policy
- A placement policy determines where in real
memory a process piece is to reside - This is important in a segmentation system
- A replacement policy determines which page is
replaced - The page removed should be the page least likely
to be referenced in the near future - Most policies predict the future behavior on the
basis of past behavior
29Replacement policy
- Consider only the set of pages available to be
swapped out - Ignore locked frames
- Locked frames contain . . .
- Kernel of the operating system
- Control structures
- System root page tables, for example
- I/O buffers
- Associate a lock bit with each frame
30Replacement policy algorithms
- Optimal replacement policy
- Selects for replacement that page for which the
time to the next reference is the longest (if
ever) - Not practical
- Impossible to have perfect knowledge of future
events - So, use the principle of locality to improve the
chances that the replaced page is unlikely to be
referenced soon
31Replacement policy algorithms
- Least Recently Used (LRU)
- Replaces the page that has not been referenced
for the longest time - By the principle of locality, this should be the
page least likely to be referenced in the near
future - Inefficient to implement
- Each frame would need to be tagged with the time
of last reference (much overhead)
32Replacement policy algorithms
- First-in, first-out (FIFO)
- Logically treat page frames allocated to a
process as arranged in a circular buffer - Pages are replaced in round-robin style
- Simplest replacement policy to implement
- Page that has been in memory the longest is
replaced - But, these pages may be needed again very soon
- Imagine a grocery store eliminating bread and
milk just because they had been stocked a long
time
33Replacement policy algorithms
- Clock
- Requires hardware use bit for each frame
- When a page is first loaded, the use bit is set
to 1 - When the page is referenced, the use bit is set
to 1 - When it is time to replace a page, the OS does a
round-robin scan, choosing the first frame
encountered which still has a clear use bit - During the search for replacement, the OS clears
each use bit it encounters
34(No Transcript)
35Replacement policy algorithms
- Clock (continued)
- The frame has to earn the right to stay the next
time around - This is the most effective practical method
- It can be enhanced by using the modify bit in
the PTE - Unmodified pages are preferred for replacement
36(No Transcript)
37Comparison of Placement Algorithms
38Page buffering
- A replaced page is added to a pool of free frames
which have been replaced but not yet
overwritten - The pool is separated into two lists
- Free page list of unmodified (clean) free pages
- Modified page list of pages that need to be
written back to disk - Modified pages can be cleaned in batches and
moved to the free page list - This decouples cleaning from replacement
- Some page faults can be handled by simple
bookkeeping - Page buffering is best used in conjunction with
the FIFO page replacement policy - Used by the VAX/VMS operating system
39Resident set size
- Fixed-allocation for each process
- Gives a process a fixed number of page frames
within which to execute - When a page fault occurs, a frame is reclaimed
from the current process - Difficult to allocate the optimal number of frames
40Resident set size
- Variable-allocation for each process
- Size of the resident set of a process varies and
adapts over the lifetime of the process - Involves OS overhead
- Global scope a page fault gives the process a
new frame taken from any process - Local scope frames are reclaimed from the
current process, but the allocation size is
adjusted periodically based on the fault rate
41Cleaning policy
- Demand cleaning
- A page is written out only when it has been
selected for replacement - Precleaning
- Pages are written out in batches (cleaned)
- It works best to combine precleaning with page
buffering
42Working set
- Definition The working set W(t,?) of a process is
the set of process pages at virtual time t that
have been referenced in the previous ? virtual
time units - The page fault rate is low if
- resident set gt W(t,?)
- Goal Allow as many processes as possible
consistent with a low fault rate
43(No Transcript)
44Load control (LS Criterion)
- Adjust the number of processes so that
- mean time between faults mean time needed to
process a fault - Determines the optimal multiprogramming level
- the number of processes resident in main memory
- Processor use is maximized at this point
- If too few processes, all processes may be
blocked from time to time - If too many processes, thrashing will occur
- To reduce the number or processes, some could be
suspended
45Multiprogramming level
46Process Suspension choose the . . .
- Lowest priority process
- Faulting process - does not have much of its
working set in main memory so it will be blocked
anyway - Last process activated - unlikely to have its
working set resident - Process with smallest resident set - requires the
least future effort to reload - Largest process yields the most free frames