Operating Systems: Memory Management (Chapter 8: 8.1-8.6)
Operating Systems: Memory Management (Chapter 8: 8.1-8.6)
Overview
Provide Services
processes files
(done)
(done) (after memory management) (done) (next!) (done after files)
Manage Devices
processor memory disk
RAM
User Prog
ROM
RAM
User Prog OS
Mono-programming -- No multiprocessing!
- Early efforts used Swapping, but slooooow
900k
Partition 3
Partition 4 Partition 3
500k
Partition 2 Partition 1 Partition 2
300k 200k
Partition 1
OS
OS
(a)
(b)
Unequal queues
Address Binding
Compile Time
maybe absolute binding (.com) Source
Compile
Object Link Load Module Load RAM Binary Run
Link Time
dynamic or static libraries
Load Time
relocatable code
Run Time
relocatable memory segments overlays paging
Loader
Memory
Loader
Memory
Save disk space. Libraries move? Moving code? Library versions? Load time still the same.
Loader
0 0 1.2s
0 0 0
Dynamic solutions
compute when needed for unpredictable situations
Relocation Register
Physical Address
CPU
346
14000 +
14346
Memory
MMU
CPU
<
+
physical address
MMU
Variable-Sized Partitions
Idea: want to remove wasted memory that
is not needed in each partition Definition:
Hole - a block of available memory scattered throughout physical memory
Variable-Sized Partitions
OS process 5 8 done process 8 OS process 5 9 arrv 5 done process 2 process 2 process 2 OS process 5 process 9 10 arrv OS process 9 process 10
process 2
Memory Request?
What if a request for additional memory?
OS
process 3
process 8
malloc(20k)?
process 2
Internal Fragmentation
Have some empty space for each
processes
A stack Allocated to A Room for growth
A data
A program OS
External Fragmentation
External Fragmentation - total memory
space exists to satisfy request but it is not contiguous OS
50k process 3 125k Process 9
?
process 8 100k
process 2
Compaction
(b) OS
process 8
process 8 process 3
process 2
process 2
process 2
Cost of Compaction
process 1 process 1
50k
process 3 90k
process 3
process 8 process 2
Solution?
Want to minimize external fragmentation
Large Blocks But internal fragmentation!
Tradeoff
Sacrifice some internal fragmentation for reduced external fragmentation Paging
Paging Misc
Paging
Logical address space noncontiguous;
process gets memory wherever available
Divide physical memory into fixed-size blocks
+ +
size is a power of 2, between 512 and 8192 bytes called Frames called Pages
Paging
Address generated by CPU divided into:
Page number (p) - index to page table
+
page table contains base address of each page in physical memory (frame)
Paging Example
Page size 4 bytes Memory size 32 bytes (8 pages)
Page 0 0 1 1 4 2 3 0 1 2 3 Page 2 Page 0
Page 1
Page 2
4
5
Page 1
Page 3
Logical Memory
3 7
Page Table
6
7 Page 3
Physical Memory
Paging Example
Page 0 Offset 000 001 010 011 0 Page 0 1 0 Frame 00 01 1 1 000
001
010 011 100 101 110 111
Page 2
Page 1
100
101
01 11
10 00
Page 3
110
111
11 10
Page Table
Logical Memory
Physical Memory
Paging Hardware
address space 2m page offset 2n page number 2m-n
page number p page offset d n
m-n
Paging Example
Consider:
Physical memory = 128 bytes Physical address space = 8 frames
How many bits in an address? How many bits for page number? How many bits for page offset? Can a logical address space have only 2 pages? How big would the page table be?
How many bytes (words) of physical memory? How many frames are there? How many bytes is a page? How many bits for page offset? If a process page table is 12 bits, how many logical pages does it have?
b=7
Page 1
Process B
1 7
Page Table page number p m-n=3
Page 0A
Page 0B
4
5 6
Page 1A
Page 0
0 1
1 4 7 Page Table
Page 1B
Page 1
Process A
Physical Memory
Paging Tradeoffs
Advantages Disadvantages
+
no external fragmentation (no compaction) relocation (now pages, before were processes) internal fragmentation
consider: 2048 byte pages, 72,766 byte proc
35 pages + 1086 bytes = 962 bytes
+
+
avg: 1/2 page per process small pages! page table / process (context switch + space) lookup (especially if page to disk)
overhead
+ +
Memory
Disk
Page 1 Page 0
PTBR
3
Physical Memory
Associative Registers
logical address p d
CPU
hit
f d
physical address
associative registers
physical memory
miss
(Intel P3 has 32 entries) (Intel P4 has 128 entries)
f page table
Also called Translation Lookaside Buffer (TLB)
Protection
Protection bits with each frame Store in page table Expand to more perms
0 1 v v v
Protection Bit
Page 0
Page 1 Page 2 Logical Memory
1 0 2 3
0
1
Page 1
Page 0
3 0 i
Page Table
2
3 Page 2
Physical Memory
Typical logical address spaces:
4 Gbytes => 232 address bits (4-byte address)
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 p1 p2 10 10 page offset d
12
...
...
Page Table
p1 p2 d
desired page
i
search
i
pid p
Physical Memory
Still need page per process --> backing store Memory accesses longer! (search + swap)
Memory View
Paging lost users view of memory Need logical memory units that grow and contract
Solution?
Segmentation!
symbol table
Segmentation
Logical address: <segment, offset> Segment table - maps two-dimensional user
defined address into one-dimensional physical address
base - starting physical location limit - length of segment
Hardware support
Segment Table Base Register Segment Table Length Register
Segmentation
logical address s
CPU limit base < no error (Er, what have we gained?) Paged segments! physical memory yes +
physical address
main
stack
(done)
(done)
(done)
Paging
Basic Enhanced
(done)
(done) (done)
Specific
Windows Linux
Characteristics
Access: read only, read-write States: valid, zeroed, free
Segments
Kernel code, kernel data, user code, user data
middle-layer defined to be 0