You are on page 1of 5

08P31A0563 AIM: To write a program that simulates First In First Out page replacement algorithm.

PROGRAM:
#include<stdio.h> int rf[50],lrf,nf,i; int MainMemory[10]; int isInMainMemory(int page) { for(i=0;i<nf;i++) if(MainMemory[i]==page) return 0; } void ShowFrames() { for(i=0;i<nf;i++) printf("%3d ",MainMemory[i]); printf("\n"); } void main() { int i=0,j=0,k=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(j=0;j<lrf;j++) { return 1; /*Page Found*/ /*rf --> Reference String*/

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563 if(isInMainMemory(rf[j])) ; else { if(k==nf) k=0; MainMemory[k++]=rf[j]; ShowFrames(); } } }

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 ****Snap shots of MainMemory while replacing pages*** 7 -1 -1 7 0 -1 7 0 1 2 0 1 2 3 1 2 3 0 4 3 0 4 2 0 4 2 3 0 2 3 0 1 3 0 1 2 7 1 2 7 0 2 7 0 1

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563 AIM: To write a program that simulates Least Recently Used page replacement algorithm.

PROGRAM:
int rf[50],lrf,nf,r=0,i; int MainMemory[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 previousIndexFromRF(int page) { for(i=r;i>=0;i--) if(rf[i]==page) return i; return -1; } int nextIndexOfMM() { int n,m=lrf,j; /*Page Found*/ /*rf --> Reference String*/

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563 for(j=0;j<nf;j++) if(MainMemory[j]==-1) return j; for(j=0;j<nf;j++) { n=previousIndexFromRF(MainMemory[j]); if(n==-1) 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("****Snap Shots of MainMemory while replacing pages***\n\n"); for(i=0;i<nf;i++) MainMemory[i]=-1; for(r=0;r<lrf;r++) if(isInMainMemory(rf[r])) ; else { MainMemory[nextIndexOfMM()]=rf[r]; ShowFrames(); } }

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

08P31A0563

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 ****Snap Shots 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 4 3 2 0 3 2 1 3 2 1 0 2 1 0 7

SRI SAI ADITYA INSTITUTE OF SCIENCE AND TECHNOLOGY

You might also like