L11 MMU and VirtualMemory
L11 MMU and VirtualMemory
Unit (MMU)
and
Virtual Memory
Memory Management Unit (MMU)
• A Computer system can be Uni-programming or
Multiprogramming depending upon whether it allows only
one or multiple Application programs to the executed
concurrently.
• In a Uni-programming system - main memory divided into two
parts:
• One for the Operating System (OS) programs
• The other for the Application Program currently being executed
• In a Multiprogramming system – main memory is divided into
two parts:
• One for the OS
• The other for application programs
• The part for application programs is subdivided to accommodate
the multiple programs under execution or the application
processes.
• The task of subdivision of memory to accommodate multiple
programs is carried out dynamically by the OS and is managed
with support from the MMU.
Swapping
▪ There may be many application programs to be executed – OS tries to
accommodate these in the main memory
• However, it may not be possible to accommodate all the programs in the
memory
• Programs that are not accommodated, wait for their turn in a queue
▪ When one program completes, the memory area it occupied is released and
a program in the queue is loaded in that space.
▪ When a program initiates an I/O operation it
is required to wait for the operation to
complete before it becomes ready for
executing further.
– it goes into dormant state – the waiting
time can be substantial
• Such a program may be temporarily moved
out of the main memory to the swap space in
the online secondary storage, to
accommodate some other program in the
queue
• This is called swapping
Logical Address/ Physical Address
• A swapped program is moved back, when some memory space
becomes available, to continue processing
• The program may not be moved back to the same area in memory
that it had occupied earlier
• Thus, the addresses for instructions and data of a program may
change every time it is swapped back-and-forth
• This calls for relocation of the program every time after a swap and
that can be pretty costly, in terms of time consumed
• The idea of Logical address is used to solve this problem
• The physical address is the actual location of a data/ instruction
in main memory
• A logical address is the location if a data/ instruction that is
relative to the beginning of the program.
• The processor generates only the logical addresses of the instruction
or the data.
• MMU converts the logical address to physical address by adding
the current starting physical location of the program (base
address) to the logical address.
Logical Address/ Physical Address
Main Memory
Processor
Relocation
Register
Logical Address (715)
Base Address
50000
+ 50000
Application
Physical Address (50715) Program
OR
Questions??
Logical Address to Physical Address
Effect of Variable
Sized Partitioning
Dynamic Allocation of Memory Partitions
▪ How to satisfy a request of a given size from a list
of free holes.
• First-fit
• allocate the first hole that is big enough
• Best-fit
• Allocate the smallest hole that is big enough; must search
entire list, unless ordered by size.
• Produces the smallest leftover hole.
• Worst-fit
• Allocate the largest hole; must also search entire list.
• Produces the largest leftover hole.
• First-fit and best-fit are better than worst-fit in terms
of speed and storage utilization.
Fragmentation
▪ Free memory space is scattered in a fragmented manner in
large number of Holes
• External fragmentation
• Total memory space exists to satisfy a request, but it is not
contiguous.
• Internal fragmentation
• Allocated memory partition is larger than the requested space
• Excess area in the partition remains unused.
• Reduce external fragmentation by compaction
• Shuffle memory contents to place all free memory together in
one large block
• Compaction is done at execution time
• Possible only if relocation is dynamic.
• I/O problem - (1) latch job in memory while it is in I/O
(2) Do I/O only into OS buffers.
Questions?
Non-contiguous Memory Allocation
– The Paging Approach
• Both unequal fixed-size and variable-size partitions are
inefficient in the use of memory
• Paging is the approach where –
• Memory is partitioned into relatively small, equal fixed-size chunks
– called frames;
• Each program is also divided into chunks – called pages;
• Size of the pages are kept same as that for a frame (and it is a
power of 2);
• A page of a program can be loaded into any of the frames in
memory;
• OS maintins a page table for each program under execution with
one entry for each page;
• These Page Table entries hold the frame numbers of the Frames in
the main memory holding the corresponding pages.
Paging – Address Translation
• OS maintins one page table per process with one entry per page
of the program.
• A page table entry holds the frame number of the frame to which
the page loaded
• The Logical address
consists of the page Logical address
number and the
relative address of the
location within the
page (offset)
• The Physical address
consists of a frame
number and relative
address (offset)
• It is the job of the
MMU to translate the
Logical Address
provided by the CPU
to Physical Address
Paging – Address Translation
Logical/ Virtual Address
Page Number Word Offset
3 100
Page Table
0
1 Concatenation
2 Operation
3 27
4
…
P-1
Note: 27 100
Length of Logical/ Virtual Frame Number Word Offset
Address need not be same
as that of Physical Address.
Physical Address
Example:
26-bit Address for 64MB Logical/ Virtual Memory Space
16-bit Page Number 10-bit Word Offset
0000000000000011 0000000100
Page Table
0
1 Concatenation
2 Operation
3 000000011011
4
…
P-1
000000011011 0000000100
12-bit Frame Number 10-bit Word Offset
22-bit Address for 4MB Physical Memory Space
Paging for Multiple Processes
▪ Every process has its own page table
▪ For every process there is-
• A page-table base register (PTBR) points to the page table.
• A Page-table length register (PTLR) that indicates the size (no. of
entries) of the page table.
▪ No external fragmentation
• All frames (physical memory) can be used by processes
▪ Internal fragmentation is possible
• On average 1/2 page per process (the last page)
▪ Physical memory space used by a process need not be
contiguous
▪ Logical memory space of a process is contiguous
Free Frames
Frame Number V M AC
▪ Hierarchical Paging
▪ In a n-level hierarchical table the page number field of logical address will
be divided into n sub-fields:
▪ Each level is a separate table in memory
▪ Converting a logical address to a physical one may require more memory
accesses.
▪ Caching (TLAB) can help performance remain reasonable.
• Consider 3-level page with memory access time of 100ns, cache look-up
time of 5ns and TLAB hit rate is 95%.
• Memory access with TLAB miss will require 4 memory references.
• Effective Access time = 0.95 x 105 + .05 x 405 = 120 ns
• This is only a 20% slowdown in memory access
Questions?
Hashed Page Tables
▪ Objective is to Reduce Page Table Size and the Access Overhead
▪ Maintains a single Page Table common to all the processes
▪ Common to have address space with address length > 32 bits
▪ The logical page number is
hashed into a page table
▪ Multiple page numbers
may hash to the same
location in the table.
• A chain will be
maintained for these
page numbers that hash
to the same location
• Logical page numbers are
compared in this chain
searching for a match
• If a match is found, the
corresponding physical
frame is extracted
Questions?
Inverted Page Table
▪ One entry for each page
frame in physical memory
• Entry consists of logical
address of page in physical
memory with information
about process that owns the
page. (page# + process_id)
• Decreases memory needed to
store page table
• Increases time to search table
when a page reference occurs
• Table sorted by physical
address, lookup by virtual
address
• Hashing may be used to limit
search to one (maybe few)
page table entries.
Questions?
Segmentation
• A program consists of the procedures and the data Program
structures. These we may call the objects, the collection Code Segment-1
of which constitute the program. Code Segment-2
• In the Segmentation method the program is partitioned
into Segments, where each segment consists of one or
Code Segment-3
more of the objects in the program.
Code Segment-4
• The segments may vary in length depending upon the
size of the object(s) in it. There will however be an upper Data Segment-1
and a lower limit on the length of the segments.
• One or more segments of a program under execution
Data Segment-2
may be loaded into the main memory. The space
occupied in memory by a program segment constitutes a
memory segment.
• Access rights (e. g. Read Only, Read and Write, shared
etc.) may be specified for each of the segments
individually to ensure proper protection of the objects in
it in a multi-user environment.
Logical view of segmentation
Program
Code Segment-1
Code Segment-2
C1
Code Segment-3
C2
Code Segment-4
D1
C1
C4
Data Segment-1 C4
C3 C2
Data Segment-2 D1 C3
D2 D2
3 500
Segment
Descriptor Table
Start Segment A
Address Length
L A<L? Yes
0
1 No
2 Abort
3
4
13500 1000
+
…
S-1 14000
Physical Address
Shared Segments
• Disadvantages:
• As the segment lengths vary, leads to memory
Fragmentation;
• A entire program segment needs to be loaded into the
physical memory when reference to any of its locations is
made
• Leads to low degree of multiprogramming.
Segmentation Vs Paging
Paging Approach Segmentation Approach
Partitions the program space into Partitions the program space in to
equal sized pages, irrespective the segments containing one or more
content. objects such as, procedures, data
structures.
Since the pages are not object The segments are object specific.
specific, no object specific access Therefore object specific access
rights, sharing or protection rights, sharing or protection
mechanisms can be implemented. mechanisms can be implemented.
External fragmentation does not External fragmentation leads to
occur. poor utilization of memory.
Page size is a power of 2. Therefore Segment size is variable and not a
the Physical address generation power of 2. Therefore the Physical
requires Concatenation of 2 bit address generation requires
strings, not Arithmetic Summing – Arithmetic Summing – Costlier.
Less Costly.
Combining Segmentation with Paging
▪ Partition the Program into segments and partition these
segments into equal sized pages;
▪ Partition the Physical Memory into Frames that are equal to
the size of the Pages;
▪ Any page of a segment may be loaded into any of the frames
in the physical memory;
▪ All the pages of a program segment may not be loaded into
the physical memory together;
▪ Only the demanded pages of a segment, as per locality of
reference, may only be loaded;
▪ Similarly all segments of a program also may not be loaded
into the main memory – can be loaded on demand;
▪ Combines the strengths of both Paging and Segmentation.
Segmentation with Paging
Logical Address
Segment Register Program Counter
(Segment Number) Page Number Word Offset
3 50 432
Segment
Descriptor Table P
Segment Start Page Table
Length Address
P<L? Y Frame Number
0 L
1 N
…
100
P
2 Abor
3 55 13500 t + 150
345
4
…
S-1
345 432
Physical Address
Questions?
Demand Paging/ Segmentation
Demand Paging – TLB
• A large amount of physical memory is required for the page
tables
• To read a word from memory –
• Translate logical address to physical address using page table
• Page number + offset -> frame address + offset
• Every memory reference requires two physical memory
accesses-
1. To fetch the appropriate page table entry to obtain the frame
address for computing the physical address
2. To fetch the desired data from the physical address
• To overcome this – use a special cache for page table entries
– called translation lookaside buffer (TLB)
• Similar to memory cache
• Contains page table entries that have been recently used
• Maintain it in the MMU
Demand Paging – TLB with Cache Virtual memory mechanism is required
to interact with MM cache as follows:
1. CPU requests for some logical address
(page number + offset)
2. MMU consults the TLB to see if
requested page table entry is present
Paging/ Segmentation Policies
Policies need to be adopted to minimize overhead due to page faults
and maximize utilization through high degree of multiprogramming
▪ Fetch Policy
• When should a page or segment be brought into primary memory from
secondary (disk) storage?
• Demand Fetch
• Anticipatory Fetch
▪ Placement Policy
• When a page or segment is brought into memory, where is it to be put?
• Paging – trivial
• Segmentation – significant problem
• Paged Segments – trivial
▪ Replacement Policy
• Which page/segment should be replaced when there is not enough room for a
required page/segment?
▪ Allocation Policy
• How many page frames/ physical memory space should be allocated to
process?
Questions?
Page Replacement
▪ When a new page is to be brought in and if no free page frame
is available, an existing page needs to be swapped out to make
way for loading of the new page in the freed frame.
• There is need for selection of the page in memory to be swapped out.
• A page swapped out may need to be swapped in when a reference
occurs to the page later. This will cause a page fault and the
associated overhead.
• Hence the page to be selected for replacement needs suitable policy
to minimize the number of page faults
▪ A swapped-out page needs to be copied back to the virtual
memory space (online storage image) of the process only if the
page is modified (the Modified/ Dirty bit is set).
Need For Page Replacement
Basic Page Replacement
▪ Find the location of the desired
page on disk
▪ Find a free frame:
• If there is a free frame, use it
• If there is no free frame, use
a page replacement
algorithm to select a victim
frame
▪ Bring the desired page into the
(newly) free frame; update the
page and frame tables
▪ Resume execution of the
process
Page Replacement Policies
▪ The Principle of Optimality
• Replace the page that will not be used again in the farthest time
into the future.
▪ Random Page Replacement
• Choose a page randomly
▪ FIFO - First in First Out
• Replace the page that has been in memory the longest.
▪ LRU - Least Recently Used
• Replace the page that has not been used for the longest time.
▪ LFU - Least Frequently Used
• Replace the page that is used least often.
▪ NUR - Not Used Recently
• An approximation to LRU
Frame 1 1 4 5 1,2,3,4,1,2,5,1,2,3,4,5
x=3, i.e. 3 frames Frame 2 2 1 3
9 Page faults
Frame 3 3 2 4
Frame 1 1 5 4 1,2,3,4,1,2,5,1,2,3,4,5
x=4, i.e. 4 frames Frame 2 2 1 5
10 Page faults
Frame 3 3 2
Frame 4 4 3
Belady’s Anomaly -- More frames does not mean less page faults
Optimal Algorithm
▪ Replace page that will
An Example:
not be used for longest
Allocated frames: 4
period of time.
• Requires knowledge of Reference String: 1,2,3,4,1,2,5,1,2,3,4,5
future reference string
Ref Page# 1 2 3 4 1 2 5 1 2 3 4 5
• Hence not feasible in
practice. Replace - - - - - - 4 - - - 1 -
Frame 1 1 1 1 1 5
4 frames Frame 2 2 2 2 2 2 8 Page faults
Frame 3 3 5 5 4 4
Frame 4 4 4 3 3 3
Implementation of LRU algorithm
▪ Counter Implementation
• Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter.
• When a page needs to be replaced, look at the counters to
determine which page to change (page with smallest time value).
▪ Stack Implementation
• Keeps a stack of page number in a doubly linked form
• Page referenced
• move it (most referenced page) to the top
• required 6 pointers to be changed
• No search required for replacement
Use of a Stack to Record the Most
Recent Page References
LRU Approximation Algorithms
▪ Reference Bit
• With each page, associate a bit, initially = 0.
• When page is referenced, bit is set to 1.
• Replace any one of the pages for which reference bit is 0 (if any
exists).
▪ Additional Reference Bits Algorithm
• Record reference bits at regular intervals.
• Maintain an unsigned number (say, 8 bits) in a table for each page in
memory.
• Periodically, shift the reference bit into high-order bit, i.e. shift other
bits to the right, dropping the lowest bit.
• During page replacement, consider the page with the lowest number
as the LRU page.
LRU Approximation Algorithms
▪ Second Chance
• FIFO (clock) replacement algorithm
• Need a reference bit.
• When a page is selected, inspect the
reference bit.
• If the reference bit = 0, replace
the page.
• If page to be replaced (in clock
order) has reference bit = 1, then
• Set reference bit to 0
• Leave page in memory
• Replace next page (in clock
order) subject to same rules.
LRU Approximation Algorithms
▪ Enhanced Second Chance
• Need a reference bit and a modified bit as an ordered pair.
(ref bit, modified bit)
• 4 situations are possible:
• (0,0) - Neither recently used nor modified – best page to replace.
• (0,1) - Not recently used, but modified – not quite as good, because
the page will need to be written out before replacement.
• (1,0) - Recently used but clean – likely be used again soon.
• (1,1) - Probably will be used again, will need to write out before
replacement.
• Used in the Macintosh virtual memory management scheme
Counting Algorithms
▪ Keep a counter of the number of references
that has been made to each page.
• LFU (least frequently used) algorithm
• Replaces page with smallest count.
• Rationale : frequently used page should have a large
reference count.
• Variation - shift bits right, exponentially decaying count.
• MFU (most frequently used) algorithm
• Replaces page with highest count.
• Based on the argument that the page with the smallest count
was probably just brought in and has yet to be used.
Page Buffering Algorithm
▪ Keep pool of free frames
• Solution 1
• When a page fault occurs, choose victim frame.
• Desired page is read into free frame from pool before victim is
written out.
• Allows process to restart soon, victim is later written out and
added the frame to the free frame pool.
• Solution 2
• Maintain a list of modified pages. When paging device is idle,
write modified pages to disk and clear modify bit.
• Solution 3
• Keep frame contents in pool of free frames and remember which
page was in frame. If desired page is in free frame pool, no need
to page in.
Control Bits
Page Protection
Segmentation Protection