CSCI430530 Lecture10
CSCI430530 Lecture10
Chapter 8
Virtual Memory
7/18/10
Roadmap
• Hardware and Control Structures
• Operating System Software
• UNIX and Solaris Memory Management
• Linux Memory Management
• Windows Memory Management
Terminology
Key points in
Memory Management
1) Memory references are logical addresses
dynamically translated into physical addresses at
run time
– A process may be swapped in and out of main
memory occupying different regions at different
times during execution
2) A process may be broken up into pieces that
do not need to located contiguously in main
memory
Breakthrough in
Memory Management
• If both of those two characteristics are
present,
– then it is not necessary that all of the pages or all
of the segments of a process be in main memory
during execution.
• If the next instruction, and the next data
location are in memory then execution can
proceed
– at least for a time
Execution of a Process
• Operating system brings into main memory a
few pieces of the program
• Resident set - portion of process that is in
main memory
• An interrupt is generated when an address is
needed that is not in main memory
• Operating system places the process in a
blocking state
Execution of a Process
• Piece of process that contains the logical
address is brought into main memory
– Operating system issues a disk I/O Read request
– Another process is dispatched to run while the
disk I/O takes place
– An interrupt is issued when disk I/O complete
which causes the operating system to place the
affected process in the Ready state
Implications of
this new strategy
• More processes may be maintained in main
memory
– Only load in some of the pieces of each process
– With so many processes in main memory, it is very
likely a process will be in the Ready state at any
particular time
• A process may be larger than all of main
memory
Real and
Virtual Memory
• Real memory
– Main memory, the actual RAM
• Virtual memory
– Memory on disk
– Allows for effective multiprogramming and
relieves the user of tight constraints of main
memory
Locality and Virtual Memory
• Benefits (1 & 2) are attractive, but is the scheme
practical
– Q: won't interrupting to page in in the
middle of program cause noticeable delays?
Locality and Virtual Memory
• Over any short period of time, execution may be
confined to a small section of the program (e.g. a
subroutine or a loop).
– Only access a few local variables, or an array.
• Wasteful to load all of the other stuff, not being used
– Often the case in simple paging or partitioning
with swapping, we swap out a process after being
swapped in, and it only used a very small part of
its data and code during that time.
Thrashing
• A state in which the system spends most of its
time swapping pieces rather than executing
instructions.
• To avoid this, the operating system tries to guess
which pieces are least likely to be used in the near
future.
• The guess is based on recent history
Principle of Locality
• Program and data references within a process
tend to cluster
• Only a few pieces of a process will be needed
over a short period of time
• Therefore it is possible to make intelligent
guesses about which pieces will be needed in
the future
• This suggests that virtual memory may work
efficiently
A Processes Performance
in VM Environment
• Note that during
the lifetime of the
process, references
are confined to a
subset of pages.
Support Needed for
Virtual Memory
• Hardware must support paging and
segmentation
• Operating system must be able to manage the
movement of pages and/or segments
between secondary memory and main
memory
Paging
• Each process has its own page table
• Each page table entry contains the frame
number of the corresponding page in main
memory
• Two extra bits are needed to indicate:
– whether the page is in main memory or not
– Whether the contents of the page has been
altered since it was last loaded
(see next slide)
Paging Table
Address Translation
Page Tables
• Page tables are also stored in virtual memory
• When a process is running, part of its page
table is in main memory
Two-Level
Hierarchical Page Table
Address Translation for Hierarchical
page table
Page Size
• Smaller page size, less amount of internal
fragmentation
• But Smaller page size, more pages required
per process
– More pages per process means larger page tables
• Larger page tables means large portion of
page tables in virtual memory
Page Size
• Secondary memory is designed to efficiently
transfer large blocks of data so a large page
size is better
Further complications
to Page Size
• Small page size, large number of pages will be
found in main memory
• As time goes on during execution, the pages in
memory will all contain portions of the
process near recent references. Page faults
low.
• Increased page size causes pages to contain
locations further from any recent reference.
Page faults rise.
Page Size
Roadmap
• Hardware and Control Structures
• Operating System Software
• UNIX and Solaris Memory Management
• Linux Memory Management
• Windows Memory Management
Memory Management
Decisions
• Whether or not to use virtual memory
techniques
• The use of paging or segmentation or both
• The algorithms employed for various aspects
of memory management
Key Design Elements
• Handspread:
– The gap between fronthand and backhand
• Both have defaults set at boot time based on
physical memory
Kernel Memory
Allocator
• The kernel generates and destroys small tables
and buffers frequently during the course of
execution, each of which requires dynamic
memory allocation.
• Most of these blocks significantly smaller than
typical pages,
– Therefore normal paging would be inefficient
• Variation of “buddy system” is used
Lazy Buddy
• UNIX often exhibits steady-state behavior in
kernel memory demand;
– i.e. the amount of demand for blocks of a
particular size varies slowly in time.
• To avoid unnecessary joining and splitting of
blocks,
– the lazy buddy system defers coalescing until it
seems likely that it is needed, and then coalesces
as many blocks as possible.
Lazy Buddy
System Parameters
• Ni = current number of blocks of size 2i
• Ai = current number of blocks of size 2i that
are allocated (occupied).
• Gi = current number of blocks of size 2i that
are globally free.
• Li = current number of blocks of size 2i that
are locally free
Lazy Buddy
System Allocator
Linux
Memory Management
• Shares many characteristics with Unix
– But is quite complex
• Two main aspects
– Process virtual memory, and
– Kernel memory allocation.
Linux
Memory Management
• Page directory
• Page middle directory
• Page table
Linux Virtual Memory
• Three level page table structure
– Each table is the size of one page
• Page directory
– Each process has one page directory
– 1 page in size, must be in main memory
• Page middle directory:
– May be multiple pages, each entry points to one
page in the page table
Linux Memory cont
• Page table
– May also span multiple pages.
– Each page table entry refers to one virtual page of
the process.
Address Translation
Page Replacement
• Based on the clock algorithm
• The “use bit” is replace with an 8-bit age
variable
– Incremented with each page access
• Periodically decrements the age bits
– Any page with an age of 0 is “old” and is a
candidate for replacement
• A form of Least Frequently Used policy
Windows
Memory Management
• The Windows virtual memory manager
controls how memory is allocated and how
paging is performed.
• Designed to operate over a variety of
platforms
– uses page sizes ranging from 4 Kbytes to 64
Kbytes.
Windows Virtual
Address Map
• On 32 bit platforms each user process sees a
separate 32 bit address space
– Allowing 4G per process
• Some reserved for the OS,
– Typically each user process has 32G of available
virtual address space
– With all processes sharing the same 2G system
space
32 bit Windows
Address Space
Windows Paging
• On creation, a process can make use of the
entire user space of almost 2 Gbytes.
• This space is divided into fixed-size pages
managed in contiguous regions allocated on
64Kbyte boundaries
• Regions may be in one of three states
– Available
– Reserved
– Committed
Resident Set
Management System
• Windows uses “variable allocation, local
scope”
• When activated a process is assigned data
structures to manage its working set
• Working sets of active processes are adjusted
depending on the availability of main memory