LRU Page Replacement

#include<stdio.h>
#include<conio.h>

int frame[20];
int count[20];
int s[100];

int max(int n)
{
    int i=0, max=count[0], loc=0;
    for(i=0;i<n;i++)
    {
        if(max<count[i])
        {
            max=count[i];
            loc=i;
        }
    }
    return loc;
}

void main()
{
    int i=0, j=0, k=0, f=0, n=0, hit=0, t;
    printf("Enter frame size: ");
    scanf("%d", &f);
    printf("Enter no. of pages: ");
    scanf("%d", &n);
    printf("Enter Page Replacement string:\n");
    for(i=0;i<n;i++)
        scanf("%d", &s[i]);
    printf("\nLRU Page Replacement Policy:\n");
    for(i=0;i<f;i++)
    {
        frame[i]=s[i];
        count[i]++;
        for(j=0;j<=i;j++)
            printf("%d ", frame[j]);
        printf("\n");
    }
    for(i=f;i<n;i++)
    {
        for(j=0;j<f;j++)
        {
            count[j]++;
            if(s[i]==frame[j])
            {
                hit++;
                count[j]=1;
                for(k=0;k<f;k++)
                    printf("%d ", frame[k]);
                printf("H\n");
            }
        }
        if(k==f)
        {
            k=0;
            continue;
        }
        t=max(f);
        frame[t]=s[i];
        count[t]=1;
        for(j=0;j<f;j++)
            printf("%d ", frame[j]);
        printf("\n");
    }
    printf("\nHits %d and Percentage of hits %d%", hit, hit*100/n);
    printf("\nMiss %d and Percentage of miss %d%", n-hit, (n-hit)*100/n);
}

/*Output
Enter frame size: 3
Enter no. of pages 20
Enter Page Replacement string
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

LRU Page Replacement Policy:
1
1 2
1 2 3
4 2 3
4 2 3 H
4 2 1
5 2 1
5 6 1
5 6 2
1 6 2
1 6 2 H
1 3 2
7 3 2
7 3 6
7 3 6 H
2 3 6
2 3 1
2 3 1 H
2 3 1 H
2 3 6

Hits 5 and Percentage of hits 25
Miss 15 and Percentage of miss 75
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s