Вы находитесь на странице: 1из 5

08P31A0563

AIM: To write a program that simulates Optimal page replacement algorithm.

PROGRAM:
int nextIndexOfMM() { int n,m=-1,j; for(j=0;j<nf;j++) if(MainMemory[j]==-1) return j; for(j=0;j<nf;j++) { n=nextIndexFromRF(MainMemory[j]); if(n==lrf) return j; if(n>m) m=n; } return index(rf[m]); } void main() { int i=0; printf("Enter the length of the reference String : "); scanf("%d",&lrf); printf("Enter the Reference String : "); for(i=0;i<lrf;i++) scanf("%d",&rf[i]); printf("Enter the No.Of Frames : "); scanf("%d",&nf); printf("****SnapShots of MainMemory while replacing pages***\n\n"); for(i=0;i<nf;i++) MainMemory[i]=-1; for(r=0;r<lrf;r++)

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563

if(isInMainMemory(rf[r])) ; else { MainMemory[nextIndexOfMM()]=rf[r]; showFrames(); } getch(); }

OUTPUT:
Enter the length of the reference String : 20 Enter the Reference String : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter the No.Of Frames : 3 ****SnapShots of MainMemory while replacing pages*** 7 -1 -1 7 0 -1 7 0 1 2 0 1 2 0 3 2 4 3 2 0 3 2 0 1 7 0 1

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563

AIM: To write a program that simulates Least Frequently Used page replacement algorithm.

PROGRAM:
int rf[50],lrf,nf,r=0,i; int MainMemory[10],at[10]; int isInMainMemory(int page) { for(i=0;i<nf;i++) if(MainMemory[i]==page) return 1; return 0; } void ShowFrames() { for(i=0;i<nf;i++) printf("%3d ",MainMemory[i]); printf("\n"); } int index(int page) { for(i=0;i<nf;i++) if(MainMemory[i]==page) return i; return -1; } int count(int page) { int count=0; for(i=0;i<=r;i++) if(rf[i]==page) count++; return count; } /* at --> Arrival Time*/

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563

int nextIndexOfMM() { int n,m=lrf+1,j,idx=0; for(j=0;j<nf;j++) if(MainMemory[j]==-1) return j; for(j=0;j<nf;j++) { n=count(MainMemory[j]); if(n==0) return j; if(n<m||(n==m&&at[j]<at[idx])) { m=n; idx=j; } } return idx; } void main() { int i=0; printf("Enter the length of the reference String : "); scanf("%d",&lrf); printf("Enter the Reference String : "); for(i=0;i<lrf;i++) scanf("%d",&rf[i]); printf("Enter the No.Of Frames : "); scanf("%d",&nf); printf("****SnapShots of MainMemory while replacing pages***\n\n"); for(i=0;i<nf;i++) MainMemory[i]=-1;

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563

for(r=0;r<lrf;r++) if(isInMainMemory(rf[r])) ; else { int t=nextIndexOfMM(); MainMemory[t]=rf[r]; at[t]=r; ShowFrames(); } getch(); }

OUTPUT:
Enter the length of the reference String : 20 Enter the Reference String : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter the No.Of Frames : 3 ****SnapShots of MainMemory while replacing pages*** 7 -1 -1 7 0 -1 7 0 1 2 0 1 2 0 3 4 0 3 4 0 2 3 0 2 3 0 1 3 0 2 1 0 2 7 0 2 1 0 2

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

Вам также может понравиться