Operating system paging storage algorithm - page replacement

FIFO, LRU page replacement algorithm

In both source programs, the following data is defined:

question:

The FIFO algorithm always eliminates the page that enters the memory first, that is, it selects the page that has resided in the memory for the longest time
and eliminates it. The LRU algorithm always eliminates the page that has not been used for the longest time, that is, it selects the page that has not been visited for the longest time
and eliminates it.

#define InitPysiBlocks 3
#define MaxPages 20
int PysicalBlocks[InitPysiBlocks] = {
    
     -1,-1,-1 };
int PageSequence[30] = {
    
     7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};

Carry out algorithm design according to the FIFO and LRU algorithm descriptions in the textbook

void FIFO(int py[],int pg[])//参数可以是指针
void LRU(int py[],int pg[])//参数可以是指针

FIFO algorithm

#include <stdio.h>

#define InitPysiBlocks 3
#define MaxPages 20


void main()
{
    
    
    void FIFO(int py[],int pg[]);
    int PysicalBlocks[InitPysiBlocks] = {
    
    -1,-1,-1};
    int PageSequence[20] = {
    
     7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
    FIFO(PysicalBlocks,PageSequence);
}

void FIFO(int py[],int pg[])
{
    
    

    int *flag=py;
    int Same;
    int j,i,k,max=0,m=0;
    printf("内存块:\n");
    printf("块1 块2 块3\n");

    //对3个物理块进行初始化
    for(i=0;i<20;i++)
    {
    
    
        for(j=0;j<3;j++)
        {
    
    
            if(py[j]==-1)// 判断是块否空
            {
    
    
                py[j]=pg[i];
                printf(" %d  ",py[i++]);
                if(j==2) printf("\n");
                continue;
            }
        }
        //确定所有物理块是否都满
        for(j=0;j<3;j++)
        {
    
    
            if(py[j]!=-1);
        }

        if(j==3)//块满后,扫描的此页如果块中有,直接扫描下一页,如果没有,则进行置换
        {
    
    
            Same=0;
            for(j=0;j<3;j++)//块满是否需要置换,Same=1,不需要置换
            {
    
    
                if(py[j]==pg[i])
                {
    
    
                    Same=1;
                    break;
                }
            }
            if(Same==1)
            {
    
    
                for(j=0;j<3;j++)//块满,存在相同页面,直接扫描下一页
                {
    
    
                    break;
                }
            }
            else//块满,不存在相同页面,发生置换
            {
    
    
                *flag = pg[i];
                if(flag==&py[InitPysiBlocks - 1])
                    flag = &py[0];
                else
                    flag++;
                printf("%2d  %2d  %2d 发生置换\n", py[0], py[1], py[2]);
                continue;
            }
        }
    }
}

result:

Insert image description here

LRU algorithm

#include <stdio.h>

#define InitPysiBlocks 3
#define MaxPages 20

void main()
{
    
    
    void LRU(int py[],int pg[]);
    int PysicalBlocks[InitPysiBlocks] = {
    
    -1,-1,-1};//对内存初始化-1表示空
    int PageSequence[20] = {
    
     7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
    LRU(PysicalBlocks,PageSequence);
}


void LRU(int py[],int pg[])
{
    
    
    int Same;
    int i,j,k,p,n,min;
    int flag[3],f;
    printf("内存块:\n");
    printf("块1 块2 块3\n");

    for(i=0;i<20;i++)//访问序列
    {
    
    
        n=0;

        for(k=0;k<3;k++)
        {
    
    
            flag[k]=0;
        }
        for(j=0;j<3;j++)
        {
    
    
            if(py[j]==-1)// 判断是块否空
            {
    
    
                py[j]=pg[i];
                printf(" %d  ",py[j]);
                if(j==2) printf("\n");
                break;
            }
        }
        //确定所有物理块是否都满
        for(j=0;j<3;j++)
        {
    
    
            if(py[j]!=-1);
        }
        if(j==3)//块满后,扫描的此页如果块中有,直接扫描下一页,如果没有,则进行置换
        {
    
    
            Same=0;
            for(j=0;j<3;j++)//块满是否需要置换,Same=1,不需要置换
            {
    
    
                if(py[j]==pg[i])
                {
    
    
                    Same=1;
                    break;
                }
            }
            if(Same==1)
            {
    
    
                for(j=0;j<3;j++)//块满,存在相同页面
                {
    
    
                    if(py[j]==pg[i])
                        break;
                }
            }
            else	//块满,不存在相同页面,发生置换
            {
    
    
                //找出最久未使用的块
                for (f = i ;f >= 0; f--) {
    
    
                    if(pg[f] == py[0]) flag[0]=1;
                    if(pg[f] == py[1]) flag[1]=1;
                    if(pg[f] == py[2]) flag[2]=1;
                    if(flag[0]+flag[1]+flag[2]==2){
    
    
                        min = 0;
                        for (n=0; n <3; n++){
    
    
                            if(flag[n]==0){
    
    
                                min = n;
                                break;
                            }
                        }
                    }
                }
                py[min] = pg[i];
                //输出置换后的内存
                printf("%2d  %2d  %2d 发生置换\n", py[0], py[1], py[2]);
                p++;
                continue;
            }

        }

    }

}

result:

Insert image description here

Guess you like

Origin blog.csdn.net/Systemmax20/article/details/124757253