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

Virtual Memory

The document describes an experiment to implement a paged virtual memory system using the FIFO algorithm in C++. It explains virtual memory and how it uses the hard drive to emulate RAM. When a page is requested, it checks if it is already loaded in memory. If not, it loads the page using FIFO by removing the oldest page and adding the new one. It outputs the initial memory contents, page faults on requests, and final memory contents. The number of page faults is also displayed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Virtual Memory

The document describes an experiment to implement a paged virtual memory system using the FIFO algorithm in C++. It explains virtual memory and how it uses the hard drive to emulate RAM. When a page is requested, it checks if it is already loaded in memory. If not, it loads the page using FIFO by removing the oldest page and adding the new one. It outputs the initial memory contents, page faults on requests, and final memory contents. The number of page faults is also displayed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

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;

using namespace std;


bool isLoaded(int page_number)
{
std::deque<int>::iterator it=page_memory.begin();
while(it!=page_memory.end())
{
if(*(it++)==page_number)
{
return true;
}
}
return false;
}

void loadPage(int page_number)


{
page_memory.pop_front();
page_memory.push_back(page_number);
}

void serveRequest(int requested_page)


{
if(!isLoaded(requested_page))
{
number_of_faults++;
std::cout<<"Requested page doesn't exist in memory. Will now be
loaded."<<std::endl;
loadPage(requested_page);
}
else
{
std::cout<<"Requested page already exists in memory."<<std::endl;
}
}

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;
}

std::cout<<"Memory finally contains: ";


std::deque<int>::iterator it1=page_memory.begin();
while(it1 != page_memory.end())
{
std::cout<<*(it1++)<<" ";
}

std::cout<<"\n"<<number_of_faults<<" page fault(s) occured during the series of


requests!"<<std::endl;
return 0;
}
Output:

Conclusion:

You might also like