Professional Documents
Culture Documents
CH 07
CH 07
___________________________
Memory Management
1
Goals of memory management
Allocate memory resources among competing processes,
maximizing memory utilization and system throughput
Provide isolation between processes
No process shall overwrite other process’s location
Provide a convenient abstraction for programming (and
for compilers, etc.)
2
2
Tools of memory management
Base and limit registers
Swapping
Paging (and page tables and TLB’s)
Segmentation (and segment tables)
Page faults => page fault handling => virtual memory
The policies that govern the use of these mechanisms
3
Today’s desktop and server systems
The basic abstraction that the OS provides for memory
management is virtual memory (VM)
Efficient use of hardware (real memory)
VM enables programs to execute without requiring their entire
address space to be resident in physical memory
Many programs don’t need all of their code or data at once (or
ever – branches they never take, or data they never read/write)
No need to allocate memory for it, OS should adjust amount
allocated based on run-time behavior
Program flexibility
Programs can execute on machines with less RAM than they
“need”
On the other hand, paging is really slow, so must be minimized!
Protection
Virtual memory isolates address spaces from each other
One process cannot name addresses visible to others; each
process has its own isolated address space
4
VM requires hardware and OS support
MMU’s, TLB’s, page tables, page fault handling, …
Typically accompanied by swapping, and at least limited
segmentation
5
A trip down Memory Lane …
Single task system
Why?
Because it’s instructive
Because embedded processors (98% or more of all processors)
typically don’t have virtual memory
An embedded system may have only one program!
6
Cont…
Swapping
save a program’s entire state (including its memory image) to disk
allows another program to be run
first program can be swapped back in and re-started right where it
was
7
Then came multiprogramming
multiple processes/jobs in memory at once
to overlap I/O and computation between processes/jobs,
easing the task of the application programmer
memory management requirements:
protection: restrict which addresses processes can use, so
they can’t stomp on each other
fast translation: memory lookups must be fast, in spite of
the protection scheme
fast context switching: when switching between jobs,
updating memory hardware (protection and translation) must
be quick
8
Logical vs. Physical Address Space
Logical address – is address generated by the CPU;
also referred to as virtual address
Physical address – address seen by the memory unit
9
Memory-Management Unit (MMU)
Hardware device that at run time maps virtual to physical
address
10
Memory-Management Unit (Cont.)
Consider simple scheme. which is a generalization of the
base-register scheme.
The base register now called relocation register
The value in the relocation register is added to every
address generated by a user process at the time it is sent
to memory
12
Contiguous Allocation
Contiguous allocation is one early method
In contiguous allocation, each process contained in single
contiguous section of memory
Main memory must support both OS and user processes
Hence, Main memory usually splits into two partitions:
Resident operating system, usually held in low memory with
interrupt vector
User processes then held in high memory
13
Contiguous Allocation (Cont.)
Relocation registers used to protect user processes from
each other, and from changing operating-system code and
data
Base register contains value of smallest physical address
Limit register contains range of logical addresses – each logical
address must be less than the limit register
MMU maps logical address dynamically
14
Contiguous Allocation technique #1: Fixed partitions
Physical memory is broken up into fixed partitions
partitions may have different sizes, but partitioning never changes
hardware requirement: base register, limit register
physical address = virtual address + base register
base register loaded by OS when it switches to a process
how do we provide protection?
if (physical address > base + limit) then… ?
Advantages
Simple
Problems
internal fragmentation: the available partition is larger than what
was requested
external fragmentation: two small partitions left, but one big job –
what sizes should the partitions be??
15
Mechanics of fixed partitions
physical memory
0
limit register base register partition 0
2K
2K P2’s base: 6K
partition 1
6K
yes
offset <? + partition 2
virtual address 8K
no
partition 3
raise
protection fault
12K
16
Contiguous Allocation technique #2: Variable partitions
Obvious next step: physical memory is broken up into
partitions dynamically – partitions are tailored to programs
hardware requirements: base register, limit register
physical address = virtual address + base register
how do we provide protection?
if (physical address > base + limit) then… ?
Advantages
no internal fragmentation
simply allocate partition size to be just big enough for process
(assuming we know what that is!)
Problems
external fragmentation
as we load and unload jobs, holes are left scattered throughout
physical memory
17
Mechanics of variable partitions
physical memory
partition 2
yes
offset <? + partition 3
virtual
address no
raise
protection fault partition 4
18
Dealing with fragmentation
Compaction:- Compact
memory by copying
Swap a program out partition 0 partition 0
Re-load it, adjacent
partition 1 partition 1
to another
partition 2
Adjust its base partition 2 partition 3
register
partition 3 partition 4
“Lather, rinse,
repeat”
Very expensive partition 4
19
Memory Management Problems
Fixed partitions
Suffer from internal fragmentation
Variable partitions
Suffer from external fragmentation
Compaction
Suffer from overhead
Swapping
Requires writing to disk sectors
20
Modern techniques
• Segmentation
• Paging
21
Segmentation
Segmentation is non-contiguous memory allocation technique.
It is Memory-management scheme that supports user view of memory
A program is a collection of segments
A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
22
User’s View of a Program
23
Logical View of Segmentation
1
4
1
2
3 2
4
3
24
Characteristics
Segmentation is a variable size partitioning scheme.
memory divided into partitions of unequal size.
The size of partitions depend on the length of modules.
The partitions of secondary memory are called
as segments.
25
Segmentation Architecture
Logical address consists of a two parts:
<segment-number, offset>,
Segment table – It maps two-dimensional Logical address
into one-dimensional Physical address.
each table entry has:
base – contains the starting physical address where the
segments reside in memory
limit – specifies the length of the segment
27
Segmentation Hardware
28
Example
29
Paging
It is another noncontiguous memory allocation scheme
Divide physical memory into fixed-sized blocks called frames
Size is power of 2, between 512 bytes and 16 Mbytes
Divide logical memory into blocks of same size called pages
Keep track of all free frames
To run a program of size N pages, need to find N free frames and load
program
Uses a page table to translate logical to physical addresses
Backing store likewise split into pages
Still have Internal fragmentation
Process is allocated physical memory whenever the latter is available
Avoids external fragmentation which is the problem of varying sized
memory chunks
30
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
31
Paging Hardware
32
Paging Model of Logical and Physical Memory
33
Formula on address transliteration
For Process
Virtual Address Space = Size of process
Number of pages the process is divided = Process size / Page size
If process size = 2X bytes, then number of bits in virtual address space = X bits
For Page Table-
Size of page table = Number of entries in page table x Page table entry size
Number of entries in pages table = Number of pages the process is divided
Page table entry size = Number of bits in frame number + Number of bits used
for optional fields if any
For Main Memory-
Physical Address Space = Size of main memory
Size of main memory = Total number of frames x Page size
Frame size = Page size
If number of frames in main memory = 2n, then number of bits in frame number = n bits
If Page size = 2n Bytes, then number of bits in page offset = n bits
If size of main memory = 2n Bytes, then number of bits in physical address = n bits
If the memory is byte-addressable, then size of one location = 1 byte.
If the memory is word-addressable where 1 word = m bytes, then size of one location =
m bytes.
Thus, size of memory = 2n x m bytes.
34
Paging Example
38
Free Frames
39
Implementation of Page Table
Page table is kept in main memory
Page-table base register (PTBR) points to the page table
Page-table length register (PTLR) indicates size of the
page table
In this scheme every data/instruction access requires two
memory accesses
One for the page table and one for the data / instruction
The two memory access problem can be solved by the use
of a special fast-lookup hardware cache called associative
memory or translation look-aside buffers (TLBs)
40
Implementation of Page Table (Cont.)
Some TLBs store address-space identifiers (ASIDs) in
each TLB entry – uniquely identifies each process to
provide address-space protection for that process
Otherwise need to flush at every context switch
TLBs typically small (64 to 1,024 entries)
On a TLB miss, value is loaded into the TLB for faster
access next time
Replacement policies must be considered
Some entries can be wired down for permanent fast access
41
Associative Memory
Associative memory – parallel search
P a ge # F ra m e #
42
Paging Hardware With TLB
43
Effective Access Time
Hit ratio – percentage of times that a page number is found in the TLB
An 80% hit ratio means that we find the desired page number in the
TLB 80% of the time.
Suppose that 10 nanoseconds to access memory.
If we find the desired page in TLB then a mapped-memory access
take 10 ns
Otherwise we need two memory access so it is 20 ns
Effective Access Time (EAT)
EAT = 0.80 x 10 + 0.20 x 20 = 12 nanoseconds
implying 20% slowdown in access time
Consider amore realistic hit ratio of 99%,
EAT = 0.99 x 10 + 0.01 x 20 = 10.1ns
implying only 1% slowdown in access time.
44
Memory Protection
Memory protection implemented by associating protection
bit with each frame to indicate if read-only or read-write
access is allowed
Can also add more bits to indicate page execute-only, and so on
Valid-invalid bit attached to each entry in the page table:
“valid” indicates that the associated page is in the process’ logical
address space, and is thus a legal page
“invalid” indicates that the page is not in the process’ logical
address space
Or use page-table length register (PTLR)
Any violations result in a trap to the kernel
45
Valid (v) or Invalid (i) Bit In A Page Table
46
Shared Pages
Shared code
One copy of read-only (reentrant) (non self modifying code) shared
among processes (i.e., text editors, compilers, window systems)
reentrant code never changes during execution.
Thus, two or more processes can execute the same code at the same
time.
Similar to multiple threads sharing the same process space
Also useful for inter-process communication if sharing of read-write
pages is allowed
Shared code(Pages) must appear in same location in the logical
address space of all processes.
Private code and data
Each process keeps a separate copy of the code and data
The pages for the private code and data can appear anywhere in
the logical address space
47
Shared Pages Example
48
Structure of the Page Table
Memory structures for paging can get huge using straight-
forward methods
Consider a 32-bit logical address space as on modern computers
Page size of 4 KB (212)
Page table would have 1 million entries (232 / 212)
If each entry is 4 bytes -> 4 MB of physical address space /
memory for page table alone
That amount of memory used to cost a lot
Don’t want to allocate that contiguously in main memory
Solution
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
49
Hierarchical Page Tables
Break up the logical address space into multiple page
tables
A simple technique is a two-level page table
We then page the page table
50
Two-Level Page-Table Scheme
51
Two-Level Paging Example
A logical address (on 32-bit machine with 1K page size) is
divided into:
a page number consisting of 22 bits
a page offset consisting of 10 bits
53
64-bit Logical Address Space
Even two-level paging scheme not sufficient
If page size is 4 KB (212)
Then page table has 252 entries
If two level scheme, inner page tables could be 210 4-byte entries
Address would look like
55
Hashed Page Tables
Common in address spaces > 32 bits
The virtual page number is hashed into a page table
This page table contains a chain of elements hashing to the same
location
Each element contains (1) the virtual page number (2) the
value of the mapped page frame (3) a pointer to the next
element
Virtual page numbers are compared in this chain searching
for a match
If a match is found, the corresponding physical frame is extracted
56
Hashed Page Table
57
Inverted Page Table
Rather than each process having a page table and keeping track of all
possible logical pages, track all physical pages
There is only a single table
One entry for each real page of memory
Entry consists of
Virtual address of the page stored in that real memory location, and
PID of the process that owns that page
Advantage
Decreases memory needed to store each page table,
Disadvantage
increases time needed to search the table when a page reference occurs
However, TLB can accelerate access
58
Inverted Page Table Architecture
59
End of Chapter Seven
60