Slide Virtual Mem
Slide Virtual Mem
Slide Virtual Mem
Background
Demand Paging
Copy-on-Write
Page Replacement
Allocation of Frames
Thrashing
Memory-Mapped Files
Allocating Kernel Memory
Other Considerations
Operating-System Examples
Observation
Code needs to be in memory to be executed, but entire program rarely
used. E.g., Error code, unusual routines, large data structures
Logical address space can be much larger than physical address space
Demand segmentation
Meanwhile, physical
memory organized in
page frames
Faster response
More users
Lazy swapper – never swaps a page into memory unless page will be
needed (Swapper that deals with pages is a pager)
With swapping, pager guesses which pages will be used before
swapping out again
Instead, pager brings in only those pages into memory
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 are not memory resident
Need to detect and load the page into memory from storage
Without changing program behavior
Without programmer needing to change code
⌧
⌧
⌧
When a page fault occurs, the operating system must bring the
desired page from secondary storage into main memory
Most operating systems maintain a free-frame list – a pool of free
frames for satisfying such requests
8. Save the registers and process state for the other user processes
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in
memory
12. Restore the user registers, process state, and new page table, and
then resume the interrupted instruction
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
4. Continue the process by restarting the instruction that caused the
trap
Note: now potentially 2 page transfers for page fault increasing EAT
15 page faults
Replace page that will not be used for longest period of time
9 is optimal for the example
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, check counters to find smallest value
Search through table needed
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 (for the example of reference string)
But each update more expensive
No search for replacement
LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
Operating System Concepts 37 Silberschatz, Galvin and Gagne ©2018
LRU Approximation Algorithms
F F F
F F F
F F F
F F F
Improve algorithm by using reference bit and modify bit (if available)
in concert
Take ordered pair (reference, modify):
(0, 0) neither recently used nor modified => best page to replace
(0, 1) not recently used but modified => not quite as good, must write out
before replacement
(1, 0) recently used but clean => probably will be used again soon
(1, 1) recently used and modified => probably will be used again soon
and need to write out before replacement
When page replacement called for, use the clock scheme but use the
four classes replace page in lowest non-empty class
Might need to search circular queue several times
2 pages to handle to
priority allocation
Local replacement – each process selects from only its own set of
allocated frames
More consistent per-process performance
if D > m Thrashing
One policy: if D > m, then suspend or swap out one of the processes
=2
Fig from Feitelson
Operating System Concepts 60 Silberschatz, Galvin and Gagne60
©2018
=6
Whenever a timer interrupts, copy and sets the values of all reference
bits to 0
Alternate strategy
Slab is one or more physically contiguous pages
Cache consists of one or more slabs
Single cache for each unique kernel data structure
Each cache filled with objects – instantiations of the data structure
When cache created, filled with objects marked as free
When structures stored, objects marked as used
If slab is full of used objects, next object allocated from empty slab
If no empty slabs, new slab allocated
Benefits include no fragmentation, fast memory request satisfaction
Slab started in Solaris, now wide-spread for both kernel mode and
user memory in various OSes
Linux 2.2 had SLAB, now has both SLOB and SLUB allocators
SLOB (Simple List of Blocks) for systems with limited memory
maintains 3 list objects for small, medium, large objects
Pre-paging
Page size
TLB reach
Inverted page table
Program structure
But if pre-paged pages are unused, I/O and memory was wasted
Assume s pages are pre-paged and α of the pages is used
Is cost of s * α save pages faults > or < than the cost of pre-paging
s * (1- α) unnecessary pages?
Program structure
int[128,128] data;
Windows
select for replacement a frame from a process with lower priority number
Assume that this number of free frames falls below a certain threshold
that triggers page replacement. The replacement algorithm (say, an LRU
approximation algorithm) selects four frames - 15, 3, 35, and 26 - to place
on the free-frame list. It first places these frames on a modified-frame list.
Typically, the modified-frame list would next be written to swap space,
making the frames available to the free-frame list. An alternative strategy
is to compress a number of frames, say three, and store their
compressed versions in a single page frame.
TLB reach refers to the amount of memory accessible from the TLB
and is equal to the number of entries in the TLB multiplied by the
page size. One technique for increasing TLB reach is to increase the
size of pages.
Linux, Windows, and Solaris manage virtual memory similarly,
using demand paging and copy-on-write, among other features. Each
system also uses a variation of LRU approximation known as the
clock algorithm.