You are on page 1of 48

S.

Thulasi Bharathi 1
Department of Computer Science,
St. Joseph’s College (Autonomous)
Trichy-2

UNIT 2 - Memory Management Unit


OUTLINES
▪ Memory-Management Unit
▪ Single Contiguous Allocation
▪ Partitioned Memory management
▪ Fragmentation

UNIT 2 - Memory Management Unit 2


WHAT DOES IT MEAN BY MMU?
▪ Store the data.
▪ Keep track of memory locations whether free or allocated.
▪ Move information between primary memory and secondary
memory called swapping.
▪ Be responsible for protecting the memory allocation.
▪ Sharing of memory spaces.

UNIT 2 - Memory Management Unit 3


UNIT 2 - Memory Management Unit 4
MULTISTEP PROCESSING OF A USER PROGRAM
▪ Compiler and Assembler generate an
object file (containing code and data
segments) from each source file
▪ Linker combines all the object files for a
program into a single executable object
file,
which is complete and self-sufficient
▪ Loader (part of OS) loads an executable
object file into memory at locations
determined by the operating system

UNIT 2 - Memory Management Unit 5


FUNCTIONS OF MMU
• Keep track of status of each location of primary memory (allocated /unallocated).
• Determining the allocation policy for memory
• Allocation technique – once it is decided to allocate memory, the specific location
must be selected and allocation information updated.
• Deallocation technique – a process may explicitly release previously allocated
memory or reclaim the memory based on the policy.

UNIT 2 - Memory Management Unit 6


UNIT 2 - Memory Management Unit 7
LOGICAL VS. PHYSICAL ADDRESS SPACE
▪ Physical address - the “hardware” address of a
memory word (0xb0000)
▪ Address seen by the memory unit

▪ Logical address (virtual address, relative address)


▪ Used by the program
▪ Generated by the CPU

▪ Absolute code - all addresses are physical


▪ Relocatable code - all addresses are relative

UNIT 2 - Memory Management Unit 8


DIFFERENCES BETWEEN ADDRESSES

UNIT 2 - Memory Management Unit 9


▪ The Address Binding refers to the mapping of computer
instructions and data to physical memory locations.
▪ Both logical and physical addresses are used in computer memory.
▪ It assigns a physical memory region to a logical pointer by
mapping a physical address to a logical address known as a virtual
address.
▪ It is also a component of computer memory management that the
OS performs on behalf of applications that require memory access.
▪ Compile Time Address Binding
▪ Load Time Address Binding
▪ Execution Time or Dynamic Address Binding

UNIT 2 - Memory Management Unit 10


▪ Compile-time Address Binding :
▪ If the compiler is responsible for performing address binding then it is
called compile-time address binding.
▪ It will be done before loading the program into memory.
▪ The compiler requires interacts with an OS memory manager to perform
compile-time address binding.
▪ Load time Address Binding :
▪ It will be done after loading the program into memory.
▪ This type of address binding will be done by the OS memory manager i.e
loader.
▪ Execution time or dynamic Address Binding :
▪ It will be postponed even after loading the program into memory.
▪ The program will be kept on changing the locations in memory until the
time of program execution.
▪ The dynamic type of address binding done by the processor at the time of
program execution.

UNIT 2 - Memory Management Unit 11


DYNAMIC LOADING
▪ Routine is not loaded until it is called
▪ Better memory-space utilization; unused routine is
never loaded
▪ 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

UNIT 2 - Memory Management Unit 12


OVERLAYS
▪ Keep in memory only those instructions and data
that are needed at any given time
▪ Dynamic linking
▪ Dynamic loading

UNIT 2 - Memory Management Unit 13


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
▪ Operating system needed to check if routine is in
processes’ memory address.
▪ Dynamic linking is particularly useful for libraries

UNIT 2 - Memory Management Unit 14


UNIT 2 - Memory Management Unit 15
SWAPPING
• A process needs to be in memory to be executed.
• A process, however, can be swapped temporarily out of memory to a backing store, and
then brought back into memory for continued execution.
• A process that is swapped out will be swapped back into the same memory space that it
occupied previously.
• The system maintains a ready queue consisting of all processes whose memory images
are on the backing store or in memory and are ready to run
• Whenever the CPU scheduler decides to execute a process, it calls the dispatcher.
• The dispatcher checks to see whether the next process in the queue is in memory. If not,
and there is no free memory region, the dispatcher swaps out a process currently in
memory and swaps in the desired process.
• It then reloads registers as normal and transfers control to the selected process.

UNIT 2 - Memory Management Unit 16


FUNCTIONS OF MEMORY MANAGEMENT
• Keep track of status of each partition (in use /not in use).
• Determining who gets the memory – handled by job schedular
• Allocation technique an available partition of sufficient size is assigned.
• Deallocation technique – when the job terminates, the partition is indicated “ not in
use” and is available for future allocation.

UNIT 2 - Memory Management Unit 17


•The concurrent residency of more than
one program in the main memory is
referred as multiprogramming."
•Since multiple programs are resident in
the memory, as soon as the currently
executing program finishes its execution,
the next program is dispatched for its
consumption.
•The main objective of multiprogramming
is:
• Maximum CPU utilization.
• Efficient management of the main
memory.

UNIT 2 - Memory Management Unit 18


▪ A contiguous memory allocation is a memory management technique where
whenever there is a request by the user process for the memory, a single section of
the contiguous memory block is given to that process according to its requirement.
❑ Fixed sized partition scheme
❑ Variable sized partition scheme

▪ A process is allocated into memory as one complete unit


▪ Multiple-partition allocation
▪ Hole – block of available memory
▪ holes of various size are scattered throughout memory
▪ When a process arrives, it is allocated memory from a hole large enough to
accommodate it
▪ Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
UNIT 2 - Memory Management Unit 19
CONTIGUOUS ALLOCATION

UNIT 2 - Memory Management Unit 20


SINGLE CONTIGUOUS ALLOCATION
▪ It is a simple memory management scheme that requires no special hardware
features.
▪ It is associated with stand alone computers with simple Batch Operating systems.
▪ One to one correspondence between the system and the user.
▪ Four functions of memory management:
• Keep track of memory
• The job gets all memory when scheduled
• Allocation of memory
• Deallocation of memory

UNIT 2 - Memory Management Unit 21


PARTITIONED ALLOCATION
▪ In Partition Allocation, when there is more than one partition freely available to
accommodate a process’s request, a partition must be selected.
▪ To choose a particular partition, a partition allocation method is needed.
▪ When it is time to load a process into the main memory and if there is more than
one free block of memory of sufficient size then the OS decides which free block to
allocate.

UNIT 2 - Memory Management Unit 22


CONTIGUOUS ALLOCATION
▪ How to satisfy a request of size n from a list of free
holes?
▪ Dynamic Storage-Allocation methods
▪ 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 search entire list, unless ordered by size
▪ Produces the largest leftover hole
▪ First-fit and best-fit better than worst-fit in terms of speed
and storage utilization

UNIT 2 - Memory Management Unit 23


FIRST FIT ALGORITHM
• This method keeps the free/busy list of jobs organized by
memory location, low-ordered to high-ordered memory.
• In this method, first job claims the first available memory
with space more than or equal to it’s size.
• The operating system doesn’t search for appropriate
partition but just allocate the job to the nearest memory
partition available with sufficient size.

UNIT 2 - Memory Management Unit 24


FIRST FIT ALGORITHM
▪ Memory partition: 150kb,220kb,500kb,350kb,700kb
▪ Process : p1 -200kb, p2-160kb, p3 - 450kb, p4-500kb

150 220 500 350 700


200 160 450
P1 P2 P3

▪ P4 – 500kb should wait for the memory

UNIT 2 - Memory Management Unit 25


▪ Advantages of First-Fit Memory Allocation:
It is fast in processing.
▪ As the processor allocates the nearest available memory partition to the job, it is
very fast in execution.
▪ Disadvantages of First-Fit Memory Allocation :
It wastes a lot of memory.
▪ The processor ignores if the size of partition allocated to the job is very large as
compared to the size of job or not. It just allocates the memory.
▪ As a result, a lot of memory is wasted and many jobs may not get space in the
memory, and would have to wait for another job to complete.

UNIT 2 - Memory Management Unit 26


BEST FIT ALGORITHM
▪ 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.

▪ memory partition: 150kb,220kb,500kb,350kb,700kb

▪ Process : p1 -200kb, p2-160kb, p3 - 450kb, p4-500kb

▪ All the process are allocated to this scenario.

UNIT 2 - Memory Management Unit 27


BEST FIT ALGORITHM
▪ memory partition: 150kb,220kb,500kb,350kb,700kb
▪ Process : p1 -200kb, p2-160kb, p3 - 450kb, p4-500kb

150 220 500 350 700


200 450 160 500

▪ All the process are allocated to this scenario.

UNIT 2 - Memory Management Unit 28


▪ Advantages of Best-Fit Allocation :
Memory Efficient.
▪ The operating system allocates the job minimum possible space in the memory,
making memory management very efficient.
▪ To save memory from getting wasted, it is the best method.
▪ Disadvantages of Best-Fit Allocation :
It is a Slow Process.
▪ Checking the whole memory for each job makes the working of the operating
system very slow.
▪ It takes a lot of time to complete the work.

UNIT 2 - Memory Management Unit 29


▪ In Worst Fit Algorithm,
▪ Algorithm first scans all the partitions.
▪ It then allocates the partition of largest size to the process.

UNIT 2 - Memory Management Unit 30


FRAGMENTATION
▪ Fragmentation
▪ As processes are loaded and removed from memory, the free
memory space is broken into little pieces.
▪ It happens after sometimes that processes cannot be
allocated to memory blocks considering their small size and
memory blocks remains unused. This problem is known as
Fragmentation.

UNIT 2 - Memory Management Unit 31


External fragmentation
Total memory space is enough to satisfy a request or to reside a process in it, but it is not contiguous, so it
cannot be used.
BOTH FIRST FIT AND BEST FIT
One solution to the problem of external fragmentation is COMPACTION.
The goal is to shuffle the memory contents so as to place all free memory together in one large block.
(Costly)
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.
SEGMENTATION
PAGING

Internal fragmentation
Memory block assigned to process is bigger. Some portion of memory is left unused, as it cannot be used
by another process.

UNIT 2 - Memory Management Unit 32


SEGMENTATION
▪ Segmentation is a memory-management scheme that supports this
programmer view of memory.
▪ A logical address space is a collection of segments.(CPU)
▪ For simplicity of implementation, segments are numbered and are referred to by a
segment number, rather than by a segment name. Thus, a logical address consists of a
two tuple:
<segment-number, offset>.

UNIT 2 - Memory Management Unit 33


▪ In a web browser, each open tab or web page is treated as a separate process, and it is
divided into different segments, each serving a specific purpose. Here are some
common segments that a web browser might use for each web page:
▪ Code Segment: This segment contains the executable code of the web page, including
HTML, JavaScript, and CSS files. It is marked as read-only to prevent accidental
modifications.
▪ Data Segment: The data segment holds the dynamic data of the web page, such as
variables, user input, and dynamically generated content. This segment is marked
as read-write, allowing the web page to modify its data during execution.
▪ Stack Segment: The stack segment is used to store function call information and
local variables. It grows and shrinks dynamically as functions are called and return.
▪ Heap Segment: The heap segment is used for dynamic memory allocation during
runtime. It is managed by the browser's memory management system and allows the
web page to allocate and deallocate memory as needed.

UNIT 2 - Memory Management Unit 34


SEGMENTATION ARCHITECTURE
▪ 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
▪ Since segments vary in length, memory allocation is
a dynamic storage-allocation problem

UNIT 2 - Memory Management Unit 35


LOGICAL VIEW OF SEGMENTATION

UNIT 2 - Memory Management Unit 36


EXAMPLE OF SEGMENTATION

UNIT 2 - Memory Management Unit 37


▪ Segmentation:
▪ Divides main memory into variable-sized segments.
▪ Processes are divided into variable-sized segments, each representing a specific
unit (e.g., code, data, stack).
▪ Uses segment registers for address translation.
▪ The Address Translation Unit is responsible for performing the address translation process.
It takes the logical address (segment number + offset) and uses the segment register,
segment table register, and segment limit register to determine the corresponding physical
address.
▪ May lead to external fragmentation.
▪ Supports memory sharing among processes.

UNIT 2 - Memory Management Unit 38


▪ A computer can address more memory than the amount physically installed on the
system.
▪ This extra memory is actually called virtual memory and it is a section of a
hardware that's set up to match the computer's RAM.
▪ Paging technique plays an important role in implementing virtual memory.
▪ Paging is a storage mechanism that allows OS to retrieve processes from the
secondary storage into the main memory in the form of pages.
▪ Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages.
▪ The size of the process is measured in the number of pages.

UNIT 2 - Memory Management Unit 39


UNIT 2 - Memory Management Unit 40
▪ The Memory Management Unit (MMU) is responsible for converting logical
addresses to physical addresses. The physical address refers to the actual address
of a frame in which each page will be stored, whereas the logical address refers to
the address that is generated by the CPU for each page.
▪ When the CPU accesses a page using its logical address, the OS must first collect
the physical address in order to access that page physically. There are two
elements to the logical address:
▪ Page number
▪ Offset
▪ The OS’s memory management unit must convert the page numbers to the frame
numbers.

UNIT 2 - Memory Management Unit 41


Let’s consider an example:
If the physical address is 12 bits, then the physical address space would be 4 K words
If the logical address is 13 bits, then the logical address space would be 8 K words
If the page size is equal to the frame size, which is equal to 1 K words (assumption),
Then:

UNIT 2 - Memory Management Unit 42


▪ The address generated by the CPU (Logical Address) is divided into the
following:
▪ Page offset(d): It refers to the number of bits necessary to represent a certain word
on a page, page size in Logical Address Space, or page word number or page
offset.
▪ Page number(p): It is the number of bits needed to represent the pages in the
Logical Address Space or the page number.
▪ The Physical Address is divided into the following:
▪ Frame offset(d): It refers to the number of bits necessary to represent a certain
word in a frame, or the Physical Address Space frame size, the word number of a
frame, or the frame offset.
▪ Frame number(f): It’s the number of bits needed to indicate a frame of the
Physical Address Space or a frame number.
UNIT 2 - Memory Management Unit 43
▪ The Memory Management Unit (MMU) is responsible for converting logical
addresses to physical addresses. The physical address refers to the actual address
of a frame in which each page will be stored, whereas the logical address refers to
the address that is generated by the CPU for each page.
▪ When the CPU accesses a page using its logical address, the OS must first collect
the physical address in order to access that page physically. There are two
elements to the logical address:
▪ Page number
▪ Offset
▪ The OS’s memory management unit must convert the page numbers to the frame
numbers.

UNIT 2 - Memory Management Unit 44


▪ Paging is a memory management technique where a large process is divided into
pages and is placed in physical memory which is also divided into frames.
▪ Frame and page size is equivalent.
▪ The operating system uses a page table to map the logical address of the page
generated by CPU to its physical address in the main memory.
▪ Structure of page table
▪ Hierarchical Page Table
▪ Hashed Page Table
▪ Inverted Page Table

UNIT 2 - Memory Management Unit 45


▪ Hierarchical Paging is multilevel paging.
▪ There might be a case where the page table is too big to fit in a contiguous
space, so we may have a hierarchy with several levels.
▪ In this type of Paging the logical address space is broke up into Multiple page
tables.
▪ Hierarchical Paging is one of the simplest techniques and for this purpose,
▪ a two-level page table and three-level page table can be used.

UNIT 2 - Memory Management Unit 46


▪ Consider a system having 32-bit logical address space and a page size of 1 KB and it is further
divided into:
▪ Page Number consisting of 22 bits.
▪ Page Offset consisting of 10 bits.
▪ As we page the Page table, the page number is further divided into :
▪ Page Number consisting of 12 bits. ❖ P1 is an index into the Outer Page table.
▪ Page Offset consisting of 10 bits. ❖ P2 indicates the displacement within the page of
the Inner page Table.
LOGICAL ADDRESS ❖ As address translation works from outer page
table inward so is known as forward-mapped Page
Table.
❖ Below given figure below shows the Address
Translation scheme for a two-level page table

UNIT 2 - Memory Management Unit 47


▪ Three Level Page Table
▪ For a system with 64-bit logical address space, a two-level paging scheme is not
appropriate. Let us suppose that the page size, in this case, is 4KB.If in this case, we
will use the two-page level scheme then the addresses will look like this:

Thus in order to avoid such a large table, there is a solution and


that is to divide the outer page table, and then it will result in
a Three-level page table:

UNIT 2 - Memory Management Unit 48

You might also like