Topic MemoryManagement
Topic MemoryManagement
Topic MemoryManagement
Tongping Liu
0xFFFF
Device drivers
Operating system
User program (ROM)
(ROM)
(RAM)
User program
User program (RAM)
Operating system (RAM)
Operating system
(RAM)
(RAM)
0
CPU 0.9
0.8
– More processes will 0.7
increase CPU utilization
CPU Utilization
0.6
CPU 0.3
0.2
– At 80% I/O wait, even 0.1
10 processes aren’t 0
enough 0 1 2 3 4 5 6 7 8 9 10
Degree of Multiprogramming
• More processes => 80% I/O Wait 50% I/O Wait 20% I/O Wait
memory management
University of Massachusetts Amherst 8
Purpose of Memory Management
• Protection:
– Each program cannot access others’ memory, only accesses
locations that have been allocated to it.
• Sharing:
– Allow several processes to access the same memory
• Equal-size partitions
– Any process whose size is less than or
equal to the partition size can be loaded
into an available partition
• The operating system can swap a
process out of a partition
• Best-fit algorithm
– Chooses the block that is closest in size to the request
– Worst performer overall
– Since smallest block is found for process, the smallest
amount of fragmentation is left
– Memory compaction must be done more often
• First-fit algorithm
– Scans memory form the beginning and chooses the first
available block that is large enough
– Fastest
– May have many process loaded in the front end of
memory that must be searched over when trying to find
a free block
• Next-fit
– Scans memory from the location of the last placement
– More often allocate a block of memory at the end of
memory where the largest block is found
– The largest block of memory is broken up into smaller
blocks
– Compaction is required to obtain a large block at the
end of memory
P1 data
P2 code
print
function
P2 data
P1 code
Translator Physical
User process
Virtual (MMU) Physical memory
address address
University of Massachusetts Amherst 28
Why Virtual Memory?
2N
• Basic idea: allow OS to allocate
more memory than the real
Auxiliary
regions
Translator Physical
User process
Virtual (MMU) Physical memory
address address
Department of Computer Science @ UTSA 31
University of Massachusetts Amherst 31
Simple MMU: Relocation Register
• In segmentation, relocation register maps logical
(virtual) address to physical address, where user
program deals with logical addresses, but not
real physical addresses
segment 2
yes
<? + segment 3
Physical
no Address
raise
segment 4
protection fault
0010000000100000
+ 0000001011110000
----------------------------
0010001100010000
Segment #
…
(1,4ff)
Offset …
2fff
. (Data) . (Stack segment)
(1,000) 2000
… …
(0,6ff) 4ff
. (Code) . (Data segment)
(0,0) 0
• Pros
– Multiple areas of address space can grow separately
– Easy to share parts of address space (can share code
segment)
• Cons
– Complex memory allocation
– (still have external fragmentation)
E
C C C
B B B B
A A A
D
OS OS OS OS OS
E
C C C What should we do,
B B B B reject it or accept it?
A A A
D
OS OS OS OS OS
Paging System
• Virtual address 60–64K
56–60K
-
-
– Divided into pages 52–56K
48–52K
-
-
• Physical memory 44–48K
40–44K
5
1
– Divided into frames 36–40K
32–36K
-
-
28–32K 3 7 28–32K
24–28K - 6 24–28K
• Page vs. Frame 20–24K
16–20K
-
0
5
4
20–24K
16–20K
- 3 12–16K
– Same size block 12–16K
8–12K - 2 8–12K
1 4–8K
– Unit of mapping/allocation 4–8K
0–4K
4
7 0 0–4K
56
University of Massachusetts Amherst 56
Page Table for Mapping
p d
m-n n
0010000000100000
+ 0000001011110000
----------------------------
0010001100010000
• Internal fragmentation
– Average only ½ page (half of the last one)
• External fragmentation
– Completely non-existent since we never carve up pages
• # of virtual pages:
2^12/128= 2^5 = 32 Page #:5bits Offset: 7 bits
• # of physical frames
1K/128=8 Frame #:3bits Offset: 7 bits