8 Memory
8 Memory
8 Memory
Storage Management
Chapter 8: Memory Management
Address Generation
qAddress generation has three stages:
vCompile: compiler
vLink: linker or linkage editor
vLoad: loader
0 0 0
OS OS
user prog.
user prog.
user prog.
device drivers
OS in ROM
max max max
Why Multiprogramming?
OS OS
300k 300k
partition 1 partition 1
200k 200k
partition 2 partition 2
limit base
memory
yes
CPU < +
logical
address no physical
address
not your space
traps to the OS
addressing error
Relocation: How does it work?
Multiprogramming with Variable Partitions
qThe OS maintains a memory pool. When a job
comes in, the OS allocates whatever a job needs.
qThus, partition sizes are not fixed, The number
of partitions also varies.
OS OS OS OS
A A A A
B B free
free
C C
free
free free
Memory Allocation: 1/2
qWhen a memory request comes, we must
search all free spaces (i.e., holes) to find a
suitable one.
qThere are some commonly seen methods:
vFirst Fit: Search starts at the beginning of the set of
holes and allocate the first large enough hole.
vNext Fit: Search starts from where the previous first-
fit search ended.
vBest-Fit: Allocate the smallest hole that is larger
than the request one.
vWorst-Fit: Allocate the largest hole that is larger
than the request one.
Memory Allocation: 2/2
qIf the hole is larger than the requested size, it is cut
into two. The one of the requested size is given to
the process, the remaining one becomes a new hole.
qWhen a process returns a memory block, it becomes
a hole and must be combined with its neighbors.
before X is freed after X is freed
A X B A B
A X A
X B B
X
Fragmentation
qProcesses are loaded and removed from memory,
eventually the memory will be cut into small holes
that are not large enough to run any incoming
process.
qFree memory holes between allocated ones are
called external fragmentation.
qIt is unwise to allocate exactly the requested
amount of memory to a process, because of
address boundary alignment requirements or the
minimum requirement for memory management.
qThus, memory that is allocated to a partition, but is
not used, are called internal fragmentation.
External/Internal Fragmentation
allocated partition
external
used fragmentation
used
free
used un-used
used
used
free internal fragmentation
used
Compaction for External Fragmentation
qIf processes are relocatable, we may move used
memory blocks together to make a larger free
memory block.
used used
free used
used used
used used
used
free
free
free
used used
Paging: 1/2
qThe physical memory is divided into fixed-sized
page frames, or frames.
qThe virtual address space is also divided into
blocks of the same size, called pages.
qWhen a process runs, its pages are loaded into
page frames.
qA page table stores the page numbers and their
corresponding page frame numbers.
qThe virtual address is divided into two fields:
page number and offset (with that page).
logical address
Paging: 2/2 physical
memory
p d 0
page # offset within the page 1
logical
2
memory d
0 page table 3
0 7
1 4
d 1 2
2 9 5
2
3 5
6
3
7
8
logical address <1, d> translates to 9
physical address <2,d>
10
Address Translation
Address Translation: Example
Hardware Support
qPage table may be stored in special registers if the
number of pages is small.
qPage table may be stored in physical memory, and a
special register, page-table base register, points to
the page table.
qUse translation look-aside buffer (TLB). TLB stores
recently used pairs (page #, frame #). It compares
the input page # against the stored ones. If a match
is found, the corresponding frame # is the output.
Thus, no physical memory access is required.
qThe comparison is carried out in parallel and is fast.
qTLB normally has 64 to 1,024 entries.
Translation Look-Aside Buffer
valid
page # frame #
Y 123 79
Y 374 199
p (page #) N 906 3
Y 767 100
if page # = 767,
N 222 999 Output frame # = 100
Y 23 946
If the TLB reports no hit, then we go for a page table look up!
Fragmentation in a Paging System
qDoes a paging system have fragmentation?
qPaging systems do not have external
fragmentation, because un-used page frames
can be used by the next process.
qPaging systems do have internal fragmentation.
qBecause the address space is divided into equal
size pages, all but the last one will be filled
completely. Thus, the last page contains
internal fragmentation and may be 50% full.
Protection in a Paging System
qIs it required to protect among users in a paging
system? No, because different processes use
different page tables.
qHowever, we can use a page table length register
that stores the length of a processs page table. In
this way, a process cannot access the memory
beyond its region. Compare this with the
base/limit register pair.
qWe can also add read-only, read-write, or execute
bits in page table to enforce r-w-e permission.
qWe can also add a valid/invalid bit to each page
entry to indicate if the page is in memory.
Shared Pages
q Pages may be shared by multiple processes.
q If the code is a re-entrant (or pure) one, a program does
not modify itself, routines can also be shared!
logical space page table
0 A 0 M
1 B 1
2 X 2
B
N
Multi-Level Page Table
virtual address
index 1 index 2 index 3 offset
page table 8 6 6 12
base register memory
level 1
page table level 2
page table level 3
page table
q There are 256, 64 and 64 entries in level 1, 2 and 3
page tables.
q Page size is 4K = 4,096 bytes.
q Virtual space size = (28*26*26 pages)*4K= 232 bytes
Inverted Page Table: 1/2
qIn a paging system, each process has its own page
table, which usually has many entries.
qTo save space, we can build a page table which has
one entry for each page frame. Thus, the size of this
inverted page table is equal to the number of page
frames.
qEach entry in an inverted page table has two items:
vProcess ID: the owner of this frame
vPage Number: the page number in this frame
qEach virtual address has three sections:
<process-id, page #, offset>
Inverted Page Table: 2/2 memory
k d
k
pid page #