Skip to main content

LRU ,Optimal and FIFO Page Replacement Techniques

Program Start:
#include<stdio.h>
/*
* n use for pages
* pg[30] use for pages
* fr[10] use for creating fargements
*/
int n,pg[30],fr[10];
//fifo page replacement
void fifo();
//optimal page replacement
void optimal();
//least recent use page replacement
void lru();
//main function is start from here
void main()
{
            /*
            * i use for loop
            * ch is use for choice
            */
            int i,ch;
            printf("+++++++++++++++++++++++++++++++++\n");
            printf("      -1  = NULL\n");
            printf("+++++++++++++++++++++++++++++++++\n");
            printf("\nEnter total number of pages:");
            //enter number of pages you want like 10
            scanf("%d",&n);
            //page sequences size enter like 1 2 3 4 5 1 1 1 1 1
            printf("\nEnter sequence:");
            for(i=0;i<n;i++)                                   //accepting sequence
                        scanf("%d",&pg[i]);//take input of each page into pg array
            do
            { //select choice of any of them the apply the functionilty
                        printf("\n\tMENU\n");
                        printf("\n1)FIFO");
                        printf("\n2)OPTIMAL");
                        printf("\n3)LRU");
                        printf("\n4)Exit");
                        printf("\nEnter your choice:");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1: fifo();
                                                break;

                                    case 2: optimal();
                                                break;

                                    case 3: lru();
                                                break;

                        }
            }while(ch!=4);
            getchar();
}

void fifo()
{
            /*
            * i is use for loop
            * num is use for temp store
            * flag is 0 that means no fault if flag is 1 than fault occur
            * psize is use for frame to enter you want
            * count is use to count fault
            */
            int i,f,r,s,count,flag,num,psize;
            f=0;
            r=0;
            s=0;
            flag=0;
            count=0;
            printf("\nEnter size of page frame:");
            scanf("%d",&psize);//frame szie enter
            for(i=0;i<psize;i++)     //loop repeat itself according to psize variable
            {
                        fr[i]=-1; // all frame is equal to -1(NULL)
            }
            while(s<n)       //s=0 and n is page size that you enter first
            {
                        flag=0; // flag is 0
                        num=pg[s]; //all page is assinge to num variable
                        //loop start repeat itself according to framesize (psize) 
                        for(i=0;i<psize;i++)
                        {

                                    if(num==fr[i]) //checking page size if equal put it and no error occur(no falut)
                                    {
                                                s++;
                                                flag=1;
                                                break;
                                    }
                        }
            if(flag==0) //flag is zero  checking the page size is zero checking the framesize
            {
                        if(r<psize) //r = 0  and if framesize is greater than this exceute
                        {         
                                                fr[r]=pg[s]; //assinge fr[ number of frame] = pg[s ++ number of page]
                                                r++;
                                                s++;
                                                count++;//fault counting
                        }
                        else
                        {
                                                if(f<psize)  //f = 0 and psize
                                    {
                                                            fr[f]=pg[s]; //frame[f+1] and pg [s+1]
                                                            s++;
                                                            f++;
                                                            count++;//fault count+1
                                    }
                                    else
                                                f=0; //no exceute than f=0;
                                    }
                        }
                        printf("\n");                              //print the data
                        for(i=0;i<psize;i++)
                        {
                                    printf("%d\t",fr[i]);
                        }
            }
printf("\nPage Faults=%d",count);
getchar();
}

void optimal()
{
            /*
            * f get number of fargement to create.
            * i,x, j,cnt, m,k is use for loops
            * p = position of fragement
            */
            int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;
            fault=0; //fault count in variable store
            dist=0;
            k=0;
            printf("\nEnter frame size:");
            scanf("%d",&f);
            //initilizing distance and frame array  
            for(i=0;i<f;i++)
            {
                        count[i]=0; // all count array become zero.
                        fr[i]=-1;           //all fragement is become -1
            }
            for(i=0;i<n;i++)//first loop start
            {
                        flag=0;  // flag is 0
                        temp=pg[i]; //assige pg[] to temp variable
                        for(j=0;j<f;j++)                                   //second loop start
                        {
                                    if(temp==fr[j])//checking temp equal to fragement size if equal than assige.
                                    {
                                                flag=1; // page is successful allocated
                                                break; //return stop and start new page and put and find a suitable fragement.
                                    }
                        }                                  //second loop end
//start from 000000
                        if((flag==0)&&(k<f))    //flag is zero and (k =0;) less than f
                        {
                                    fault++; //fault occur add one
                                    fr[k]=temp; //fr[k =0] put into temp
                                    k++; //and k+1
                        }
//searching the frame that contain value.
                        else if((flag==0)&&(k==f)) // else if flag is 0 and k equal to f
                        {
                                    fault++;                                   //than also fault + 1
                                    for(cnt=0;cnt<f;cnt++)                                   //third loop start
                                    {                     
                                                current=fr[cnt];      //current = 0 and  fragement [loop var];
                                                for(c=i;c<n;c++) //fourth loop start
                                                {
                                                            if(current!=pg[c])   //current have fargement size
                                                                        count[cnt]++;    // count[loop var] +1;
                                                            else
                                                                        break;  //if not than break
                                                }        // fourth loop end
                                    }            //third loop end
                                    max=0; // max is equal to zero

                       

                                    for(m=0;m<f;m++) //loop fifth start
                                    {
                                                if(count[m]>max)  //count[loop var] less than max
                                                {
                                                            max=count[m];  //max = count[ loop var]
                                                            p=m;    //  p is variable equal to m variable
                                                }
                                    }
                                    fr[p]=temp; // p have the value from m that p have the position of fr[p] = temp to stre in temp vairable
                        }
                        printf("\n");                             
                        for(x=0;x<f;x++)// sixth loop start
                        {
                                    printf("%d\t",fr[x]); //print the f fragemtn size repeat that time.
                        }//sixth loop end
            }
            printf("\nTotal number of faults=%d",fault); // show the falut
            getchar();
}

void lru()
{
            /*
            * i,j,m,x,cnt use for loops
            * f use fragement generation
            * count[] array use for counting
            * fault counts
            */
            int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;
            fault=0;
            dist=0;
            k=0;
            printf("\nEnter frame size:");
            scanf("%d",&f);  //fargement size want to enter
            //initilizing distance and frame array  
            for(i=0;i<f;i++) //loop start
            {
                        count[i]=0; //all array becomes zero
                        fr[i]=-1;   //all fragement size become -1(NULL)
            }
            for(i=0;i<n;i++) //first loop start  n get value from start variable
            {
                        flag=0; // flag zero
                        temp=pg[i];  //temp have empty we put some value in pg[i] value which get from start
                        for(j=0;j<f;j++)    //second loop start                         
                        {
                                    if(temp==fr[j]) //temp value is equal to fragement size fr[j] each compare one by one
                                    {
                                                flag=1; //if this true flag is one than its break the loop
                                                break;//stop the loop
                                    }
                        }//second loop end
                        if((flag==0)&&(k<f))  //flag == 0 and  k=0 <f than this condition is exceute
                        {
                                    fault++; //count the fault that occurs in fargement which is not store.
                                    fr[k]=temp; //fragement[k]  store in temp
                                    k++; //k is a vairable which store a position of fragement replaced.
                        }
                        else if((flag==0)&&(k==f)) // flag is equal to zero and k (position size) == f
                        {
                                    fault++;                                   //count the fault
                                    for(cnt=0;cnt<f;cnt++) // third loop start                               
                                    {                     
                                                current=fr[cnt]; // current is variable which is equal to fragement[loop variable]
                                                for(c=i;c>0;c--) // fourth loop start
                                                {
                                                            if(current!=pg[c])  //current != page[loop variable c]
                                                                        count[cnt]++; // count [ loop variable c] +1
                                                            else
                                                                        break; //if the above condition is false than this exceute and break the loop(stop)
                                                }//fourth loop end
                                    }//third loop end
                                    max=0; // max variable is equal to zero
                                    for(m=0;m<f;m++) //fifth loop start
                                    {
                                                if(count[m]>max) //count[loop var m] > max (equal = 0)
                                                {
                                                            max=count[m]; // max  equal count[loop var m]
                                                            p=m; //p store the loop times
                                                }
                                    }
                                    fr[p]=temp; // fragement [loop time (P)]  put in temp vairable.
                        }
                        printf("\n");                             
                        for(x=0;x<f;x++)//sixth loop start
                        {
                                    printf("%d\t",fr[x]); //print all the page on list one by one
                        }
            }
            printf("\nTotal number of faults=%d",fault); //count the fault that occur in the in paging
            getchar();

}

Comments

Popular posts from this blog

SCHEMES OF STUDIES

Abasyn offers programs leading to the award of degrees in Management Sciences, Computer Science/IT, Education, Pharmacy, Microbiology, Engineering and B.Tech etc with specializations in the relevant program under the semester system. 1. The medium of instruction is English. 2. The two examinations will be held in the theory part of course as well as Practical, where required. A student will have to pass theory as well as practical examination simultaneously in Pharm-D program or any other science degree program, where applicable.

Data Communication And Networks by Behrouz A.Forouzan

Click here to Download

Ivor Horton's Beginning Java 7th Edition

Book Description Whether you want to learn  Java  to write customized applets to embed in your web pages or large-scale applications, this is the book for you. Ivor Horton's comprehensive and easy-to-use tutorial guide provides you with the essential know-how for developing programs using the latest Java Development Kit 7 (JDK 7). It integrates step-by-step examples with clear explanations of key concepts to clearly show you the ins and outs of Java development. As you progress through the chapters, you'll gain invaluable  programming  experience that will enable you to begin writing fully featured, real-world Java programs. Book Details Publisher: Wrox By: Ivor Horton ISBN: 978-1-4571-0686-6 Year: 2011 Pages: 1150 Language: English File size: 75.6 MB File format: PDF QR Code Click here to Download