0% found this document useful (0 votes)
67 views

Virtual Memory Revision

Virtual memory is a storage scheme that provides users with the illusion of having a very large main memory. It treats a part of secondary memory as the main memory. When a process is loaded, different parts of it are loaded into main memory instead of loading the whole process at once. This allows for more processes to be loaded simultaneously, improving CPU utilization. When a process requests a page not currently in main memory, it causes a page fault. The operating system then selects a page using a page replacement algorithm and loads the requested page instead.

Uploaded by

Aditya Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Virtual Memory Revision

Virtual memory is a storage scheme that provides users with the illusion of having a very large main memory. It treats a part of secondary memory as the main memory. When a process is loaded, different parts of it are loaded into main memory instead of loading the whole process at once. This allows for more processes to be loaded simultaneously, improving CPU utilization. When a process requests a page not currently in main memory, it causes a page fault. The operating system then selects a page using a page replacement algorithm and loads the requested page instead.

Uploaded by

Aditya Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Virtual Memory

Virtual memory is a part of the system's secondary memory that acts and gives
us a feel as if it is a part of the main memory. Virtual memory allows a system to
execute heavier applications or multiple applications simultaneously without
exhausting the RAM (Random Access Memory). In particular, the system can
behave as if its total RAM resources were equal to the whole amount of physical
RAM plus the complete amount of virtual RAM.
OR
Virtual Memory is a storage scheme that provides user an illusion of having a
very big main memory. This is done by treating a part of secondary memory as
the main memory.
In this scheme, User can load the bigger size processes than the available main
memory by having the illusion that the memory is available to load the process.
Instead of loading one big process in the main memory, the Operating System
loads the different parts of more than one process in the main memory.
By doing this, the degree of multiprogramming will be increased and therefore,
the CPU utilization will also be increased.
Modern microprocessors intended for general-purpose use, a memory
management unit, or MMU, is built into the hardware. The MMU's job is to
translate virtual addresses into physical addresses. A basic example is given
below −
Virtual memory is commonly implemented by demand paging. It can also be
implemented in a segmentation system. Demand segmentation can also be used
to provide virtual memory.

Page Table

Imagine the physical memory of your computer to the front section of your
wardrobe. You keep the clothes that you would like to wear in front so that they
are handy. Now, in the above scenario, consider yourself to be the operating
system and the backside of your wardrobe to be the hard disk. The clothes at the
back can also be accessed when needed although they may take a little more
time. To make it easy to search, you may keep a note of where you have placed
what clothing items. Such a mapping mechanism is called a page table.

What is Demand Paging?

Let's consider the wardrobe analogy that we spoke about earlier. Imagine that
you are scrolling through an e-commerce platform and like some clothes.
However, since you don't have space in your wardrobe, you save it in your
wishlist and decide to buy them only when needed. As compared to this process,
demand paging is a process that keeps pages of a process that are infrequently
used in secondary memory, and pulls them only when required to satisfy the
demand.

A demand paging system is quite similar to a paging system with swapping


where processes reside in secondary memory and pages are loaded only on
demand, not in advance. When a context switch occurs, the operating system
does not copy any of the old program’s pages out to the disk or any of the new
program’s pages into the main memory Instead, it just begins executing the new
program after loading the first page and fetches that program’s pages as they are
referenced.
While executing a program, if the program references a page which is not
available in the main memory because it was swapped out a little ago, the
processor treats this invalid memory reference as a page fault and transfers
control from the program to the operating system to demand the page back into
the memory.

Advantages
Following are the advantages of Demand Paging −
 Large virtual memory.
 More efficient use of memory.
 There is no limit on degree of multiprogramming.
Disadvantages
 Number of tables and the amount of processor overhead for handling page
interrupts are greater than in the case of the simple paged management
techniques.

What is Page Replacement in Operating Systems?

Page replacement is needed in the operating systems that use virtual memory
using Demand Paging. As we know that in Demand paging, only a set of pages
of a process is loaded into the memory. This is done so that we can have more
processes in the memory at the same time.
When a page that is residing in virtual memory is requested by a process for its
execution, the Operating System needs to decide which page will be replaced by
this requested page. This process is known as page replacement and is a vital
component in virtual memory management.

Why Need Page Replacement Algorithms?

To understand why we need page replacement algorithms, we first need to know


about page faults. Let’s see what is a page fault.

Page Fault: A Page Fault occurs when a program running in CPU tries to access


a page that is in the address space of that program, but the requested page is
currently not loaded into the main physical memory, the RAM of the system.

Since the actual RAM is much less than the virtual memory the page faults occur.
So whenever a page fault occurs, the Operating system has to replace an
existing page in RAM with the newly requested page. In this scenario, page
replacement algorithms help the Operating System in deciding which page to
replace. The primary objective of all the page replacement algorithms is to
minimize the number of page faults.

Page Replacement Algorithms in Operating Systems

First In First Out (FIFO)

FIFO algorithm is the simplest of all the page replacement algorithms. In this, we


maintain a queue of all the pages that are in the memory currently. The oldest
page in the memory is at the front-end of the queue and the most recent page is
at the back or rear-end of the queue.

Whenever a page fault occurs, the operating system looks at the front-end of the
queue to know the page to be replaced by the newly requested page. It also
adds this newly requested page at the rear-end and removes the oldest page
from the front-end of the queue.

Example: Consider the page reference string as 3, 1, 2, 1, 6, 5, 1, 3 with 3-page


frames. Let’s try to find the number of page faults:
 Initially, all of the slots are empty so page faults occur at 3,1,2.

Page faults = 3

 When page 1 comes, it is in the memory so no page fault occurs.

Page faults = 3

 When page 6 comes, it is not present and a page fault occurs. Since there
are no empty slots, we remove the front of the queue, i.e 3.

Page faults = 4

 When page 5 comes, it is also not present and hence a page fault occurs.
The front of the queue i.e 1 is removed.

Page faults = 5

 When page 1 comes, it is not found in memory and again a page fault
occurs. The front of the queue i.e 2 is removed.

Page faults = 6

 When page 3 comes, it is again not found in memory, a page fault occurs,
and page 6 is removed being on top of the queue

Total page faults = 7

Belady's anomaly: Generally if we increase the number of frames in the


memory, the number of page faults should decrease due to obvious
reasons. Belady’s anomaly refers to the phenomena where increasing the
number of frames in memory, increases the page faults as well.
Advantages

 Simple to understand and implement


 Does not cause more overhead

Disadvantages

 Poor performance
 Doesn’t use the frequency of the last used time and just simply replaces the
oldest page.
 Suffers from Belady’s anomaly.

Optimal Page Replacement in OS

Optimal page replacement is the best page replacement algorithm as this


algorithm results in the least number of page faults. In this algorithm, the pages
are replaced with the ones that will not be used for the longest duration of time in
the future. In simple terms, the pages that will be referred farthest in the future
are replaced in this algorithm.

Example:

Let’s take the same page reference string 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames
as we saw in FIFO. This also helps you understand how Optimal Page
replacement works the best.

 Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and
take the empty slots.

Page faults = 3

 When page 1 comes, it is in the memory and no page fault occurs.

Page faults = 3
 When page 6 comes, it is not in the memory, so a page fault occurs and 2
is removed as it is not going to be used again.

Page faults = 4

 When page 5 comes, it is also not in the memory and causes a page fault.
Similar to above 6 is removed as it is not going to be used again.

page faults = 5

 When page 1 and page 3 come, they are in the memory so no page fault
occurs.

Total page faults = 5

Advantages

 Excellent efficiency
 Less complexity
 Easy to use and understand
 Simple data structures can be used to implement
 Used as the benchmark for other algorithms

Disadvantages

 More time consuming


 Difficult for error handling
 Need future awareness of the programs, which is not possible every time

Least Recently Used (LRU) Page Replacement Algorithm

The least recently used page replacement algorithm keeps the track of usage of


pages over a period of time. This algorithm works on the basis of the principle of
locality of a reference which states that a program has a tendency to access
the same set of memory locations repetitively over a short period of time. So
pages that have been used heavily in the past are most likely to be used heavily
in the future also.

In this algorithm, when a page fault occurs, then the page that has not been
used for the longest duration of time is replaced by the newly requested
page.

Example: Let’s see the performance of the LRU on the same reference string of
3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames:
 Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and
take the empty slots.

Page faults = 3

 When page 1 comes, it is in the memory and no page fault occurs.

Page faults = 3

 When page 6 comes, it is not in the memory, so a page fault occurs and the
least recently used page 3 is removed.

Page faults = 4

 When page 5 comes, it again causes a page fault and page 1 is


removed as it is now the least recently used page.

Page faults = 5

 When page 1 comes again, it is not in the memory and hence page 2 is
removed according to the LRU.

Page faults = 6

 When page 3 comes, the page fault occurs again and this time page 6 is
removed as the least recently used one.

Total page faults = 7

Now in the above example, the LRU causes the same page faults as the FIFO,
but this may not always be the case as it will depend upon the series, the number
of frames available in memory, etc. In fact, on most occasions, LRU is better
than FIFO.

Advantages

 It is open for full analysis


 Doesn’t suffer from Belady’s anomaly
 Often more efficient than other algorithms

Disadvantages

 It requires additional data structures to be implemented


 More complex
 High hardware assistance is required

Last In First Out (LIFO) Page Replacement Algorithm

This is the Last in First Out algorithm and works on LIFO principles. In this


algorithm, the newest page is replaced by the requested page. Usually, this is
done through a stack, where we maintain a stack of pages currently in the
memory with the newest page being at the top. Whenever a page fault occurs,
the page at the top of the stack is replaced.

Example: Let’s see how the LIFO performs for our example string of 3, 1, 2, 1, 6,
5, 1, 3 with 3-page frames:

 Initially, since all the slots are empty, page 3,1,2 causes a page fault and
takes the empty slots.

Page faults = 3

 When page 1 comes, it is in the memory and no page fault occurs.

Page faults = 3
 When page 6 comes, the page fault occurs and page 2 is removed as it is
on the top of the stack and is the newest page.

Page faults = 4

 When page 5 comes, it is not in the memory, which causes a page fault,
and hence page 6 is removed being on top of the stack.

Page faults = 5

 When page 1 and page 3 come, they are in memory already, hence no
page fault occurs.

Total page faults = 5

As you may notice, this is the same number of page faults as of the optimal page
replacement algorithm. So we can say that for this series of pages, this is the
best algorithm that can be implemented without the prior knowledge of future
references.

Advantages

 Simple to understand
 Easy to implement
 No overhead

Disadvantages

 Does not consider Locality principle, hence may produce worst


performance
 The old pages may reside in memory forever even if they are not used

You might also like