4.1 Memory Management
4.1 Memory Management
4.1 Memory Management
Reading:
Silberschatz
chapter 9
Reading:
Stallings
chapter 7
EEL 602 1
Outline
¾ Background
¾ Issues in Memory Management
¾ Logical Vs Physical address, MMU
¾ Dynamic Loading
¾ Memory Partitioning
Placement Algorithms
Dynamic Partitioning
¾ Buddy System
¾ Paging
¾ Memory Segmentation
¾ Example – Intel Pentium
EEL 602 2
Background
¾ Main memory → fast, relatively high cost, volatile
EEL 602 3
Issues in Memory Management
¾ Relocation: Swapping of active process in and out of
main memory to maximize CPU utilization
Process may not be placed back in same main memory region!
Ability to relocate the process to different area of memory
EEL 602 5
Binding of Instructions and Data to Memory
Address binding of instructions and data to memory addresses can
happen at three different stages
EEL 602 6
Logical Vs Physical Address Space
¾ Each logical address is bound to physical
address space;
Logical address – generated by the CPU; also
referred to as virtual address
Physical address – address seen by the memory unit
EEL 602 7
Memory-Management Unit (MMU)
¾ The runtime mapping from virtual → physical address
EEL 602 9
Memory Partitioning
Two schemes – used in several variations of now-obsolete OS
¾ Fixed Partitioning: OS occupies fixed portion of main memory, rest available for
multiple processes. Two alternatives;
Equal size fixed partitions → any process ≤ partition size can be loaded
Unequal size partitions → several unequal size partitions, process of matching sizes
EEL 602 10
Unequal-Size Partitions
Assign each processes the smallest partition to which it will fit
¾ Advantages:
Process are always assigned in such a way as to minimize
wasted memory within a partition → internal fragmentation
Relatively simple and require minimal OS software and
overhead
¾ Disadvantages:
Limitations on the active number of processes, number of
partitions specified at system generation time
Small jobs cannot utilize partition space efficiently; In most
cases it is an inefficient technique
EEL 602 11
Placement Algorithm with Partitions
¾ Equal-size partitions
Because all partitions are of equal size, it does not
matter which partition is used
¾ Unequal-size partitions
Can assign each process to the smallest partition
within which it will fit
Queue for each partition size
Processes are assigned in such a way as to minimize
wasted memory within a partition
EEL 602 12
Placement Algorithm with Partitions
EEL 602 13
Dynamic Partitioning
Developed to address the drawbacks of fixed partitioning
¾ Leaves Holes
First at the end → eventually lot of small holes
Memory becomes more fragmented with time, memory utilization ↓
¾ External Fragmentation
Memory that is external to all partitions becomes increasingly fragmented
¾ Compaction
Used to overcome external fragmentation
OS shifts processes so that free memory is together in one block
Compaction requires use of dynamic relocation capability
Time consuming procedure and wasteful of processor time
EEL 602 14
Dynamic Partitioning
EEL 602 15
Placement Algorithms
Compaction is time consuming → OS must be clever in plugging holes
while assigning processes to memory
EEL 602 17
Placement Algorithms
¾ Which of the above approaches is the best?
Process Size/Sequence, General Comments
First-Fit → Simplest, usually the best and fastest
EEL 602 18
Buddy System
¾ Drawbacks
Fixed partitioning: Limits number of active process, inefficient
if poor match between partition and process sizes
Dynamic Partitioning: Complex to maintain, includes the
overhead of compaction
¾ Compromise may be the Buddy System - Entire space
available is treated as a single block of 2U
¾ If a request of size s such that 2U-1 < s ≤ 2U, entire block
is allocated
Otherwise block is split into two equal buddies
Process continues until smallest block greater than or equal to s
is generated
EEL 602 19
Buddy System - Example
Initial block size 1 MB; First request A is for 100 KB
EEL 602 20
Buddy System - Example
Binary tree representation immediately after Release B request.
EEL 602 21
Relocation
¾ A process may occupy different partitions which means different
absolute memory locations during execution (from swapping)
EEL 602 22
Paging
¾ Partitioning main memory → small equal fixed-size chunks
Each process is divided into the same size chunks → pages
Chunks of memory → frames or page frames
¾ Advantages
No external fragmentation
Internal fragmentation → only a fraction of last page of a process
EEL 602 23
Paging - Example
Assignment of process pages to free frames
EEL 602 24
Paging - Example
Assignment of process pages to free frames.
EEL 602 25
Paging - Example
Data structures for page tables at time epoch (f)
EEL 602 26
Paging - Example
¾ Convenience in Paging scheme
Frame size → power of 2
Relative address (wrt origin of program) and the logical address
(page # and offset) are same
Example - 16 bit address, page size → 1K or 1024 bytes
Maximum 64 (26) pages of 1K bytes each
¾ Advantages
Logical addressing → transparent to programmer, assembler, linker
Relatively easy to implement a function to perform dynamic
address translation at run time
EEL 602 27
Paging - Example
EEL 602 28
Paging - Example
Logical-to-physical address translation in Paging
EEL 602 29
Paging - Example
Logical-to-physical address translation in Paging
EEL 602 30
Implementation of Page Table
¾ Different methods of storing page tables, OS dependent
¾ Pointer to page table → PCB
¾ Hardware implementation of page tables
Page table → Set of dedicated high speed registers, Simplest
Suitable for small page table sizes, Usually very large
requirements
¾ Page table is kept in main memory
Page-table base register (PTBR) points to the page table
Two memory access, page table and other for data/instruction
Memory access slowed by a factor of two
¾ Solution to the two memory access problem
Usage of a special fast-lookup hardware cache called
associative memory or translation look-aside buffers (TLBs)
TLB contains Page # → Frame #, Small # of TLB entries (64-
1024)
EEL 602 31
Paging Hardware With TLB
EEL 602 32
Shared Pages
¾ Shared code
One copy of read-only (reentrant) code shared
among processes, e.g. text editors, compilers
Shared code must appear in same location in the
logical address space of all processes
EEL 602 33
Shared Pages Example
EEL 602 34
Segmentation
¾ Memory-management scheme that supports user view of
memory
¾ Program → Collection of segments (name and length)
¾ Complier automatically constructs segments reflecting
input program
¾ Example – A C complier might create separate
segments for the following
main program,
procedure,
function,
object,
local variables, global variables,
common block,
stack,
symbol table, arrays
EEL 602 35
Segmentation
¾ The program/process and its associated data is divided
into a number of segments
EEL 602 37
User’s View of a Program
EEL 602 38
Logical View of Segmentation
1
4
1
3 2
4
EEL 602 39
Example of Segmentation
EEL 602 40
Sharing of Segments
EEL 602 41
Segmentation
¾ Compared to dynamic partition, segmentation program may occupy
more than one partition and these partitions need not be contiguous
EEL 602 42
Segmentation
EXAMPLE: Logical Addresses.
EEL 602 43
Segmentation
EXAMPLE:
Logical-to-physical address translation in Segmentation
EEL 602 44
Hierarchical Page Tables
¾ Most systems support a large logical address space
232 – 264 , page table itself becomes excessively large
¾ Break up the logical address space into multiple page
tables
EEL 602 45
Two-Level Paging Example
¾ A logical address (32-bit machine with 4K page size) is divided into:
a page number consisting of 20 bits
a page offset consisting of 12 bits
¾ Since the page table is paged, page number is further divided into:
a 10-bit page number
a 10-bit page offset
¾ Thus, a logical address is as follows:
10 10 12
EEL 602 46
Two-Level Page-Table Scheme
EEL 602 47
Address-Translation Scheme
¾ Address-translation scheme for a two-level 32-bit
paging architecture
EEL 602 48