Professional Documents
Culture Documents
Memory Management
(Chapter 8: 8.1-8.6)
Overview
• Provide Services (done)
– processes (done)
– files (after memory management)
• Manage Devices
– processor (done)
– memory (next!)
– disk (done after files)
Simple Memory Management
• One process in memory, using it all
– each program needs I/O drivers
– until 1960
I/O drivers
RAM User
Prog
Simple Memory Management
• Small, protected OS, drivers
– DOS
Device
OS ROM Drivers
ROM
User User
RAM Prog RAM User RAM Prog
Prog
OS OS
• “Mono-programming” -- No multiprocessing!
- Early efforts used “Swapping”, but slooooow
Multiprocessing w/Fixed Partitions
Simple!
Partition 4 Partition 4
900k
Partition 3 Partition 3
500k
Partition 2 Partition 2
300k
Partition 1 Partition 1
200k
OS OS
(a) (b)
Printf.o Main.o
Linker
ar X Window code:
- 500K minimum
Static a.out - 450K libraries
Library
Loader
Memory
Load Time Dynamic Linking
Printf.c Main.c
gcc gcc
Printf.o Main.o
• Save disk space.
Linker • Libraries move?
ar
• Moving code?
Dynamic a.out • Library versions?
Library • Load time still
Loader
the same.
Memory
Run-Time Dynamic Linking
Printf.c Main.c
gcc gcc
Printf.o Main.o
Linker
ar Save disk space.
Startup fast.
Dynamic a.out Might not need all.
Library
Loader
Run-time
Loader
Memory
Memory Linking Performance
Comparisons
Linking Disk Load Run Run Run Time
Method Space Time Time Time (0 used)
(4 used) (2 used)
Static 3Mb 3.1s 0 0 0
yes
CPU < +
physical
no address
MMU
error
OS
process 3
malloc(20k)?
process 8
process 2
Internal Fragmentation
• Have some “empty” space for each
processes
A stack
Allocated to A Room for growth
A data
A program
OS
process 3
125k Process 9 ?
process 8
100k
process 2
100k
process 2
CPU p d f d
physical
f memory
page table
Paging Example
0
• Page size 4 bytes
• Memory size 32 bytes (8 pages) 1 Page 0
Page 0 3 Page 2
0 1
4 Page 1
Page 1 1 4
Page 2 5
2 3
Page 3 3 7 6
7 Page 3
Logical Page Table
Memory Physical
Memory
Paging Example
Offset
000 000
Page 0
001 0 0 1 0 1 1 001
010 010
Page 1
Page Frame
011 00 01 011
100 100
Page 2
01 11
101 101
10 00
110 110
Page 3
11 10
111 111
Page Table
Logical Physical
Memory Memory
Paging Hardware
• address space 2m
• page offset 2n phsical
memory
• page number 2m-n 2m bytes
0
Page 0 0 3
1 Page 0A
Page 1 1 7
2
Page Table
Process B
3 Page 0B
page number page offset
p d 4 Page 1A
m-n=3 n=4
5
Page 0 0 1
6
Page 1 1 4
7 Page 1B
Process A Page Table
Physical
Memory
Paging Tradeoffs
• Advantages
– no external fragmentation (no compaction)
– relocation (now pages, before were processes)
• Disadvantages
– internal fragmentation
+ consider: 2048 byte pages, 72,766 byte proc
– 35 pages + 1086 bytes = 962 bytes
+ avg: 1/2 page per process
+ small pages!
– overhead
+ page table / process (context switch + space)
+ lookup (especially if page to disk)
Implementation of Page Table
• Page table kept in registers
• Fast!
• Only good when number of frames is small
• Expensive!
Registers
Memory
Disk
Implementation of Page Table
• Page table kept in main memory
• Page Table Base Register (PTBR) 0 1 4
Page 0 0 1 1 Page 1
Logical 3
Page Table
Memory Physical
Memory
• Page Table Length
• Two memory accesses per data/inst access.
– Solution? Associative Registers
Associative Registers
logical
address p d 10-20% mem time
physical
address
physical
memory
associative
registers
f
miss
(Intel P3 has 32 entries) Also called Translation
page table Lookaside Buffer (TLB)
(Intel P4 has 128 entries)
Associative Register Performance
• Hit Ratio - percentage of times that a page
number is found in associative registers
Effective access time =
hit ratio x hit time + miss ratio x miss time
• hit time = reg time + mem time
• miss time = reg time + mem time * 2
• Example:
– 80% hit ratio, reg time = 20 nanosec, mem time
= 100 nanosec
– .80 * 120 + .20 * 220 = 140 nanoseconds
Protection
• Protection bits with each frame
• Store in page table
• Expand to more perms Protection
Bit
0 1 v
Page 0 0 Page 1
1 0 v
Page 1 1 Page 0
2 3 v
Page 2 2
3 0 i
3 Page 2
Logical Page Table
Memory Physical
Memory
Large Address Spaces
• Typical logical address spaces:
– 4 Gbytes => 232 address bits (4-byte address)
• Typical page size:
– 4 Kbytes = 212 bits
• Page table may have:
– 232 / 212 = 220 = 1million entries
• Each entry 3 bytes => 3MB per process!
• Do not want that all in RAM
• Solution? Page the page table
– Multilevel paging
Multilevel Paging
page number page offset
p1 p2 d
10 10 12
...
Page 0
...
...
...
...
Logical ...
Memory Outer Page
Table
Page Table
Multilevel Paging Translation
page number page offset
p1 p2 d
p1
p2
d
outer page desired
table inner page page
table
Inverted Page Table
• Page table maps to physical addresses
CPU pid p d
i d
i
search
pid p Physical
Memory
CPU
main
limit base
+
< physical
yes address
stack
no
error