Design and implement a Demand Paging virtual memory simulator! It must be a text
ID: 3539433 • Letter: D
Question
Design and implement a Demand Paging virtual memory simulator!
It must be a text based application (NOT a GUI based one).
You can use the C/C++ or Java programming language.
The following algorithms must be implemented: FIFO, OPT and LRU.
The application must simulate the execution of each of these algorithms on a hypothetical computer having only five physical frames (numbered from 0 to 4), assuming that the single process that is running has a virtual memory of ten frames (numbered from 0 to 9).
The algorithms will be simulated based on a reference string (a sequence of pages that are to be accessed) that will be either read from the keyboard or randomly generated.
The program should be menu-based (see below menu requirements) and the menu will keep the user in a loop containing the following options:
Will exit the program
1 Read reference string
A reference string will be read from the keyboard and stored in a buffer. Each value of the reference string will be verified and validated (or rejected).
Using option 1 more than once will result in overwriting the old reference string.
2 Generate reference string
A reference string will be randomly generated; the length of the reference string will be given by the user interactively. The string will be stored in a buffer.
Using option 2 more than once will result in overwriting the old reference string.
3 Display current reference string
Will display the stored reference string; if there is no reference string stored yet, an error message will be displayed.
4 Simulate FIFO
Will simulate the step by step execution of the FIFO algorithm using the stored reference string; if there is no reference string stored yet, an error message must be displayed.
The user will press a key after each step of the simulation to continue the simulation.
5 Simulate OPT
Will simulate the step by step execution of the OPT algorithm using the stored reference string; if there is no reference string stored yet, an error message must be displayed.
The user will press a key after each step of the simulation to continue the simulation.
6 Simulate LRU
Will simulate the step by step execution of the LRU algorithm using the stored reference string; if there is no reference string stored yet, an error message must be displayed.
The user will press a key after each step of the simulation to continue the simulation.
Selecting a different option will result in an error message but the user will NOT exit the loop!
Please read the entire requirements and provide a complete script with all 7 menu items and a working simulator for full points.
Explanation / Answer
#include<stdio.h>
void FIFO(char [],char [],int,int);
void lru(char [],char [],int,int);
void opt(char [],char [],int,int);
int main()
{
int ch,YN=1,i,l,f;
char F[10],s[25];
clrscr();
//system("clear");
printf(" Enter the no of empty frames: ");
scanf("%d",&f);
printf(" Enter the length of the string: ");
scanf("%d",&l);
printf(" Enter the string: ");
scanf("%s",s);
for(i=0;i<f;i++)
F[i]=-1;
do
{
// system("clear");
printf(" *********** MENU ***********");
printf(" 1:FIFO 2:LRU 3:OPT 4:EXIT");
printf(" Enter your choice: ");
scanf("%d",&ch);
//system("clear");
switch(ch)
{
case 1:
for(i=0;i<f;i++)
{
F[i]=-1;
}
FIFO(s,F,l,f);
break;
case 2:
for(i=0;i<f;i++)
{
F[i]=-1;
}
lru(s,F,l,f);
break;
case 3:
for(i=0;i<f;i++)
{
F[i]=-1;
}
opt(s,F,l,f);
break;
case 4:
exit(0);
}
printf(" Do u want to continue IF YES PRESS 1 IF NO PRESS 0 : ");
scanf("%d",&YN);
}while(YN==1);return(0);
}
//FIFO
void FIFO(char s[],char F[],int l,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf(" PAGE FRAMES FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}
if(flag==0)
{
printf(" %c ",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
printf(" Page-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf(" %c ",s[i]);
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
printf(" No page-fault");
}
if(j==f)
j=0;
}
}
//LRU
void lru(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,cnt=0,top=0;
printf(" PAGE FRAMES FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
{
flag=1;
break;
}
}
printf(" %c ",s[i]);
if(j!=f && flag!=1)
{
F[top]=s[i];
j++;
if(j!=f)
top++;
}
else
{
if(flag!=1)
{
for(k=0;k<top;k++)
{
F[k]=F[k+1];
}
F[top]=s[i];
}
if(flag==1)
{
for(m=k;m<top;m++)
{
F[m]=F[m+1];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.