6/recent/ticker-posts

Optimal Page Replacement 1

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.  Initially, all the frames contain default value as -1. 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 reference string given, pages in frames and the total number of page faults.

Note: Text in Bold corresponds to the input 
  To display pages inside a frame use additional spacing ("%10d",frame) between pages.
Sample Input and Output

Enter number of pages: 

9
Enter reference string: 
7
0
1
2
0
3
0
4
2

Enter number of frames: 
3
Page_Frames
         7        -1        -1
         7         0        -1
         7         0         1
         2         0         1
         2         0         1
         2         0         3
         2         0         3
         2         4         3
         2         4         3
Total Page Fault is 6


Answer:

#include<stdio.h>
int main()
{
    int pagef=0,pages,frames,i,n,m,m_interval,interval[100],found,position,p_frame=-1,flag,temp;
    printf("Enter number of pages:\n");
    scanf("%d",&pages);
    int ref[pages];
    printf("Enter reference string:\n");
    for(i=0;i<pages;i++)
    {
        scanf("%d",&ref[i]);
    }
    printf("Enter number of frames:\n");
    scanf("%d",&frames);
    int frame[frames];
    printf("Page_Frames\n");
    for(i=0;i<frames;i++){
        frame[i]=-1;
    }
    for(m=0;m<pages;m++)
    {
        flag=0;
        for(n=0;n<frames;n++){
            if(ref[m]==frame[n]){
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            if(p_frame==frames-1){
                for(n=0;n<frames;n++){
                    interval[n]=0;
                    for(temp=m+1;temp<pages;temp++){
                        if(frame[n]==ref[temp]){
                            interval[n]=interval[n]+1;
                        }
                    }
                }
                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=0;n<frames;n++){
                    if(m_interval>interval[n]){
                        m_interval=interval[n];
                        position=n;
                    }
                }
            }
            frame[position]=ref[m];
            pagef++;
        }
        for(n=0;n<frames;n++)
        {
            printf("%10d",frame[n]);
        }
        printf("\n");
    }
    printf("Total Page Fault is %d",pagef);
}



Post a Comment

0 Comments