Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
60 views49 pages

Memory Management Essentials

Uploaded by

diyashah2977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views49 pages

Memory Management Essentials

Uploaded by

diyashah2977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

5.

Memory Management
5.1.Basic Of Memory Management
1. Memory management in operating system is important because of it directly
affects the execution time of a process.
2. The execution time of a process depends on the availability of the data in the main
memory.
3. Therefore an operating system must perform the memory management in such a
manner that the essential data is always present in the main memory.
4. An effective memory management system ensures accuracy availability and
consistency of the data imported from the secondary memory to the main memory
5.1.1.Overview Of Memory
1. Memory in a computer system is used to store data and instruction and it is the storage
space in the computer, where data is to be proceed and instructions required for processing are
stored.
2. The operating system must co-reside in main memory with one or more executing processes in
a multiprogramming system, in order to share a processor, a number of processes must be kept in
memory.
3. Memory is a Central to the operations of a modern computer system memory consists of a
large array of words or byte each with its own address.
Fig:Central Nature Of Memory in a Computer System.

CPU Memory I/O system


• 4.The CPU fetches instructions from memory according to the value of the program counter.
This instructions may cause additional loading from and storing to specific memory
addresses.
• 5. Both the CPU and I/O system interact with memory interaction is achieved through a
sequence of reads or writes to specific memory addresses. The CPU fetches from and stores
in memory.
• 6. Addresses in a source program are generally symbolic.A compiler will typically bind this
symbol addreses to re-locatable addresses.
• 7. The linkage editor or loader will bind this re-locatable addresses to absolute addresses.
Each binding is a mapping from one address space to another. These absolute addresses
loaded into memory to be executed.
• 8. As a program execute it accesses program instructions and data from memory by
generating this absolute addresses. Eventually the program terminates, its memory space is a
declared available and next program may be loaded and executed.
5.1.2.Functions Of Memory Management
1.Process Isolation:
Process isolation means controlling of one process interacts with
data and memory of other process.
2.Tracking Memory locations:
Memory management keeps track of each and every memory the
location regardless Of either it is allocated to some process or it
is free. it checks how much memory is to be allocated to process.
3.Automatic allocation and Management:
Memory should be allocated dynamically based on the priorities
of the process .otherwise the process waiting will increase and it
decrease the CPU utilization and the memory utilization.
4.Long Term Storage:
Long term storage of process will reduce the memory utilization.
5.Support of modular Programming:
A program is divided into number of modules if the
memory is not sufficient for the entire program we can
load at least some of the modules instead of the entire
program this will increase CPU utilization and memory
utilization.
6.Protection and Access control:
Do not apply the protection mechanism and access
control to the all the process is better to apply to the
important application only it will save the execution time
7.Keeping status of main memory Locations:Memory
management keeps track of the status of each memory
allocation whether it is allocated for free.
Memory Partitioning
• Memory partitioning is the system by which the memory of a computer
system is divided into sections for use by the resident programs. These
memory divisions are known as partitions. There are different ways in
which memory can be partitioned: fixed, variable, and dynamic
partitioning.
• Fixed (or static) Partitioning in Operating System
• 1.Fixed partitioning, also known as static partitioning, is a memory
allocation technique used in operating systems to divide the physical
memory into fixed-size partitions or regions, each assigned to a specific
process or user.
• 2. Each partition is typically allocated at system boot time and remains
dedicated to a specific process until it terminates or releases the partition.
1.In fixed partitioning, the memory is divided into fixed-size chunks, with
each chunk being reserved for a specific process. When a process requests
memory, the operating system assigns it to the appropriate partition. Each
partition is of the same size, and the memory allocation is done at system
boot time.
• 2.Fixed partitioning has several advantages over other memory allocation
techniques. First, it is simple and easy to implement. Second, it is
predictable, meaning the operating system can ensure a minimum amount
of memory for each process. Third, it can prevent processes from
interfering with each other’s memory space, improving the security and
stability of the system.
• 3.However, fixed partitioning also has some disadvantages. It can lead to
internal fragmentation, where memory in a partition remains unused. This
can happen when the process’s memory requirements are smaller than
the partition size, leaving some memory unused. Additionally, fixed
partitioning limits the number of processes that can run concurrently, as
each process requires a dedicated partition.
• Overall, fixed partitioning is a useful memory allocation technique in
situations where the number of processes is fixed, and the memory
requirements for each process are known in advance.
• In operating systems, Memory Management is the function responsible
for allocating and managing a computer’s main memory. Memory
Management function keeps track of the status of each memory location,
either allocated or free to ensure effective and efficient use of Primary
Memory.
• As illustrated in above figure, first process is only consuming 1MB out of 4MB in
the main memory.
Hence, Internal Fragmentation in first block is (4-1) = 3MB.
Sum of Internal Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)=
3+1+1+2 = 7MB.
• Suppose process P5 of size 7MB comes. But this process cannot be accommodated
in spite of available free space because of contiguous allocation (as spanning is not
allowed). Hence, 7MB becomes part of External Fragmentation.
• There are some advantages and disadvantages of fixed partitioning.
• Advantages of Fixed Partitioning –
• 1.Implementation of fixed partition is a simple.
• 2.In fixed partitioning the overhead of process is low.
• Disadvantages Of Fixed Memory Partitioning:
• 1. The main problem with which the partition method is how to determine the
number of partitions and how to determine their sizes.
• 2. fixed the partitioning limits the degree of multiprogramming which in turn may
reduce the effectiveness of short term scheduling.
5.1.3.2.Dynamic (Variable)Memory Partitioning
1.The dynamic partitioning was designed to overcome the problems of a fixed
partitioning scheme.
2. In a dynamic partitioning scheme, each process occupies only as much
memory as they require when loaded for processing.
3. Requested processes are allocated memory until the entire physical
memory is exhausted or the remaining space is insufficient to hold the
requesting process.
4. In this scheme the partitions used are of variable size, and the number of
partitions is not defined at the system generation time.
0 Job Queue
JOB Memory Time
40K
Monitor 1 60K 10
2 100K 5
3 30K 20
216K
256K 4 70K 8
5 50K 15
• Advantages of Dynamic Partitioning memory management schemes:
• Simple to implement.
• Easy to manage and design.
• Disadvantages of Dynamic Partitioning memory management schemes:
• This scheme also suffers from internal fragmentation.
• The number of partitions is specified at the time of system segmentation.
• Dynamic Storage Allocation
• Disk space can be viewed as a large array of disk blocks.at any given time some of
these blocks are allocated to files and others are free.
• Disk space seen as a collection of free and user the segments, each segment is a
continuous set of disk blocks. An unallocated segment is called a hole. The
dynamic storage allocation problem is how to satisfy a request of size ‘n’ from a list
of free holes. There are many solution to this problem.
• The set of holes is searched to determine which hole is the best to allocate. the
most common strategy used to select a free hole from the set of available holes
are first fit, best fit and worst fit.
• 1.First Fit:
• Allocate the first to hole that is a big enough for the new process. searching can start either at
beginning of the set of holes or where the previous first fit search ended. we can stop
searching as soon as we find the larger in a free wall first feet is a generally faster.
• 2.Best Fit:
• Allocate the smallest to hold that is a big enough .we search the entire list unless the list of
kept ordered by size. the strategy produces the smallest the left over a hole.
• 3.Worst Fit:.
• allocate the largest hole .again we must search the entire list unless it is sorted by size. first fit
and best bit are better than worst fit in both time and storage utilization first fit is generally
faster.
Comparision Of static Memory Partition MFT(Multiprogramming with a fixed number of Tasks)and MVT
(Multiprogramming with a variable number of TASKS).
Sr.No Factors MFT MVT

1 Region Size Region size in this partitioning, In this partitioning region or partition
memory is divided into several partitions size is not fixed and very dynamically.
affects the size that size never changes.

2 Process Size Cannot grow at runtime can grow or shrink at runtime

3 degree of multiprogramming fixed that is number of partition dynamic

4 Memory Utilization Poor Good

5 Division Of Memory Partition The division of memory into number of The size and the number of partition are
partitions and its size made in the decided during the runtime by operating
beginning prior to the execution of user system.
programs and remains fixed thereafter

6 Degree of Multiprogramming Limited and Fixed the size and the number of partition are
decided during the runtime by operating
system depending on program size.

7 Implentation Diffcult Easy

8 Fragmentation Suffers from internal fragmentation Sufferes from External Fragmentation

9 Example IBM 360,DOS IBM OS.


5.1.4.Free Space Management Techniques
A file system is responsible to allocate the free blocks to the file therefore it has to
keep track of all the free blocks present in the disk. There are mainly two approaches
by using which, the free blocks in the disk are managed.
5.1.4.1.Bit Vector
Bitmap or Bit vector – A Bitmap or Bit Vector is series or collection of bits
where each bit corresponds to a disk block. The bit can take two values: 0 and
1: 0 indicates that the block is free and 1 indicates allocated block. The given
instance of disk blocks on the disk in Figure 1 (where green blocks are
allocated) can be represented by a bitmap of 16 bits as: 0000111000000110.
• Advantages –
• 1.Simple to understand.
• 2.Finding the first free block is efficient. It requires scanning the words (a group of 8 bits) in a
bitmap for a non-zero word. (A 0-valued word has all bits 0). The first free block is then found
by scanning for the first 1 bit in the non-zero word.
• Disadvantages Of Bit Map/Bit Vector
1.Extra space required to store bit map
2.It may require a special hardware support to do bit operation i.e.finding out which
bit 1 and which bit is 0.
5.1.4.2.Linked List
1.In this approach, the free disk blocks are linked together i.e. a free block contains a
pointer to the next free block.
2. The block number of the very first disk block is stored at a separate location on disk
and is also cached in memory.
3.In Figure-2, the free space list head points to Block 5 which points to Block 6, the
next free block and so on. The last free block would contain a null pointer indicating
the end of free list.
4.A drawback of this method is the I/O required for free space list
traversal.
Advantages Of Linked Free space List
1.If a file is to be allocated a free block the operating system can simply allocate the
first block in the free space list and move the head pointer to the next free block in the
List.
2. No disk fragmentation as every block is utilized.
Disadvantages Of Linked Free Space List
1. It is not very efficient scheme as to traverse the list; we must read each block
requiring substantial time.
2. pointers used in here will also consume disk space.Additional memory is therefor
required.
• 5.1.4.3.Grouping
• The third method of free space management in operating systems is grouping. This
method is the modification of the linked list method. In this method, the first free
block stores the addresses of the n free blocks. The first n-1 of these blocks is free.
The last block in these n free blocks contains the addresses of the next n free
blocks, and so on.
• For example, consider a disk having 16 blocks where block numbers 3, 4, 5, 6, 9,
10, 11, 12, 13, and 14 are free, and the rest of the blocks, i.e., block numbers 1, 2,
7, 8, 15 and 16 are allocated to some files.
• If we apply the Grouping method considering n to be 3, Block 3 will store the
addresses of Block 4, Block 5, and Block 6. Similarly, Block 6 will store the
addresses of Block 9, Block 10, and Block 11. Block 11 will store the addresses of
Block 12, Block 13, and Block 14. This is also represented in the following figure-
• .
• Advantages
• The addresses of a large number of free blocks can be
found quickly.
• This method has the benefit of making it simple to locate
the addresses of a collection of empty disk blocks.
• It's a modification of the free list approach. So, there is no
need to traverse the whole list.
• Disadvantages
• The advantages of the grouping method are-
• The space of one block is wasted in storing addresses. Since
the nth block is used to store the addresses of next n free
blocks.
• We only save the address of the first free block since we are
unable to maintain a list of all n free disk addresses.
• There is an overhead in maintaining the index of blocks.
• 5.1.4.4.Counting
• This is the fourth method of free space management in operating systems.
This method is also a modification of the linked list method. This method
takes advantage of the fact that several contiguous blocks may be
allocated or freed simultaneously. In this method, a linked list is
maintained but in addition to the pointer to the next free block, a count of
free contiguous blocks that follow the first block is also maintained. Thus
each free block in the disk will contain two things-
• A pointer to the next free block.
• The number of free contiguous blocks following it.

For example, consider a disk having 16 blocks where block numbers 3, 4, 5,
6, 9, 10, 11, 12, 13, and 14 are free, and the rest of the blocks, i.e., block
numbers 1, 2, 7, 8, 15 and 16 are allocated to some files.
• If we apply the counting method, Block 3 will point to Block 4 and store
the count 4 (since Block 3, 4, 5, and 6 are contiguous). Similarly, Block 9
will point to Block 10 and keep the count of 6 (since Block 9, 10, 11, 12, 13,
and 14 are contiguous). This is also represented in the following figure-
• This method also overcomes the disadvantages of the linked list method since there is no
need to traverse the whole list.
• Advantages
• The advantages of the counting method are-
• Fast allocation of a large number of consecutive free blocks.
• Random access to the free block is possible.
• The overall list is smaller in size.
• Disadvantages
• The disadvantages of the counting method are-
• Each free block requires more space for keeping the count in the disk.
• For efficient insertion, deletion, and traversal operations. We need to store the entries in B-
tree.
• The entire area is reduced.
5.2.Virtual Memory
1. Virtual memory is a technique which allows the execution of processes that may
not be completely in a memory.
2. Virtual memory is a memory management technique that provides an idealized
abstraction of the storage resources that are actually available on a machine which
create the illusion to users of a very large (main) memory.
3. virtual memory is the separation of user logical memory from physical memory This
Separation allows an extremely large virtual memory to be provided for a
programmers when only a smaller physical memory is available.
4. Virtual memory makes the task programming much easier because the programmer
no longer need to worry about the amount of physical memory available. virtual
memory is commonly implemented by demand paging.
5. It can also be implemented in a segmentation system. it is not easy to implement,
and may decrease performance if it is used the carelessly.
6. The basic idea behind virtual memory is that the combined size of program, data
and stack May exceed the amount of physical memory available for it.
7. The operating system keeps those parts of the program currently in use in main
memory and rest on the disk.
Fig.Virtual Memory that is larger than physical Memory

Page 0

Page 1

Page 2

.
.
.
.
.

Page n
Memory Map Physical Memory
Virtual Memory
• 5.2.1.Introduction to Paging
• 1.In Operating Systems, Paging is a storage mechanism used to retrieve processes from the
secondary storage into the main memory in the form of pages.
• 2.The main idea behind the paging is to divide each process in the form of pages. The main
memory will also be divided in the form of frames.
• 3.One page of the process is to be stored in one of the frames of the memory. The pages can
be stored at the different locations of the memory but the priority is always to find the
contiguous frames or holes.
• 4.Pages of the process are brought into the main memory only when they are required
otherwise they reside in the secondary storage.
• 5.Different operating system defines different frame sizes. The sizes of each frame must be
equal. Considering the fact that the pages are mapped to the frames in Paging, page size
needs to be as same as frame size. Frame number
• FIG:Paging Model of Logical and Physical Memory 0
Page 0
• Page 0 1
• 1 2
Page 2
• Page 1 4 3 Page 1
3 4
Page 2 5
7 6
Page 3 7 Page 3

• 5.2.2.Segmentation
• In Operating Systems, Segmentation is a memory management technique in which
the memory is divided into the variable size parts. Each part is known as a segment
which can be allocated to a process.
• The details about each segment are stored in a table called a segment table.
Segment table is stored in one (or many) of the segments.
• Segment table contains mainly two information about segment:
• Base: It is the base address of the segment
• Limit: It is the length of the segment.
• Advantages of Segmentation
• No internal fragmentation
• Average Segment Size is larger than the actual page size.
• Less overhead
• It is easier to relocate segments than entire address space.
• The segment table is of lesser size as compared to the page table in paging.
• Disadvantages
• It can have external fragmentation.
• it is difficult to allocate contiguous memory to variable sized partition.
• Costly memory management algorithms.

Difference between Paging and Segmentation
• 5.2.3.Fragmentation
• 1.Fragmentation is an unwanted problem in the operating system in which the processes are
loaded and unloaded from memory, and free memory space is fragmented.
• 2. Processes can't be assigned to memory blocks due to their small size, and the memory
locks stay unused. It is also necessary to understand that as programs are loaded and deleted
from memory, they generate free space or a hole in the memory.
• 3.These small blocks cannot be allotted to new arriving processes, resulting in inefficient
memory use.
• 4. The conditions of fragmentation depend on the memory allocation system. As the process
is loaded and unloaded from memory, these areas are fragmented into small pieces of
memory that cannot be allocated to incoming processes. It is called fragmentation.
• Types of Fragmentation
• There are mainly two types of fragmentation in the operating system. These are as follows:
• Internal Fragmentation
• External Fragmentation
• Internal Fragmentation
• When a process is allocated to a memory block, and if the process is smaller than the amount
of memory requested, a free space is created in the given memory block. Due to this, the
free space of the memory block is unused, which causes internal fragmentation.
• For Example:
• Assume that memory allocation in RAM is done using fixed partitioning (i.e., memory blocks
of fixed sizes). 2MB, 4MB, 4MB, and 8MB are the available sizes. The Operating System uses
a part of this RAM.
• Let's suppose a process P1 with a size of 3MB arrives and is given a memory block of 4MB. As
a result, the 1MB of free space in this block is unused and cannot be used to allocate
memory to another process. It is known as internal fragmentation.
5.2.3.2.External Fragmentation
• 1.External fragmentation happens when a dynamic memory allocation method allocates
some memory but leaves a small amount of memory unusable.
• 2. The quantity of available memory is substantially reduced if there is too much external
fragmentation.
• 3. There is enough memory space to complete a request, but it is not contiguous. It's known
as external fragmentation.
• 4.This problem occurs when you allocate RAM to processes continuously. It is done in paging
and segmentation, where memory is allocated to processes non-contiguously. As a result, if
you remove this condition, external fragmentation may be decreased.
• Compaction is another method for removing external fragmentation. External fragmentation
may be decreased when dynamic partitioning is used for memory allocation by combining all
free memory into a single large block.
• The larger memory block is used to allocate space based on the requirements of the new
processes. This method is also known as defragmentation.
• Advantages
• There are various advantages of fragmentation. Some of them are as follows:
• Fast Data Writes
• Data write in a system that supports data fragmentation may be faster than
reorganizing data storage to enable contiguous data writes.
• Fewer Failures
• If there is insufficient sequential space in a system that does not support
fragmentation, the write will fail.
• Storage Optimization
• A fragmented system might potentially make better use of a storage device by
utilizing every available storage block.
• Disadvantages
• There are various disadvantages of fragmentation. Some of them are as follows:
• Need for regular defragmentation
• A more fragmented storage device's performance will degrade with time,
necessitating the requirement for time-consuming defragmentation operations.
• Slower Read Times
• The time it takes to read a non-sequential file might increase as a storage device
becomes more fragmented.
5.2.4.Page Fault
• Page faults dominate more like an error. A page fault will happen if a program tries
to access a piece of memory that does not exist in physical memory (main
memory). The fault specifies the operating system to trace all data into virtual
memory management and then relocate it from secondary memory to its primary
memory, such as a hard disk.
• Concept of Demand Paging
1.The Demand Paging is a condition which is occurred in the Virtual Memory. We know that
the pages of the process are stored in secondary memory.
2.The page is brought to the main memory when required. We do not know when this
requirement is going to occur.
3. So, the pages are brought to the main memory when required by the Page Replacement
Algorithms.
4. So, the process of calling the pages to main memory to secondary memory upon demand
is known as Demand Paging.
Advantages of Demand Paging
• It increases the degree of multiprogramming as many processes can be present in the main
memory simultaneously.
• There is a more efficient use of memory as processes having a size more than the size of the
main memory can also be executed using this mechanism because we are not loading the
whole page at a time.
• We have to the right for scaling of virtual memory.
• If any program is larger than physical memory, it helps run this program without compaction.
• Partition management is simpler.
• It is more useful in a time-sharing system.
• It has no limitations on the level of multi-programming.
• Discards external fragmentation.
• Easy to swap all pages.
• Disadvantages of Demand Paging
• Below are some disadvantages of demand paging in an operating system, such as:
• The amount of processor overhead and the number of tables used for handling the page
faults is greater than in simple page management techniques.
• It has more probability of internal fragmentation.
• Its memory access time is longer.
• Page Table Length Register (PTLR) has a limit for virtual memory.
• Page map table is needed additional memory and registers.
• Concept of Overlays
• 1. In memory management, overlays refer to a technique used to manage memory
efficiently by overlaying a portion of memory with another program or data.
• 2. The main problem in Fixed partitioning is the size of a process has to be limited by
the maximum size of the partition, which means a process can never be span over
another.
• 3.In order to solve this problem, earlier people have used some solution which is called
as Overlays.
• 4.The concept of overlays is that whenever a process is running it will not use the
complete program at the same time, it will use only some part of it.
• 5. Then overlays concept says that whatever part you required, you load it and once the
part is done, then you just unload it, means just pull it back and get the new part you
required and run it.
• Advantages
• It reduced the need for memory.
• Reduced the time required.
• Disadvantages
• The programmer must specify the overlap map.
• The programmer must be aware of the memory requirements.
• Modules that overlap must be totally separate.
• Overlay programming structure is complicated and not always possible.
5.3.Page Repalcement Algorithms
• 1. When the processor need to execute a page and if that page is not
available in main memory then this situation is called page Fault.
• 2. For bringing in the required page into main memory, if the space is not
available in memory then we need to remove the page from the main
memory for allocating the space to the new page which needs to be
executed.
• 3. When a page fault occurs operating system has to choose a page to
remove from memory to make room for the page that has to be brought in
this is known as a Pager replacement.
• 4. The approach of page replacement is, if no frame is free, we find one
that is not currently being used and free it.
• 5. we can now use the freed frame by writing its contents to swap space
and changing the page table to indicate that the page is no longer in
memory.
• 6. we can now use the freed frame to hold the page for which the process
faulted . we modify the page fault service routine to include page
replacement.
• Steps in page replacement are listed below
• 1. Find the location of the desired page on the disK.
• 2. find a free frame
• i)If there is free frame,use it.
• i)If there is no free frame, use a page replacement algorithm to select a victim frame .
• ii)write the victim frame to the disk; change the page and frame tables accordingly.
• 3. Read the desired page into the newly freed frame change the page and frame table.
• 4.restart the user process.
Fig shows steps in page replacement: Swap out victim page
Frame Valid-invalid bit 1

change to invalid
2
F Victim

Reset page table for new page


o 4
i
f v 3 Swap desired page in

Page table Physical memory


5.3.1.FIFO(First In First Out)Page Replacement Algorithm

1.This is the first basic algorithm of Page Replacement Algorithms. This algorithm is
basically dependent on the number of frames used.
2.Then each frame takes up the certain page and tries to access it. When the frames
are filled then the actual problem starts.
3. The fixed number of frames is filled up with the help of first frames present. This
concept is fulfilled with the help of Demand Paging.
4.After filling up of the frames, the next page in the waiting queue tries to enter the
frame. If the frame is present then, no problem is occurred. Because of the page
which is to be searched is already present in the allocated frames.
5. If the page to be searched is found among the frames then, this process is known
as Page Hit.
6. The First In First Out (FIFO) Page Replacement Algorithm removes the Page in the
frame which is allotted long back. This means the useless page which is in the frame
for a longer time is removed and the new page which is in the ready queue and is
ready to occupy the frame is allowed by the First In First Out Page Replacement.
• Let us understand this First In First Out Page Replacement Algorithm working
with the help of an example.
• Example:
• Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
for a memory with three frames and calculate number of page faults by using
FIFO (First In First Out) Page replacement algorithms.
• Points to Remember
• Page Not Found - - - > Page Fault
• Page Found - - - > Page Hit
• Reference String:
• Number of Page Hits = 8
• Number of Page Faults = 12
• The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
• The Page Hit Percentage = 8 *100 / 20 = 40%
• The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%
• Explanation
• First, fill the frames with the initial pages. Then, after the frames are filled we need to create
a space in the frames for the new page to occupy. So, with the help of First in First Out Page
Replacement Algorithm we remove the frame which contains the page is older among the
pages. By removing the older page we give access for the new frame to occupy the empty
space created by the First in First out Page Replacement Algorithm.
• Advantages:
• 1.It is simple to implement
• 2.It is easier algorithm.
• 3.Easy to understand and execute.
• Disadvantages:
• 1.It is not very effective
• 2.System needs to track of each frame.
• 3.Its performance is not always good.
• 4.bad replacement choice increase the page fault rate and slow process execution.
5.3.2.Optimal Page Replacement Algorithm

• 1.This is the second basic algorithm of Page Replacement


Algorithms. This algorithm is basically dependent on the
number of frames used.
• 2. Then each frame takes up the certain page and tries to
access it. When the frames are filled then the actual problem
starts.
• 3.The fixed number of frames is filled up with the help of first
frames present. This concept is fulfilled with the help of
Demand Paging.
• 4.After filling up of the frames, the next page in the waiting
queue tries to enter the frame. If the frame is present then,
no problem is occurred. Because of the page which is to be
searched is already present in the allocated frames.
• 5.If the page to be searched is found among the frames then,
this process is known as Page Hit.
• If the page to be searched is not found among the frames then, this process is
known as Page Fault.
• When Page Fault occurs this problem arises, then the OPTIMAL Page Replacement
Algorithm comes into picture.
• The OPTIMAL Page Replacement Algorithms works on a certain principle. The
principle is:
• Replace the Page which is not used in the Longest Dimension of time in future
• This principle means that after all the frames are filled then, see the future pages
which are to occupy the frames. Go on checking for the pages which are already
available in the frames. Choose the page which is at last.
• Example:
• Suppose the Reference String is:
• 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
• 6, 1, 2 are in the frames occupying the frames.
• Now we need to enter 0 into the frame by removing one page from the page
• So, let us check which page number occurs last
• From the sub sequence 0, 3, 4, 6, 0, 2, 1 we can say that 1 is the last occurring
page number. So we can say that 0 can be placed in the frame body by removing 1
from the frame.
• Let us understand this OPTIMAL Page Replacement Algorithm working with the
help of an example.
• Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1,
4, 0 for a memory with three frames and calculate number of page faults
by using OPTIMAL Page replacement algorithms.
• Points to Remember
• Page Not Found - - - > Page Fault
• Page Found - - - > Page Hit
• Reference String:
• Number of Page Hits = 8
• Number of Page Faults = 12
• The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
• The Page Hit Percentage = 8 *100 / 20 = 40%
• The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%
• Explanation
• First, fill the frames with the initial pages. Then, after the frames are filled
we need to create a space in the frames for the new page to occupy.
• Here, we would fill the empty spaces with the pages we and the empty
frames we have. The problem occurs when there is no space for occupying
of pages. We have already known that we would replace the Page which is
not used in the Longest Dimension of time in future.
• There comes a question what if there is absence of page which is in the
frame.
• Suppose the Reference String is:
• 0, 2, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
• 6, 1, 5 are in the frames occupying the frames.
• Here, we can see that page number 5 is not present in the Reference
String. But the number 5 is present in the Frame. So, as the page number
5 is absent we remove it when required and other page can occupy that
position.
• Advantages Of optimal Page Replacement:
• 1.it is the best possible optimal algorithm.
• 2.it gives the smallest number of page faults.
• Disadvantages Of optimal Page Replacement
• 1.This algorithm is difficult to implement.
• 2.It is only use as a theoretical part of page
replacement.
• 3.It requires future knowledge of reference string.
5.3.3.LRU(Least Recently Used)Page Replacement Algorithm
• 1.This is the last basic algorithm of Page Replacement Algorithms. This algorithm is
basically dependent on the number of frames used.
• 2. Then each frame takes up the certain page and tries to access it. When the
frames are filled then the actual problem starts.
• 3. The fixed number of frames is filled up with the help of first frames present. This
concept is fulfilled with the help of Demand Paging
• 4.After filling up of the frames, the next page in the waiting queue tries to enter
the frame. If the frame is present then, no problem is occurred.
• 5. Because of the page which is to be searched is already present in the allocated
frames.
• 6.If the page to be searched is found among the frames then, this process is known
as Page Hit.
• 7.If the page to be searched is not found among the frames then, this process is
known as Page Fault.
• Example:
• Suppose the Reference String is:
• 6, 1, 1, 2, 0, 3, 4, 6, 0
• The pages with page numbers 6, 1, 2 are in the frames occupying the
frames.
• Now, we need to allot a space for the page numbered 0.
• Now, we need to travel back into the past to check which page can be
replaced.
• 6 is the oldest page which is available in the Frame.
• So, replace 6 with the page numbered 0.
• Let us understand this Least Recently Used (LRU) Page Replacement
Algorithm working with the help of an example.
• Example:
• Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1,
2, 0 for a memory with three frames and calculate number of page faults
by using Least Recently Used (LRU) Page replacement algorithms.
• Points to Remember
• Page Not Found - - - > Page Fault
• Page Found - - - > Page Hit
• Reference String:
• Number of Page Hits = 7
• Number of Page Faults = 13
• The Ratio of Page Hit to the Page Fault = 7 : 12 - - - > 0.5833 : 1
• The Page Hit Percentage = 7 * 100 / 20 = 35%
• The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 35 = 65%
• Explanation
• First, fill the frames with the initial pages. Then, after the frames are filled we need to create
a space in the frames for the new page to occupy.
• Here, we would fill the empty spaces with the pages we and the empty frames we have. The
problem occurs when there is no space for occupying of pages. We have already known that
we would replace the Page which is not used in the Longest Dimension of time in past or can
be said as the Page which is very far away in the past.

You might also like