• 【操作系统】编程模拟FIFO,LRU,NUR,OPT页面置换算法


    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define random(x) (rand()%x)
    
    #define LOG     1        //1-show log    2-no show
    #define TYPE 10     //page types
    #define NUM 20         //page nums
    #define SIZE 5        //cache size
    
    struct page{
        int id;//page id
        int time=0;//different meaning in different algorithm
    };
    struct page pageList[NUM],cache[SIZE];//page needs,page cache
    
    void init(){//random data init
        printf("PageList:
    ");
        srand((unsigned)time(NULL));
        for(int i=0;i<NUM;i++)
            printf("%d ",pageList[i].id=random(TYPE));
        printf("
    ");
    }
    
    void cacheClear(){//clear the cache
        for(int i=0;i<SIZE;i++){
            cache[i].id=-1;
            cache[i].time=0;    
        }
    }
    
    int cacheHit(int i){//find cache hit or not
        for(int j=0;j<SIZE;j++)
            if(cache[j].id==pageList[i].id){
                if(LOG) 
                    printf(" hit ");
                return j;
            }
        if(LOG) 
            printf("     ");
        return -1;
    }
    
    int cacheRoom(){//find cache have free room or not
        for(int i=0;i<SIZE;i++)
            if(cache[i].id==-1)
                return i;
        return -1;
    }
    
    void cacheShow(){//show the cache pages
        if(LOG) {
            printf("cache: ");
            for(int i=0;i<SIZE;i++)
                if(cache[i].id==-1){
                    printf("N	");                
                }else
                    printf("%d	",cache[i].id);
            printf("
    ");
        } 
    }
    
    void cacheTimeAdd(){//add the pages time in cache
        for(int i=0;i<SIZE;i++)
            cache[i].time++;
    }
    
    int FIFOfun(){//FIFO replace
        int maxtime=cache[0].time,t=0;
        for(int i=1;i<SIZE;i++)
            if(maxtime<cache[i].time){
                maxtime=cache[i].time;
                t=i;
            }
        return t;
    }
    
    double FIFO(){//time in FIFO:live time
        double ret;
        if(LOG) 
            printf("
    FIFO:
    ");
        cacheClear();
        int hitsum=0;
        for(int i=0;i<NUM;i++){
            if(LOG) 
                printf("input:%d	",pageList[i].id);
            int hit=cacheHit(i);
            if(hit==-1){//if not hit
                int room=cacheRoom();
                if(room!=-1)// if have free room
                    cache[room]=pageList[i];//use room save page
                else//if have not free room 
                    cache[FIFOfun()]=pageList[i];//use FIFO to replace
            }else{//if hit
                hitsum++;    
            }
            cacheShow();
            cacheTimeAdd();
        }
        if(LOG) {
            printf("PageReplacement:%d
    ",hitsum);
            printf("PageFault:%d
    ",NUM-hitsum);    
        }
        printf("HitRate:%lf
    ",ret=(double)hitsum/NUM);
        return ret;
    }
    
    double LRU(){//time:from last use to now
        double ret;
        if(LOG)
            printf("
    LRU:
    ");
        cacheClear();
        int hitsum=0;
        for(int i=0;i<NUM;i++){
            if(LOG)
                printf("input:%d	",pageList[i].id);
            int hit=cacheHit(i);
            if(hit==-1){//if not hit
                int room=cacheRoom();
                if(room!=-1)//if have free room
                    cache[room]=pageList[i];//use free room to save page
                else//if have not free room
                    cache[FIFOfun()]=pageList[i];//use LRU ,because time have different meaning ,function is same as FIFO 
            }else{//if hit 
                hitsum++;
                cache[hit].time=0;//LRU:every hit reflash time 
            }
            cacheShow();
            cacheTimeAdd();
        }
        if(LOG){
            printf("PageReplacement:%d
    ",hitsum);
            printf("PageFault:%d
    ",NUM-hitsum);    
        }
        printf("HitRate:%lf
    ",ret=(double)hitsum/NUM);
        return ret;
    }
    
    double NUR(){//time:notuse-0 use-1
        double ret;
        if(LOG)
            printf("
    NUR:
    ");
        cacheClear();
        int hitsum=0,clockcur=0;//cur of the cache 
        for(int i=0;i<NUM;i++){
            if(LOG)
                printf("input:%d	",pageList[i].id);
            int loop=1,ishit=0;
            for(int j=0;j<SIZE;j++){  //first loop to find hit or not
                if(cache[j].id==pageList[i].id){
                    clockcur=j;
                    cache[clockcur].time=1;
                    clockcur=(clockcur+1)%SIZE;
                    ishit=1;
                    loop=0;// if hit ,there not next loop 
                    break;
                }
            }
            while(loop){//next loop to find one to replace
                loop=0;
                if(cache[clockcur].id==-1){ //id==-1,free room,loop end
                    cache[clockcur]=pageList[i];
                    cache[clockcur].time=1;
                }else if(cache[clockcur].time==0){//time==0,replace,loop end
                    cache[clockcur]=pageList[i];
                    cache[clockcur].time=1;
                }else{ //time==1,change time to 0,loop continue
                    cache[clockcur].time=0;
                    loop=1; 
                }
                clockcur=(clockcur+1)%SIZE;
            }
            if(ishit){//show hit
                hitsum++;
                if(LOG)
                    printf(" hit ");
            }else{
                if(LOG)
                    printf("     ");    
            }
            cacheShow();
        }
        if(LOG){
            printf("PageReplacement:%d
    ",hitsum);
            printf("PageFault:%d
    ",NUM-hitsum);
        }    
        printf("HitRate:%lf
    ",ret=(double)hitsum/NUM);
        return ret;
    }
    
    int OPTfun(int i){//OPT replace
        int arr[SIZE];//save cache page next use
        for(int j=0;j<SIZE;j++){
            arr[j]=INT_MAX;
            for(int k=i+1;k<NUM;k++){
                if(cache[j].id==pageList[k].id){
                    arr[j]=k;
                    break;
                }
            }
        }
        int arrmax=arr[0],t=0;//find the longest next use 
        for(int j=1;j<SIZE;j++){
            if(arr[j]>arrmax){
                arrmax=arr[j];
                t=j;
            }
        }
        return t;
    } 
    
    double OPT(){
        double ret;
        if(LOG)
            printf("
    OPT:
    ");
        cacheClear();
        int hitsum=0;
        for(int i=0;i<NUM;i++){
            if(LOG)
                printf("input:%d	",pageList[i].id);
            int hit=cacheHit(i);
            if(hit==-1){//if not hit
                int room=cacheRoom();
                if(room!=-1)//if have free room 
                    cache[room]=pageList[i];//use free room to save
                else// not free room 
                    cache[OPTfun(i)]=pageList[i];//use OPT replace
            }else{//if hit 
                hitsum++;
            }
            cacheShow();
            cacheTimeAdd();
        }
        if(LOG){
            printf("PageReplacement:%d
    ",hitsum);
            printf("PageFault:%d
    ",NUM-hitsum);    
        }    
        printf("HitRate:%lf
    ",ret=(double)hitsum/NUM);
        return ret;
    }
    
    int main(){
        int    alltime=LOG?1:100;
        double arr[4]={0,0,0,0};
        for(int i=0;i<alltime;i++){
            init();
            arr[0]+=FIFO();
            arr[1]+=LRU();
            arr[2]+=NUR();
            arr[3]+=OPT();
        }
        printf("
    ");
        printf("FIFO:%lf
    ",arr[0]/alltime);
        printf("LRU:%lf
    ",arr[1]/alltime);
        printf("NUR:%lf
    ",arr[2]/alltime);
        printf("OPT:%lf
    ",arr[3]/alltime); 
        return 0;
    }
  • 相关阅读:
    Linux课程实践一:Linux基础实践(SSH)
    《恶意代码分析实战》读书笔记 静态分析高级技术一
    Linux课程实践四:ELF文件格式分析
    Linux课程实践三:简单程序破解
    Linux课程实践二:编译模块实现内核数据操控
    2020.12.19 加分项和课程意见/建议
    博客已换
    [题解] LuoguP4983 忘情
    [题解] LuoguP4767 [IOI2000]邮局
    [题解] LuoguP2791 幼儿园篮球题
  • 原文地址:https://www.cnblogs.com/LPworld/p/14124390.html
Copyright © 2020-2023  润新知