FIFO Page Replacement

#include<stdio.h>

void fifo(int a[],int n,int f)
{
    int hit=0,i,j,m,k=0,c[100];
    float percent;
    for(i=0;i<n;i++)
    {
        c[i]=999;
    }
    printf("\nFIFO page replacement:\n");
    for(i=0;i<n;i++)
    {
        if(k==f)
            k=0;
        for(j=0;j<f;j++)
        {
            if(c[j]==a[i])
            {
                hit++;
                for(m=0;m<f;m++)
                {
                    if(c[m]!=999)
                        printf("%d ",c[m]);
                }
                printf("H\n");
                break;
            }
        }
        if(j==f)
        {
            c[k]=a[i];
            k++;
            for(m=0;m<f;m++)
            {
                if(c[m]!=999)
                    printf("%d ",c[m]);
            }
            printf("\n");
        }
    }
    printf("\nNo. of hits = %d and No. of miss = %d",hit,(n-hit));
    percent=((float)hit/n)*100;
    printf("\nPercent hits = %.2f and Percent miss = %.2f",percent,(100-percent));
}

void main()
{
    int n,f,i,j,k,a[100];
    printf("Enter frame size: ");
    scanf("%d",&f);
    printf("Enter no. of pages: ");
    scanf("%d",&n);
    printf("Enter pages: ");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    fifo(a,n,f);
}

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

FIFO page replacement:
1
1 2
1 2 3
4 2 3
4 2 3 H
4 1 3
4 1 5
6 1 5
6 2 5
6 2 1
6 2 1 H
3 2 1
3 7 1
3 7 6
3 7 6 H
2 7 6
2 1 6
2 1 6 H
2 1 3
6 1 3

No. of hits = 4 and No. of miss = 16
Percent hits = 20.00 and Percent miss = 80.00
*/

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