C programming Simulate an example of an optimal algorithm fo
C programming
Simulate an example of an optimal algorithm for operating systems.
Solution
Let us take a case of optimal page replacement algorithm usd in memory managment by operating systemin this the page with the highest labels should be removed.
include<stdio.h>
#include<conio.h>
int rs[3];
void main()
{
void display();
int z[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int max,found=0,we[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
rs[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<3;i++)
{
if(rs[i]==z[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(rs[i]==-1)
{
rs[i]=z[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
we[i]=0;
for(i=0;i<rssize;i++)
{
for(k=j+1;k<12;k++)
{
if(rs[i]==z[k])
{
we[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<rssize;i++)
{
if(we[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=we[0];
index=0;
for(i=1;i<rssize;i++)
{
if(max<we[i])
{
max=we[i];
index=i;
}
}
}
fr[index]=p[j];
pf++;
}
display();
}
printf(\"\ no of page faults:%d\",pf);
getch();
}
void display()
{
int i;
printf(\"\ \");
for(i=0;i<3;i++)
printf(\"\\t%d\",rs[i]);
}
here output of the above program
OUTPUT :
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 1
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2
no of page faults : 4



