UNIT-4 - Memory Management
UNIT-4 - Memory Management
5. Segmentation
Address Binding
1. Compile Time Binding
Logical address:
➔ An address generated by the CPU during program execution is commonly
➔ The set of all logical addresses generated by a program is a logical address space.
➔ The process accesses memory using logical addresses, which are translated into
➔ The swap time is 2000 milliseconds. Since we must swap both out and in, the total swap
time is about 4,000 milliseconds.
Contiguous Memory Allocation
● Contiguous Memory Allocation is a type of memory allocation technique where
processes are allotted a continuous block of space in memory.
● This block can be of fixed size for all the processes in a fixed size partition scheme or
can be of variable size depending on the requirements of the process in a variable size
partition scheme.
● The main memory must accommodate both the operating system and the various user
processes.We therefore need to allocate main memory in the most efficient way possible.
● The Main memory is usually divided into two partitions:
○ one for the operating system
○ one for the user processes.
We can place the operating system in either low memory or high memory.
Memory Protection
★ When the CPU scheduler selects a process for execution, the dispatcher loads the relocation
and limit registers with the correct values.
★ Because every address generated by the CPU is checked against these registers, we can
protect both the operating system and the other users' programs and data.
★ In Memory protection, we have to protect the operating system from user processes and which
can be done by using a relocation register with a limit register.
★ The relocation register has the value of the smallest physical address whereas the limit
register has the range of the logical addresses.
★ These two registers have some conditions like each logical address must be less than the limit
register.
★ The memory management unit(MMU) is used to translate the logical address with the value in
the relocation register dynamically after which the translated (or mapped) address is then sent
to memory.
● In the above diagram, when the scheduler selects a process for the execution process, the dispatcher,
on the other hand, is responsible for loading the relocation and limit registers with the correct values as
part of the context switch as every address generated by the CPU is checked against these 2 registers,
and we may protect the operating system, programs, and the data of the users from being altered by
this running process.
Memory Allocation
Fixed(Static)Partition:
Example :
Process P1(2MB) and process P3(1MB) completed their
execution. Hence two spaces are left i.e. 2MB and 1MB. Let’s
suppose process P5 of size 3MB comes. The empty space in
memory cannot be allocated as the memory offered is in a
non-contiguous manner.
● In the First Fit memory allocation strategy, the operating system searches for the first
available memory block that is large enough to accommodate a process.
● It starts searching from the beginning of the memory and allocates the first block that
meets the size requirement.
● It may result in significant fragmentation, both internal and external
Best Fit:
● In the Best Fit memory allocation strategy, the operating system searches the entire
memory space and allocates the smallest available block that is large enough to
accommodate the process.
● It aims to minimize internal fragmentation.
Worst Fit:
● In the Worst Fit memory allocation strategy, the operating system searches the entire
memory space and allocates the largest available block to the process.
● It aims to maximize external fragmentation.
● It leads to more internal fragmentation, as smaller processes may be allocated in larger
blocks, leaving unused memory within the allocated blocks.
Paging
● Paging is a memory-management scheme that permits the physical address space of a process
to be noncontiguous.
● Paging is a memory management scheme that eliminates the need for contiguous allocation of
physical memory.
● The process of retrieving processes in the form of pages from the secondary storage into the
main memory is known as paging
● The basic method for paging involves
○ Breaking physical memory into fixed-sized blocks called - - - > Frames
○ Breaking logical memory into blocks of the same size called - - - > Pages
○ When a process is to be executed, its pages are loaded into any available memory frames from
their source (a file system or the backing store).
● The hardware support for paging is illustrated in Figure( Previous Slide).
● Every address generated by the CPU (Logical Address) is divided into two parts
● The page table contains the base address of each page in physical memory.
● This base address is combined with the page offset to define the physical memory
Page 1 Frame 1
Page 2 Frame 2
Page 3 Frame 3
Frame 4
Frame 5
Frame 6
Paging example for a 32-byte memory
with 4-byte pages
Frame 7
Finding Physical Address for the corresponding Logical address
Formula:
Physical Address = In which frame the page is available * Page size + Offset
➔ Logical address 0 is page 0, offset 0. Indexing into the page table, we find that page 0 is
in frame 5.
➔ Thus, logical address 0 maps to physical address 20 [= (5 × 4) + 0].
➔ Logical address 3 (page 0, offset 3) maps to physical address 23 [= (5 × 4) + 3].
➔ Logical address 4 is page 1, offset 0; according to the page table, page 1 is mapped to
frame 6.
➔ Thus, logical address 4 maps to physical address 24 [= (6 × 4) + 0].
➔ Logical address 13 maps to physical address 9.
Translation Look-a Side Buffer (TLB)
Or
Paging Hardware with TLB
● The implementation of page table or where we have to store page table either in
○ Registers or
○ Main Memory(RAM)
● The page table is implemented as a set of dedicated registers. These registers should be
built with very high-speed logic to make the paging-address translation efficient.
● The use of registers for the page table is satisfactory if the page table is reasonably
small (for example, 256 entries).
● Most contemporary computers, however, allow the page table to be very large (for
example, 1 million entries).the use of fast registers to implement the page table is not
feasible.
Main Memory
Some of the common techniques that are used for structuring the Page table are as follows:
1. Hierarchical Paging
2. Hashed Page Tables
3. Inverted Page Tables
Hierarchical Paging
● Hierarchical paging or Multilevel paging is a type of paging where the logical
● The entries of the level 1 page table are pointers to a level 2 page table and entries of
the level 2 page tables are pointers to a level 3 page table and so on.
● The entries of the last level page table store actual frame information.
● The advantage of using hierarchical paging is that it allows the operating system to
● By dividing the page table into multiple levels, the operating system can minimize the
● address translation works from the outer page table inward, this scheme is also known
as a forward-mapped page table.
Hashed Page Tables
★ A common approach for handling address spaces larger than 32 bits is to use a hashed
page table, with the hash value being the virtual page number.
★ Each entry in the hash table contains a linked list of elements that hash to the same
○ The virtual page number in the virtual address is hashed into the hash table.
○ The virtual page number is compared with field 1 in the first element in the
linked list.
○ If there is no match, subsequent entries in the linked list are searched for a
★ In segmentation, secondary memory and main memory are divided into partitions of
unequal size.
Step 1:
● CPU always generates a logical address, It consisting of two parts-
○ Segment Number
○ Segment Offset
● Segment Number specifies the specific segment of the process from which CPU
wants to read the data.
● Segment Offset specifies the specific word in the segment that CPU wants to read.
Step 2:
● For the generated segment number, corresponding entry is located in the segment
table.
● Then, segment offset is compared with the limit (size) of the segment.
● If segment offset is found to be greater than or equal to the limit , a trap is generated.
● If segment offset is found to be smaller than the limit, then request is treated as a
valid request.
● Each entry in the segment table has a segment base and a segment limit.
○ The segment base contains the starting physical address where the segment resides
in memory,
○ The segment limit specifies the length of the segment.
Eg:
➔ We have five segments numbered from 0 through 4. The segments are stored in physical
memory as shown(Next Slide).
➔ The segment table has a separate entry for each segment, giving the beginning address of
the segment in physical memory (or base) and the length of that segment (or limit).
➔ For example, segment 2 is 400 bytes long and begins at location 4300.
➔ Thus, a reference to byte 53 of segment 2 is mapped onto location 4300 + 53 = 4353.
➔ A reference to segment 3, byte 852, is mapped to 3200 (the base of segment 3) + 852 =
4052. A reference to byte 1222 of segment 0 would result in a trap to the operating system,
as this segment is only 1,000 bytes long.