CS4310 MemoryManagement
CS4310 MemoryManagement
CS4310 MemoryManagement
Objectives
• To provide a detailed description of various ways of organizing
memory hardware
• To discuss various memory-management techniques, including
paging and segmentation
• To provide a detailed description of the Intel Pentium, which
supports both pure segmentation and segmentation with paging
Background
• Program must be brought (from disk) into memory and placed within a process
for
it to be run
• Main memory and registers are only storage CPU can access directly
• Memory unit only sees a stream of addresses + read requests, or address + data
and write requests
• Register access in one CPU clock (or less)
• Main memory can take many cycles, causing a stall
• Cache sits between main memory and CPU registers
• Protection of memory required to ensure correct operation
Base and Limit Registers
• A pair of base and limit registers define the logical address space
• CPU must check every memory access generated in user mode to be sure it is between base
and limit for that user
Binding of Instructions and Data to Memory
• Address binding of instructions and data to memory addresses
can happen at three different stages
✔ Compile time: If memory location known a priori,
absolute code can be generated; must recompile code if
starting location changes
✔ Load time: Must generate relocatable code if memory
location is not known at compile time
✔ Execution time: Binding delayed until run time if
the process can be moved during its execution from
one memory segment to another
✔ Need hardware support for address maps (e.g., base
and limit registers)
Logical vs. Physical Address Space
• The concept of a logical address space that is bound to a separate physical
address space is central to proper memory management
✔ Logical address – generated by the CPU; also referred to as virtual address
✔ Physical address – address seen by the memory unit
• Logical and physical addresses are the same in compile-time and load-time
address-binding schemes; logical (virtual) and physical addresses differ in
execution-time address-binding scheme
• Logical address space is the set of all logical addresses generated by a program
• Physical address space is the set of all physical addresses generated by a
program
• The user program deals with logical addresses; it never sees the real physical addresses
✔ Execution-time binding occurs when reference is made to location in memory
✔ Logical address bound to physical addresses
Dynamic relocation using a relocation register
• Memory Management Unit (MMU) is a hardware
device that at run time maps virtual to
physical address
• Routine is not loaded until it is called
• Better memory-space utilization; unused routine is
never loaded
• All routines kept on disk in relocatable load format
• Useful when large amounts of code are needed to
handle infrequently occurring cases
• No special support from the operating system is
required
• Implemented through program design
• OS can help by providing libraries to implement dynamic
loading
Dynamic Linking
• Static linking – system libraries and program code combined by the loader
into the binary program image
• Dynamic linking –linking postponed until execution time
• Small piece of code, stub, used to locate the appropriate memory-
resident library routine
• Stub replaces itself with the address of the routine, and executes the
routine
Swapping
• A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution
✔ Total physical memory space of processes can exceed physical memory
• First-fit: Allocate the first hole that is big enough. Searching can start either at the beginning of the
set of holes or at the location where the previous first-fit search ended. We can stop searching as
soon as we find a free hole that is large enough.
• Best-fit: Allocate the smallest hole that is big enough. We must search the entire list, unless
the list is ordered by size. This strategy produces the smallest leftover hole.
• Worst-fit: Allocate the largest hole. Again, we must search the entire list, unless it is sorted by size.
This strategy produces the largest leftover hole, which may be more useful than the smaller leftover
hole from a best-fit approach.
• Simulations have shown that both first fit and best fit are better than worst fit in terms of
decreasing time and storage utilization. Neither first fit nor best fit is clearly better than the other in
terms of storage utilization, but first fit is generally faster
In Class Practice #2
Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and 125 KB (in order), how
would the first-fit, best-fit, and worst-fit algorithms place processes of size 115 KB, 500 KB, 358 KB, 200
KB, and 375 KB (in order)?
Fragmentation
• External Fragmentation – total memory space exists to satisfy a request, but it is not
contiguous
• Reduce external fragmentation by compaction
✔ Shuffle memory contents to place all free memory together in one large block
✔ Compaction is possible only if relocation is dynamic, and is done at execution time
• Another possible solution to the external-fragmentation problem is to permit the logical
address space of the processes to be noncontiguous, thus allowing a process to be
allocated physical memory wherever such memory is available.
• Two complementary techniques achieve this solution : segmentation and paging.
Segmentation
• Segment table – maps two-dimensional physical addresses; each table entry has:
• base – contains the starting physical address where the segments reside in memory
• limit – specifies the length of the segment
• Segment-table base register (STBR) points to the segment table’s location in memory
Example:
Consider hit ratio = 80%, memory access time : 100 ns ,
Effective Access Time (EAT)
• EAT = TLB hit time* hit_ratio + TLB miss time* (1- hit_ratio)
• EAT = 0.80 x 100 + 0.20 x 200 = 120ns
• Consider more realistic hit ratio = 99%, 100ns for memory access
• EAT = 0.99 x 100 + 0.01 x 200 = 101ns
Hierarchical Page Tables
• Break up the logical address space into multiple page tables
• A simple technique is a two-level page table
• We then page the page table
Hierarchical Page Tables (Cont’d)
Address Translation
Scheme
• Since the page table is paged, the page number is further divided into:
✔ a 12-bit page number
✔ a 10-bit page offset
• where p1 is an index into the outer page table, and p2 is the displacement within the page
of the inner page table
Inverted Page Table
• An inverted page table has one entry for each real page (or frame) of memory.
• Each entry consists of the virtual address of the page stored in that real
memory location, with information about the process that owns the page.
<process-id, page-number, offset>
2 1650 1024
3 355 115
4 2048 650
#3
A 1200 KB memory is managed using variable partitions but no compaction. It currently has three
partitions of sizes 158 KB , 215 KB and 127KB respectively. What is the smallest allocation request in KB
that could be denied?
#4
Consider a logical address space of 2,048 pages with a 4-KB page size,
mapped onto a physical memory of 512 frames.
a. 3085
b. 42095
c. 650000
#6
• Consider a paging system with the page table stored in memory. a. If a memory
reference takes 50 nanoseconds:
b) If we add TLBs, and if 75 percent of all page-table references are found in the
TLBs, what is the effective memory reference time? (Assume that finding a page-
table entry in the TLB stakes 2 nanoseconds, if the entry is present.)
#7
Considering following memory, what is the minimum data movement required to perform compaction?