Optimal is the simplest technique for replacing pages in a frame. It decides when a page fault occurs then which frames are to be replaced. Create a program to counts the number of page fault occurs when an input reference string is given. The input contains integer values for a number of pages in a queue, sequence of reference strings and frame size. The output should display the total number of page faults.
Note: Text in Bold corresponds to the input
Sample Input and Output :
Enter number of pages:
6
Enter reference string:
1
2
2
3
4
5
Enter number of frames:
3
Total Page Fault is 5
Answer:
#include<stdio.h> int main() { int pages,m,n,frames,pagef=0,flag,temp,position,m_interval,p_frame=-1,interval[25],found; printf("Enter number of pages: \n"); scanf("%d",&pages); int page[pages]; printf("Enter reference string: \n"); for(m=0;m<pages;m++){ scanf("%d",&page[m]); } printf("Enter number of frames:\n"); scanf("%d",&frames); int frame[frames]; for(m=0;m<frames;m++){ frame[m]=-1; } for(m=0;m<pages;m++) { flag=0; for(n=0;n<frames;n++){ if(frame[n]==page[m]){ flag=1; break; } } if(flag==0){ if(p_frame==frames-1){ for(n=0;n<frames;n++){ for(temp=m+1;temp<pages;temp++){ interval[n]=0; if(frame[n]==page[temp]){ interval[n]=temp-m; break; } } } found=0; for(n=0;n<frames;n++){ if(interval[n]==0){ position=n; found=1; break; } } } else{ position=++p_frame; found=1; } if(found==0){ m_interval=interval[0]; position=0; for(n=1;n<frames;n++){ if(m_interval<interval[n]){ m_interval=interval[n]; position=n; } } } frame[position]=page[m]; pagef++; } } printf("Total Page Fault is %d\n",pagef); }
0 Comments