Virtual Memory Revision
Virtual Memory Revision
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.
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.
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.
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.
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.
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.
Page faults = 3
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
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.
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
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.
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
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
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
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.
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
Disadvantages
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
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.
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