Virtual Memory
Virtual Memory
10
Aim: To study and write a c program to demonstrate paged virtual memory FIFO algorithm
Language: C++
Theory:
Virtual memory is a memory management capability of an operating system (OS) that
uses hardware and software to allow a computer to compensate for physical memory
shortages by temporarily transferring data from RAM to disk storage. Computers have a
finite amount of RAM, so memory can run out, especially when multiple programs are
running at the same time. A system using virtual memory uses a section of hard drive to
emulate RAM. With virtual memory, a system can load larger programs or multiple programs
running at the same time, allowing each to operate as if it has infinite memory and without
having to purchase more RAM.
Techniques that automatically move program and data blocks into the physical main
memory when they are required for execution are called virtual memory techniques. The
binary addresses that the processor issues for either instructions or data are called virtual or
logical addresses. These are translated into physical addresses using software and hardware
components. If virtual address refers to a part of the program or data space that is currently in
the physical memory, then the contents of the appropriate location of the main memory are
accessed immediately. However, if the referenced address is not in the main memory, its
contents must be brought into a suitable location in the memory before they can be used. A
special hardware unit, called the Memory Management Unit (MMU), translates virtual
addresses to physical addresses. When data is in the main memory, it is fetched using the
cache mechanism. If the data is not in the main memory, MMU causes the OS to bring data
into the memory from the disk. Transfer between disk and the main memory is performed
using DMA scheme.
Code:
#include<iostream>
#include<deque>
#include<vector>
#define MEM_SIZE 16
std::deque<int>page_memory;
int number_of_faults = 0;
int main()
{
for(int i = 0; i < MEM_SIZE; i++)
{
page_memory.push_back(i);
}
std::cout<<"Memory size is"<<MEM_SIZE<<"pages"<<std::endl;
std::cout<<"Memory initially contains:";
std::deque<int>::iterator it=page_memory.begin();
while(it != page_memory.end())
{
std::cout<<*(it++)<<" ";
}
int request,choice = 1;
while(choice != 2)
{
std::cout<<"\nEnter page number to request: ";
std::cin>>request;
serveRequest(request);
std::cout<<"\nEnter 1 to Continue\n2 to Quit\n";
std::cin>>choice;
}
Conclusion: