6/recent/ticker-posts

OS Least Recent Use (LRU)

LRU 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 print PageFrames use ("%10s","Page_Frames") and to display pages inside a frame use additional spacing ("%10d",frame) between pages.
Sample Input and Output

Enter number of pages: 
6
Enter reference string: 
1
2
2
3
4
5

Enter number of frames: 
3
Page_Frames
         1        -1        -1
         1         2        -1
         1         2        -1
         1         2         3
         4         2         3
         4         5         3
Total Page Faults: 5


Answer:


#include<stdio.h>
int main()
{
    int pages,frames,m,n,position,k,l,temp[10],a=0,b=0,pagef=0;
    printf("Enter number of pages: \n");
    scanf("%d",&pages);
    printf("Enter reference string: \n");
    int ref[pages];
    for(m=0;m<pages;m++){
        scanf("%d",&ref[m]);
    }
    printf("Enter number of frames: \n");
    scanf("%d",&frames);
    int frame[frames];
    printf("Page_Frames\n");
    for(m=0;m<frames;m++){
        frame[m]=-1;
    }
    for(n=0;n<pages;n++){
        a=0,b=0;
        for(m=0;m<frames;m++){
            if(frame[m]==ref[n]){
                a=1;
                b=1;
                break;
            }
        }
        if(a==0){
            for(m=0;m<frames;m++){
                if(frame[m]==-1){
                    frame[m]=ref[n];
                    b=1;
                    pagef++;
                    break;
                }
            }
        }
        if(b==0){
            for(m=0;m<frames;m++){
                temp[m]=0;
            }
            for(k=n-1,l=1;l<=frames-1;l++,k--){
                for(m=0;m<frames;m++){
                    if(frame[m]==ref[k]){
                        temp[m]=1;
                    }
                }
            }
            for(m=0;m<frames;m++){
                if(temp[m]==0){
                    position=m;
                }
            }
            frame[position]=ref[n];
            pagef++;
        }
        for(m=0;m<frames;m++){
            printf("%10d",frame[m]);
        }
        printf("\n");
        }
        printf("Total Page Faults: %d",pagef);
    
}



Post a Comment

0 Comments