Title: Operating Systems
1Operating Systems
2Memory Management
- List of Topics
- 1. Memory Management
- 2. Memory In Systems Design
- 3. Binding Times
- 4. Introduction to Memory Management
- 5. Raw Memory Model
- 6. Single User Contiguous Memory
- 7. Relocation - Why and How
- 8. Overlay Management
3- Topics (continued)
- 9. Protection
- 10. Fixed Partitions
- 11. Nonuniform Sized Fixed Partitions
- 12. Uniformly Sized Fixed Partitions
- 13. Simple Paging
- 14. Benefits of Simple Paging
- 15. Page Tables
- 16. Translation Lookaside Buffers
- 17. Hierarchical Address Caching
4- Topics (continued)
- 18. Dynamic Partitions
- 19. Fragmentation
- 20. Internal Fragmentation
- 21. External Fragmentation
- 22. Coalescing Holes
- 23. Compaction
- 24. Dynamic Partition Placement
- 25. Simple Segmentation
- 26. Memory Layout of A C Program
5- Topics (continued)
- 27. malloc
- 28. sbrk
- 29. Memory Hierarchy
6Memory In Systems design
I/O System
CPU
memory
Figure 1 memory Connects CPU and Peripherals
7Binding Times
source program
compiler or assembler
compile time
object model
8linkage editor
Other object modules
load module
load time
system library
loader
9dynamically loaded system library
in-memory binary memory image
execution time (run time)
10Introduction to Memory Management
- Memory management issues
- 1. Relocation
- 2. Protection
- 3. Sharing
- 4. Logical Organization
- 5. Physical Organization
- Consider a series of solutions starting
- with the most primitive first
11Raw Memory Model
- The raw memory provides no services
- and gives the programmer complete
- control.
0
Figure 3 Raw Machine Model
1 MB
12Single 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.
13Single User Contiguous Memory(continued)
0
Monitor
fence register
User
Figure 4 Single User Contiguous Memory
32K
14Relocation - Why and How
- Relocation refers to the ability to store a
- program at an arbitrary base memory
- address.
- Actual memory locations have physical
- or absolute addresses, user program's
- access these locations using logical
- addresses.
15Relocation - Why and How (continued)
Base register
14000
Logical address 346
Physical address 14346
CPU
memory
Figure 5 Address Translation
16Overlay Management
- Overlays have gone out of fashion with
- cheaper memory, users (and compilers)
- determine which code to swap in and out.
17Figure 6 Overlay Management Example 2
symbol table
20K
common routines
30K
overlay driver
10K
70K
pass 1
pass 2
80K
18Protection
- It is undesirable to permit user programs
- (accidentally or intentionally) to accesses
- memory outside of their partition.
19Protection (continued)
0
Monitor
fence register
Figure 7 Protection in Resident Monitor Model
User
32K
20Fixed Partitions
- Fixed partitioning refers to memory being
- split into contiguous non overlapping
- regions of precomputed sizes.
- Fixed sized partitions make the selection
- of a partition for a job easy.
21Nonuniform Sized Fixed Partitions
- Fixed Partitions may have differing sizes.
22Operating system
Figure 8 Nonuniform fixed Partitions 3
New Process
23Uniformly Sized Fixed Partitions
- Memory is frequently partitioned into
- uniformly sized regions.
24Figure 9 Uniform Fixed Partition Allocation 3
Operating system 512 K
512 K
512 K
512 K
512 K
512 K
512 K
512 K
25Simple Paging
- Paging provides relocation, and splits
- memory into fixed length partitions
- called frames.
260 p-1
Page frame 0
p 2p-1
Page frame 1
Figure 10 Simple Paging 1
2p 3p-1
Page frame 2
3p 4p-1
Page frame 3
274p 5p-1
Page frame 4
5p 6p-1
Page frame 5
Figure 10 Simple Paging (continued) 1
6p 7p-1
Page frame 6
7p 8p-1
Page frame 7
28Page frame size p p p p p p p p
Range of real storage addresses 0
p-1 p 2p-1 2p 3p-1 3p 4p-1 4p 5p-1 5p
6p-1 6p 7p-1 7p 8p-1
Page frame number 0 1 2 3 4 5 6 7
29Benefits of Simple Paging
- Simple paging allows discontiguous
- storage for memory objects exceeding
- the page frame size.
30. . .
Contiguous virtual storage locations
. . .
Address mapping mechanism
Real storage
Virtual memory
31Page 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.
32Page Tables (continued)
b
b
Page
Displacement
p
Virtual address
d
p
v(p,d)
Page
bp
Map Table
b
p
Real
p1
d
address
p1
33Translation Lookaside Buffers
- Translation lookaside buffers (TLB)
- eliminate one physical memory reference
- using special associative memory, which
- addressed by its contents in O(1) parallel
- search time.
34Associative Memory Lookup
Page
Displacement
Virtual
d
address
p
V(p,d)
Associative map
Real
Address r
p1
d
p
p1
Frame
Displacement
35Hierarchical 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.
36Hierarchical Address Caching (continued)
Page table
Virtual address v(p,d)
origin register
Displacement
Page
p
Address of
d
Page table b
Try this first
Partial associative
Map (most active pages only
p
p1
p
Only if no match in associative map
p1
Only if match in associative map
Performed only if no match in associative map bp
Displacement
Frame
d
p1
Real address
Direct map (all pages)
37Dynamic Partitions
- Dynamically partitioned memory allows
- placement of relocatable code in variable
- size contigous memory regions.
38Dynamic Partitions (continued)
user
needs
I
9K.
H
18K.
G
11K.
Operating
Operating
Operating
Operating
F
32K.
system
system
system
system
E
14K.
User
User
User
User
D
25K.
A 15K
A 15K
A 15K
A 15K
User
User
User
C
10K.
B 20K
B 20K
B 20K
B
20K.
User
User
Free
C 10K
C 10K
A
15K.
User
Free
D 25K
Free
Free
39Fragmentation
- Fragmentation makes available memory
- useless by breaking it into discontiguous
- pieces too small to use.
40Fragmentation (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.
41Internal Fragmentation
- Internal fragmentation occurs when fixed
- size partitions are too large.
42Logical Address Page1,Offset478
Page 2
000001 01110111110
478
Page 1
Figure 16 Internal Fragmentation 3
Internal fragmentation
Page 0
43External Fragmentation
- External fragmentation happens when
- dynamic partitions are released. The
- fragments are frequently called holes.
44operating system
operating system
operating system
User A
User A
User A
User B finishes and frees its storage.
User B
Hole
Hole
User C
User C
User C
User D finishes and frees its storage.
User D
User D
Hole
User E
User E
User E
Hole
Hole
Hole
45Coalescing Holes
- Adjacent holes in dynamic partitions
- should be coalesced into a single larger
- hole.
46operating system
operating system
operating system
other users
other users
other users
Operating system combines adjacent holes
to form a single larger hole.
2K Hole
2K Hole
7K Hole
User A finishes and frees its storage
5K Hole
5K user A
other users
other users
other users
47Compaction
- If the amount of memory available in the
- holes is large enough to service a
- request, the holes may made contiguous
- by compacting storage.
48operating systems
operating systems
In use
In use
Operating system places all in use blocks
together leaving free storage as a single,
large hole.
In use
Free
In use
In use
Free
Free
In use
Free
49Dynamic Partition Placement
(a) First-Fit Strategy Place job in first
storage hole on free storage place list in which
it will fit
500
Operating systems
(kept in storage address order, or something in
random order.)
a
16K hole
b
In use
c
Free Storage List
14K hole
Request for 13K
Start Address
d
In use
Length
e
5K hole
a 16K c 14K e 5K g 30K
f
In use
g
. . .
30K hole
h
51(b) Best-Fit Strategy Place job in the smallest
possible hole in which it will fit.
520
Operating systems
(Kept in ascending order by hole size.)
a
16K hole
b
In use
c
Free Storage List
14K hole
Request for 13K
d
Start Address
In use
Length
e
5K hole
e 5K c 14K a 16K g 30K
f
In use
g
. . .
30K hole
h
53(c) Worst-Fit Strategy Place job in the largest
possible hole in which it will fit.
540
Operating systems
(Kept in descending order by hole size.)
a
16K hole
b
In use
c
Free Storage List
14K hole
Request for 13K
d
Start Address
In use
Length
e
5K hole
g 30K a 16K c 14K e 5K
f
In use
g
. . .
30K hole
h
55Simple Segmentation
- Segmentation provides relocation, and
- supports contiguous variable length
- partitions.
- Segmentation often provides
- protection (counterexample Intel 8086).
56limit
base
memory
Logical address
CPU
yes
lt
no
Trap address error
57Memory Layout of A C Programs
- Traditional Unix/C memory images of
- programs use segments.
58high address
Command-line arguments and environment
variables
stack
heap
initialized to zero by exec
uninitialized data (bss)
Initialized data
read from program file by exec
low address
text
59malloc
- 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).
60In Use List
user data
user data
user data
Free List
user data
Key
user data
Memory management Information
user data
61sbrk
- A user program can exhaust its default
- heap space allocation.
- The Unix sbrk system call increases data
- segment allocation at run time.
62application code
user process
Memory allocation function malloc
sbrk system call
kernel
63Memory 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.
64Memory Hierarchy
Registers
Cache
Main Memory
Magnetic Disk (Cache)
Backups Optical Juke Box Remote Access