Cache块替换算法LRU

来源:互联网 发布:windows 正版 编辑:程序博客网 时间:2024/06/08 12:05
#include"stdio.h"
#define N 10
#define M 4
typedef struct CacheUpdate
{
    int value;     //装入的序列号
    int state;   //是否装入
    int counter;
};
CacheUpdate Cache[M];
int table[M][N];   //用来保存整个cache更新的情况
char *state[N];
int sortNumbers[N];
//初使化数组table
void initialTable()
{
    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            table[i][j]=-1;
        }
    }
}
//初使化Cache组
void initialCache()
{
    for(int j=0;j<M;j++)
        Cache[j].value=-1;
}
//输入一组序列
void inputSortNumbers()
{
    printf("请输入一组访问序列:\n");
    for(int i=0;i<N;i++)
        scanf("%1d",&sortNumbers[i]);
}
//记录cache现在的元素
void recordCache(int i)
{
    for(int j=0;j<M;j++)
    {
            table[j][i]=Cache[j].value;
    }
}
//LRU替换算法
void updateCache()
{
    int i=0;
    while(i<N)
    {
        int j=0;
        while(j<M)
        {
            if(Cache[j].state==0&&Cache[j].value!=sortNumbers[i])
            {
                state[i]="装入";
                Cache[j].value=sortNumbers[i];
                Cache[j].state=1;
                Cache[j].counter=0;
                 recordCache(i);
                 i++;
                //更新其它块没有使用的时间
                int k=0;
                while(k<M)
                {
                    if(k!=j&&Cache[k].value!=-1)
                    {
                        Cache[k].counter++;
                    }
                    k++;
                }
                break;
            }
            if(Cache[j].value==sortNumbers[i])
            {
                state[i]="命中";
                Cache[j].counter=0;
                recordCache(i);
                i++;
                int k=0;
                while(k<M)
                {
                    if(k!=j&&Cache[k].value!=-1)
                    {
                        Cache[k].counter++;
                    }
                    k++;
                }
                break;
            }
            j++;
        }
        if(j==M)   //满了,考虑置换
        {
            int k=0;
            while(k<M)
            {
                if(Cache[k].value==sortNumbers[i])
                {
                    state[i]="命中";
                    recordCache(i);
                    Cache[k].counter=0;
                    i++;
                    int kk=0;
                    while(kk<M)
                    {
                        if(kk!=k)
                        {
                           Cache[kk].counter++;
                        }
                        kk++;
                    }
                    break;
                }
                k++;
            }
            if(k==M)    //未命中,考虑置换哪一块
            {
                int ii=0;
                int t=0;
                int max=Cache[ii].counter;
                ii++;
                while(ii<M)
                {
                    if(Cache[ii].counter>max)
                    {
                        max=Cache[ii].counter;
                        t=ii;
                    }
                    ii++;
                }
                Cache[t].value=sortNumbers[i];
                recordCache(i);
                state[i]="置换";
                Cache[t].counter=0;
                i++;
                //更新其它块的cache次数
                int x=0;
                while(x<M)
                {
                    if(x!=t)
                    {
                        Cache[x].counter++;
                    }
                    x++;
                }
            }
        }
    }
}
//输出字符序列
void printSqNumbers()
{
    printf("\t   ");
    for(int i=0;i<N;i++)
    printf(" %d     ",sortNumbers[i]);
    printf("\n");
}
//出输Cache块的替换过,命中,装入过程
void printCache()
{
   for(int i=0;i<M;i++)
    {
        printf("Cache块%d ",i);
        for(int j=0;j<N;j++)
        {
            printf("%3c    ",table[i][j]==-1?' ':table[i][j]+48);
        }
        printf("\n");
    }
}
//输出状态
void printState()
{
     printf("          ");
    for(int i=0;i<N;i++)
    printf("%s   ",state[i]);
    printf("\n");
}
int main()
{
    inputSortNumbers();
     printSqNumbers();
    initialTable();
    initialCache();
   updateCache();
    printCache();
    printState();
    return 0;
}
原创粉丝点击