SOFTWARE LABORATORY
CODE :
#include<stdio.h>
#include<stdlib.h>
int main()
{
int rs[20],fsz,no,hit=0,miss=0,fr[20],lru=0,used[20]={0},max;
int i,j,k,ch;
do{
printf("\n\t1.LRU\n\t2.Optimal\n\t3.Exit\n\tEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter size of reference string : ");
scanf("%d",&no);
printf("\n\tEnter String : ");
for(i=0;i<no;i++)
scanf("%d",&rs[i]);
printf("\n\tEnter frame size : ");
scanf("%d",&fsz);
for(i=0;i<fsz;i++)
fr[i]=-1;
for(i=0;i<no;i++)
{
for(j=0;j<fsz;j++)
{
if(fr[j]==rs[i])
{
hit++;
printf("\n\t***Page hit ***: Frame = ");
for(k=0;k<fsz;k++)
printf(" %d ",fr[k]);
break;
}
}
if(j==fsz)
{
for(j=0;j<fsz;j++)
{
if(fr[j]==-1)
{
miss++;
fr[j]=rs[i];
printf("\n\tPage miss : Frame = ");
for(k=0;k<fsz;k++)
printf(" %d ",fr[k]);
break;
}
}
}
if(j==fsz)
{
miss++;
printf("\n\tPage miss : Frame Before = ");
for(k=0;k<fsz;k++)
{
printf(" %d ",fr[k]);
used[k]=0;
for(j=i-1;j<0;j--)
{
if(fr[k]==rs[j])
break;
used[k]++;
}
}
max=0;
for(j=0;j<fsz;j++)
{
if(max<used[j])
{
max=used[j];
lru=j;
}
}
fr[lru]=rs[i];
printf("\tAfter = ");
for(k=0;k<fsz;k++)
printf(" %d ",fr[k]);
}
}
printf("\n\t No of miss = %d and no of hit = %d ",miss,hit);
break;
case 2:
printf("\n\tEnter size of reference string : ");
scanf("%d",&no);
printf("\n\tEnter String : ");
for(i=0;i<no;i++)
scanf("%d",&rs[i]);
printf("\n\tEnter frame size : ");
scanf("%d",&fsz);
for(i=0;i<fsz;i++)
fr[i]=-1;
hit=miss=0;
for(i=0;i<no;i++)
{
for(j=0;j<fsz;j++)
{
if(fr[j]==rs[i])
{
hit++;
printf("\n\t***Page hit ***: Frame = ");
for(k=0;k<fsz;k++)
printf(" %d ",fr[k]);
break;
}
}
if(j==fsz)
{
for(j=0;j<fsz;j++)
{
if(fr[j]==-1)
{
miss++;
fr[j]=rs[i];
printf("\n\tPage miss : Frame = ");
for(k=0;k<fsz;k++)
printf(" %d ",fr[k]);
break;
}
}
}
if(j==fsz)
{
miss++;
printf("\n\tPage miss : Frame Before = ");
for(k=0;k<fsz;k++)
{
printf(" %d ",fr[k]);
used[k]=0;
for(j=i+1;j<no;j++)
{
if(fr[k]==rs[j])
break;
used[k]++;
}
}
max=0;
for(j=0;j<fsz;j++)
{
if(max<used[j])
{
max=used[j];
lru=j;
}
}
fr[lru]=rs[i];
printf("\tAfter = ");
for(k=0;k<fsz;k++)
printf(" %d ",fr[k]);
}
}
printf("\n\t No of miss = %d and no of hit = %d ",miss,hit);
break;
}
}while(ch!=3);
}
OUTPUT :
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/cpu scheduling# cd ..
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs# cd page\ replacement/
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/page replacement# gcc p_pr.croot@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/page replacement# ./a.out
1.LRU
2.Optimal
3.Exit
Enter choice : 1
Enter size of reference string : 5
Enter String : 2 3 2 1 4
Enter frame size : 2
Page miss : Frame = 2 -1
Page miss : Frame = 2 3
***Page hit ***: Frame = 2 3
Page miss : Frame Before = 2 3 After = 1 3
Page miss : Frame Before = 1 3 After = 4 3
No of miss = 4 and no of hit = 1
1.LRU
2.Optimal
3.Exit
Enter choice : 2
Enter size of reference string : 10
Enter String : 1 3 2 4 5 7 2 5 3 1
Enter frame size : 3
Page miss : Frame = 1 -1 -1
Page miss : Frame = 1 3 -1
Page miss : Frame = 1 3 2
Page miss : Frame Before = 1 3 2 After = 4 3 2
Page miss : Frame Before = 4 3 2 After = 5 3 2
Page miss : Frame Before = 5 3 2 After = 5 7 2
***Page hit ***: Frame = 5 7 2
***Page hit ***: Frame = 5 7 2
Page miss : Frame Before = 5 7 2 After = 3 7 2
Page miss : Frame Before = 3 7 2 After = 1 7 2
No of miss = 8 and no of hit = 2
1.LRU
2.Optimal
3.Exit
Enter choice : 3
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/page replacement#
No comments:
Post a Comment