CS170 Lecture notes -- Memory Management
This lecture will attempt to cover the various concepts that are important
to the memory management functions that most operating systems must
perform.
Linking and Loading
- program addresses are logical, machine addresses are physical
- linking -- resolving logical addresses relative to the entire program
- .o files have logical addresses
- when multiple .o files are linked, addresses must be altered
- symbol table contains necessary information
- Unix utility ld() is the linker
- loading -- the introduction of a program into physical memory
- logical program addresses must be converted to physical machine addresses
Base and Bounds
- when the program is loaded, a base register is set to contain the
physical address of where the first logical address of the program will
be located in memory
- a bounds register is loaded with the physical address of the last
(highest addressed) physical memory location that the program can access
- all program instructions refer to logical addresses
- whenever an instruction attempts to access a logical memory location, the
hardware automatically
- adds the logical address to the contents of the base
register to form the physical address of the access
- compares the physical address to both the base register
and the bounds register
- if the physical address is less than the base or greater than
the bounds an address out of range exception is generated
- otherwise, the access processed with the new physical address
- OS must ensure that base+bounds pairs for different programs do not
overlap
- can also be implemented with a check on the logical bounds (see
page 284)
Static versus Dynamic Linking
- static linking: all addresses are resolved before the program is loaded
- dynamic linking: addresses are resolved "on demand"
- when an address exception occurs, the exception handler
- checks the logical address to determine if it refers to
a routine or variable that must be dynamically linked
from the information kept in a "link table"
- if it is a valid address, the routine is loaded and the
memory management state is adjusted
- for example: bounds registers would be incremented to reflect
additional code or variable
- instruction causing the exception is restarted
Overlays
- used to allow a program with a physical address space that is smaller
than than the logical address region the program occupies
- idea: group routines into bundles that can be swapped in and out of
an overlay region in the logical space of the program
- the programmer must be careful to organize the program so that functions
in one overlay do not access variables defined in another since they
cannot both be in memory at the same time
Memory Partitioning
- each program that is in memory must have its own base+bounds pair.
- memory is is allocated contiguously.
- when a program is context switched, the contents of the base+bounds
registers are saved, and the contents of the incoming process
pair is loaded
- active program not in memory are swapped to disk
- OS keeps track of where on disk the program image is
- at context switch, the base+bounds are set for the program
coming in
- easiest way to get multiple programs in memory is to break memory
up into fixed partitions
- fixed number of programs running in system
at any one time.
- use a table to keep track of where loaded
programs are and where the holes are
- MFT
- variable partition: allocate contiguous regions of memory
using a "busy/free" data structure
- when a program is loaded or swapped in, the OS
- looks for a hole that is big enough to hold the program
- first fit
- best fit
- worst fit -- reduces the largest left-over hole size
- sets the base+bounds pair for the hole
- loads the program into the hole
- adjusts the table to reflect the newly loaded program
- external fragmentation: left-over holes are too small to hold
a new program even though there may be enough total free memory to do
so
- book-keeping for holes can be expensive: a 1 byte hole needs an entire
record in the table
- to cut down on space overhead, memory is allocated in fixed-size blocks
- internal fragmentation: space within each block that is not used
totals a substantial amount even when all blocks are used
Paging (basic)
- physical memory is partitioned into fixed size frames
- logical memory is divided into fixed size pages
- every logical address is really an ordered pair (page number, offset)
(see figure 9.7 page 289)
- hardware uses page number as an offset into a contiguous page
table that contains a physical frame number
- the frame number is pre-pended to the offset to form the physical
address (see figure 9.6, page 288)
- each process has its own page table that maps pages to frames
- OS maintains a list of free frames
Problems with Paging
- page table can be huge and must be contiguous
- 512 byte pages => offset is 9 bits
- 32-bit address => page number is 23 bits => 8388608 page
table entries
- if page table entry is an int => 33554432 (32 MB) page tables
- each process, then, need a 32MB contiguous region in the
kernel => kernel is at least 32MB in size
- larger pages => more internal fragmentation, but smaller pages tables
- what if address space is 64 bits?
- each memory access is really two accesses: one for page table and one for
memory => memory speed is halved
- Translation Lookaside Buffer (TLB)
- cache page to frame mapping
- process id needed to differentiate between processes sharing
the TLB
Hierarchical Page Table
- solution to large page table is to page the page table
- requires that pages be stored on disk when memory if full => virtual
memory
- address is a three-tuple: (pt1, pt2, offset) -- see figure
9.12 on page 297
- first page number gives a page of page table entries
- each page of page-table entries contains frame numbers
- can be made to work with multiple levels with a cost: each additional
page table level adds a memory reference cost to each memory
reference made by a program
Inverted Page Table
- one physical frame table for all of memory
- entry stores pid and logical page number of the page that
is mapped there
- frame table is searched for (pid,page number) pair on each
ref
- offset into frame table gives frame number of physical address
when matched (see figure 9.15, page 301)
- advantage is that the frame table need only be big enough to map physical
memory of machine
- disadvantage is that table must be searched on every access
- hashing
- better hope the TLB is working
- UltraSPARC and PowerPC both support -- need I say more?
Memory Protection
- page table entries include bits indicating
- read only, r/w
- valid/invalid
- allows address faults to be trapped and handled
- stack over-run
- dynamic linking