(OS'25) Lecture 2
(OS'25) Lecture 2
Chapter 7
Memory Management
Dave Bremer
Otago Polytechnic, N.Z.
©2009, Prentice Hall
*Lecture slides are modified by Dr. Ahmed Salah*
GENERAL NOTE
Hidden Slides are Extra Knowledge
Not Included in Exams
Roadmap
KNOW HOW the Operating System do its Main Jobs?
Shared
Memory CPU Resourc
e
MMU
Agenda
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic PART I
– Buddy System
– Paging
– Segmentation PART II
• Questions
Learning Outcomes
• Berkeley CS162
– L13: Address Translation (Paging & Segmentation)
Memory Management
Mod#4
3. Sharing Any Loc.
5. Physical organisation
Mod#2 RAM
Yellow prog.
Can’t access Mod#1
Red one
Requirements: Relocation
• The programmer does not know where the program will
be placed in memory when it is executed,
– It may be swapped to disk and return to main memory at a
different location (relocated)
• Memory references must be translated to the actual
physical memory address
Requirements: Protection
• Processes should not be able to reference memory
locations in another process without permission
• Impossible to check absolute addresses at compile time
• Must be checked at run time Virtual Mem
Process X
Yellow prog.
Can’t access
Red one
Requirements: Sharing
• Allow several processes to access the same portion of
memory
• Better to allow each process access to the same
copy of the program rather than have their own
separate copy
Process X Process Y
SHARING
Requirements: Logical Organization
• Memory is organized linearly (usually)
• Programs are written in modules
– Modules can be written and compiled Virtual Mem
Process X
independently
Mod#5
• Different degrees of protection given to
Mod#4 Shared
modules (read-only, execute-only)
• Share modules among processes Mod#3
Mod#1
Requirements: Physical Organization
• Cannot leave the programmer with the
responsibility to manage memory
• Memory available for a program plus its data
may be insufficient
– Overlaying allows various modules to be assigned
the same region of memory but is time consuming
to program
• Programmer does not know how much space
will be available
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation
• Questions
Partitioning
• An early method of managing memory
– Not used much now
• Considered as basic for virtual memory
Types of Partitioning
1. Fixed Partitioning
2. Dynamic Partitioning
3. Buddy System CH.7
4. Simple Paging
5. Simple Segmentation
6. Virtual Memory Paging
CH.8
7. Virtual Memory Segmentation
Fixed Partitioning
• Equal-size partitions
– Any process whose size is less than or
equal to the partition size can be loaded
into an available partition
• OS can swap a process out of a partition
– If none are in a ready or running state
Example
Request 4
2
7 MB: Process A
B
C A
Internal Fragmentations
WHAT are the problems? B
(Not used – Can’t be Used)
C
Process > Partition!
Request 12 MB: Process D 7 MB
12 MB
5 MB
Can’t be loaded!!
Programmer should split it
into 2 parts (Overlaying)
ONLY one part loaded at a time
Fixed Partitioning
• Advantages:
1. Simple to implement
2. Little OS overheads
• Disadvantages:
1. A program may not fit in a partition.
• Solution: overlays
2. Main memory use is inefficient.
• Internal fragmentation: Any program, no matter how small, occupies an
entire partition.
Fixed Partitioning
• Disadvantages (cont.)
3. The number of active processes is fixed (limited by number of
partitions)
4. A large number of very small process will not use the space
efficiently
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation
• Questions
Dynamic Partitioning
Best Fit
.
.
Process B
.
2
Process C
1
.
. 0
.
Address Translation
Dynamic Partition: What’s the required info to be stored in table?
Virtual/Logical RAM
Process A
Process B
Process C
.
.
.
Address Translation
Address from process
Start Address
OS Role?
Limit
MMU Role?
Address Translation (H/W)
1. absolute address = relative + base
2. If absolute address < bounds
– Access physical memory
3. Else
– Generate interrupt to OS
Address Translation (OS)
• Base register
– Starting address for the process
• Bounds register
– Ending location of the process
• These values are set by OS when the process is loaded
or when the process is swapped in
Roadmap
• Basic Requirements
• Partitioning Techniques
– Fixed
– Dynamic
– Buddy System
– Paging
– Segmentation Address Translation
• Questions
Paging
Paging: What’s the required info to be stored in table?
Logical of Process A RAM
Page#M-1 N-1
. N-2
.
. A
.
Process
.
Page#1 .
Page#0 2
PAGES 1
FRAMES
Paging
• Extension of fixed partitioning
A.0
A.1
A.2
A.3
D.0
B.0
D.1
B.1
D.2
B.2
C.0
C.1
C.2
C.3
D.3
D.4
Paging
• Advantages:
– No external fragmentation
– Reduce internal fragmentation
• Disadvantages
– Side-effect on the modular programming
Address Translation (Ex)
Logical of Process A Logical Address RAM
P# (3 bits) Offset (11 bits)
Page#7 2 KB 15
Process A: Page Table
. 2 KB 14
. Page# Frame #
. 0 14 .
.
1 2
Page#1 .
.
Page#0 .
. 2 KB 2
PAGES 7 0
2 KB 1
Logical Address Physical Address
2 KB 0
0 KB 28 KB
MemoryPAGE#
address
= consist
2 KB 4 KB
1. page number FRAMES
3 KB OFFSET
2. offset =
within the page 5 KB
15 KB ?? KB
Address Translation
Address Translation (Ex.)
Given:
• Page Size = 1 KB
• Logical Address = 1502 (0000,0101,1101,1110)2
• Page table of Process A:
Page # 0 1 2
Frame # 5 6 17
Required
• Calculate corresponding physical address?
Address Translation (Ex.)