1
Operating Systems
Memory management
Memory Management
List of Topics1. Memory Management2. Memory In Systems Design3. Binding Times4. Introduction to Memory Management5. Raw Memory Model6. Single User Contiguous Memory7. Relocation - Why and How8. Overlay Management
Topics (continued)
9. Protection10. Fixed Partitions11. Nonuniform Sized Fixed Partitions12. Uniformly Sized Fixed Partitions13. Simple Paging14. Benefits of Simple Paging15. Page Tables16. Translation Lookaside Buffers17. Hierarchical Address Caching
2
Topics (continued)
18. Dynamic Partitions19. Fragmentation20. Internal Fragmentation21. External Fragmentation22. Coalescing Holes23. Compaction24. Dynamic Partition Placement25. Simple Segmentation26. Memory Layout of A C Program
Topics (continued)
27. malloc28. sbrk29. Memory Hierarchy
Memory In Systems design
CPU memoryI/O
System
Figure 1: memory Connects CPU and Peripherals
3
Binding Timessource
program
compiler orassembler
objectmodel
compiletime
linkage editor
load module
loader
Otherobject
modules
system library
loadtime
in-memorybinary
memoryimage
dynamicallyloaded system
library
executiontime
(run time)
4
Introduction to Memory Management
Memory management issues:1. Relocation 2. Protection3. Sharing4. Logical Organization5. Physical Organization
Consider a series of solutions startingwith the most primitive first
Raw Memory Model
The raw memory provides no services and gives the programmer complete control. 0
1 MB
Figure 3: Raw Machine Model
Single User Contiguous Memory
Primitive operating systems (such as MS-DOS and CP/M) provide some interfaces to the hardware but not much else in the way of services.
5
Single User Contiguous Memory(continued)
Monitor
User
32K
0
fence register
Figure 4: Single User Contiguous Memory
Relocation - Why and How
Relocation refers to the ability to store aprogram at an arbitrary base memory address.
Actual memory locations have physicalor absolute addresses, user program's access these locations using logicaladdresses.
Relocation - Why and How (continued)
+CPU
14000
memory
Base register
Logical address
346
Physical address
14346
Figure 5: Address Translation
6
Overlay Management
Overlays have gone out of fashion withcheaper memory, users (and compilers) determine which code to swap in and out.
symboltable
commonroutines
overlaydriver
pass 1 pass 2
20K
30K
10K
80K70K
Figure 6:OverlayManagementExample [2]
Protection
It is undesirable to permit user programs (accidentally or intentionally) to accesses memory outside of their partition.
7
Protection (continued)
Monitor
User
32K
0
fence register
Figure 7:Protectionin Resident
MonitorModel
Fixed Partitions
Fixed partitioning refers to memory being split into contiguous non overlapping regions of precomputed sizes.
Fixed sized partitions make the selectionof a partition for a job easy.
Nonuniform Sized Fixed Partitions
Fixed Partitions may have differing sizes.
8
Operatingsystem
NewProcess
Figure 8:Nonuniform
fixed Partitions [3]
Uniformly Sized Fixed Partitions
Memory is frequently partitioned into uniformly sized regions.
Operatingsystem512 K
512 K512 K
512 K512 K
512 K512 K512 K
Figure 9:UniformFixed PartitionAllocation [3]
9
Simple Paging
Paging provides relocation, and splitsmemory into fixed length partitionscalled frames.
Page frame 0
Page frame 1
Page frame 2
Page frame 3
0
p-1p
2p-12p
3p-13p
4p-1
Figure 10:SimplePaging
[1]
4p
5p-15p
6p-16p
7p-17p
8p-1
Page frame 4
Page frame 5
Page frame 6
Page frame 7
Figure 10:SimplePaging(continued)
[1]
10
Pageframe
number
01234567
Pageframesize
pppppppp
Range of realstorage
addresses
0 p-1p 2p-12p 3p-13p 4p-14p 5p-15p 6p-16p 7p-17p 8p-1
Benefits of Simple Paging
Simple paging allowsdiscontiguous storage for memory objects exceeding the page frame size.
Virtualmemory
...
...
Addressmapping
mechanism
Realstorage
Contiguousvirtual
storagelocations
11
Page Tables
One simple mechanism is to allocate some real memory space for a table, and hash page addresses using the high order address bits as pointers into the page table. There are 2 real memory accesses per virtual memory access.
Page Tables (continued)
d
b
p
p1 dp1
p
bPage # Displacement
Virtual address}v=(p,d)
Page
b Map Table
p
}Real
address
b+p
Translation Lookaside Buffers
Translation lookaside buffers (TLB) eliminate one physical memory reference using special associative memory, which addressed by its contents in O(1) parallelsearch time.
12
d }Virtual
addressV=(p,d)
p p1
Page # Displacement
p
p1 d }Real
Address r
Frame # Displacement
Associative map
Associative Memory Lookup
Hierarchical Address Caching
Rather than placing all addresses in the TLB recently/frequently used addresses are stored in associative memory, with misses being serviced by the page table.
Hierarchical Address Caching (continued)
Address ofPage table b
Page tableorigin register
p1
Page #p
Displacementd
Virtual address v=(p,d)
+
Partial associative
p p1p
Try this first
Performed only if no match in associative map b+p Real address
Displacementd
Frame# p1
Only if match in associative map
Only if no match in associative map
Direct map (all pages)
Map (most active pages only
13
Dynamic Partitions
Dynamically partitioned memory allows placement of relocatable code in variable size contigous memory regions.
Dynamic Partitions (continued)
user needs
IHGFEDCBA
9K.18K.11K.32K.14K.25K.10K.20K.15K.
Operatingsystem
UserA 15K
Operating
User
User
UserUser
User
User
User
User
User
Operating Operatingsystem systemsystem
Free
B 20K
A 15KA 15KA 15K
B 20K B 20K
FreeFree
C 10K C 10K
D 25KFree
Fragmentation
Fragmentation makes available memory useless by breaking it into discontiguous pieces too small to use.
14
Fragmentation (continued)
There are two categories of memory fragmentation:
1. Internal Fragmentation --- A fixed partition contains more memory than required by the user, and some is wasted.2. External Fragmentation --- Results from the holes left by dynamic partitions.
Internal Fragmentation
Internal fragmentation occurs when fixed size partitions are too large.
Pag
e 2
Pag
e 1
Pag
e 0
Inte
rnal
frag
men
tati
on
Logical AddressPage#=1,Offset=478
000001 01110111110
478
Figure 16:InternalFragmentation[3]
15
External Fragmentation
External fragmentation happens when dynamic partitions are released. The fragments are frequently called holes .
operatingsystem
User A
User B
User C
User D
User E
Hole
operatingsystem
User A
User C
User D
User E
Hole
operatingsystem
User A
User C
User E
Hole
Hole Hole
Hole
User B finishes and frees its storage. User D
finishes and frees its storage.
Coalescing Holes
Adjacent holes in dynamic partitionsshould be coalesced into a single largerhole.
16
operatingsystem
other users
5K user A
otherusers
2K Hole 2K Hole
5K Hole
otherusers
other users
operatingsystem
7K Hole
otherusers
other users
operatingsystem
User A finishes and frees its storage
Operating system combines adjacent
holes to form a single larger hole.
Compaction
If the amount of memory available in theholes is large enough to service arequest, the holes may made contiguousby compacting storage.
operatingsystems
In use
In use
In use
Free
Free
Free
operatingsystems
Free
In useIn use
In use
Operating system places all “in use” blocks together leaving free storage as a single, large hole.
17
(a) First-Fit Strategy
Place job in first storage hole on free storage place list in which it will fit
Dynamic Partition Placement
Operating systems
16K hole
14K hole
5K hole
30K hole
In use
In use
In use
a 16Kc 14K
e 5K
g 30K...
Request for 13K
Free Storage List
Start Address Length
(kept in storage address order, or something in random order.)
0
a
b
c
d
e
f
g
h
(b) Best-Fit Strategy
Place job in the smallest possible hole in which it will fit.
18
Operating systems
16K hole
14K hole
5K hole
30K hole
In use
In use
In use
e 5Kc 14K
a 16K
g 30K...
Request for 13KStart
Address Length
(Kept in ascending order by hole size.)
0
a
b
c
d
e
f
g
h
Free Storage List
(c) Worst-Fit Strategy
Place job in the largest possible hole in which it will fit.
Operating systems
16K hole
14K hole
5K hole
30K hole
In use
In use
In use
g 30Ka 16K
c 14K
e 5K...
Request for 13KStart
Address Length
(Kept in descending order by hole size.)
0
a
b
c
d
e
f
g
h
Free Storage List
19
Simple Segmentation
Segmentation provides relocation, and supports contiguous variable length partitions.
Segmentation often provides protection (counterexample Intel 8086).
CPU <
Logical address
no
Trap; address error
+yes
limit base
memory
Memory Layout of A C Programs
Traditional Unix/C memory images of programs use segments.
20
high address
low address text
uninitialized data(bss)
heap
stack
Initialized data
Command-line arguments and environment variables
initialized to zero by exec
read from program fileby exec
malloc
Programmers often want to allocate data objects which persist beyond the function call creating them (e.g. constructors in OOP).
In C and C++ the malloc operator maintains a linked list of data objects, in the user program's Data segment (on the Heap).
user data
user data
user data
user data
user data
user data
In Use List
Free List
Key
Memory management Information
21
sbrk
A user program can exhaust its defaultheap space allocation.
The Unix sbrk system call increases data segment allocation at run time.
Memory allocationfunction malloc
applicationcode
sbrksystem call
kernel
userprocess
Memory Hierarchy
Users want to: 1. Increase their address space, using slow cheaper memory to extend their more expensive faster memory.
2. Increase the speed at which they can the extended memory by using small amounts of expensive fast memory.
22
Memory Hierarchy
Registers
Cache
Main Memory
Magnetic Disk (Cache)
Backups Optical Juke Box Remote Access