页面置换算法----LRU(淘汰最近最久未使用过的页)

在这里插入图片描述
在这里插入图片描述




Linux—Ubuntu环境下C语言实现

/**************************************************
 page.c
--------------------------------------------------
 存储管理:页面置换算法模拟程序
 给出访问串,计算LRU算法下的缺页率
--------------------------------------------------
LRU算法的计时法
--------------------------------------------------
 测试数据:7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1
 测试数据:1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
***************************************************/

#include <stdio.h>
#include <stdlib.h>

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

#ifndef NULL
#define NULL 0
#endif

#define INVALID    -1  //-1表示缺页

页表结构类型
typedef struct pl_type {
    int pn; //页号
    int fn; //页面号
    int time;  //访问时间
    int dist;  //下次访问离当前页的距离
}pl_type;

//页面链结构类型
typedef struct fl_type {
    int pn; //页号
    int fn; //页面号
    struct fl_type *next; //链接指针
}fl_type;

//结构变量
pl_type pl[512]; //页表
fl_type fl[512],*free_head,*busy_head,*busy_tail; //页面

int page[512];  //访问串(访问的页号序列)
int total_pages; //访问串长度
int diseffect;  //缺页数

//初始化函数
//形参frame_number为分配给用户进程的内存页面数
void initialize(int frame_number)
{
    int i;
    diseffect=0; //页故障数初始化为0
   
    //建立空页表
    for(i=0;i<512;i++) {
        pl[i].pn=i;       //页号
        pl[i].fn=INVALID; //页面号为空,(开始时,页还未装入到页面)
        pl[i].time=-1;    //时间为-1 (初始化为-1)
    }
   
    //建立空闲页面链
    for(i=1;i<frame_number;i++) {
        fl[i-1].next=&fl[i]; //建立fl[i-1]和fl[i]间的链接
        fl[i-1].fn=100+i-1;//页面号由系统分配,可以是其他值,例如从100开始,fl[i-1].fn=100+i-1;
    }
    fl[frame_number-1].next=NULL;  //链表末尾为空指针
    fl[frame_number-1].fn=100+frame_number-1; //末尾结点的页面号
    free_head=&fl[0];  //空页面队列的头指针指向fl[0]
}

void LRU_time(int frame_number)
{
    int i;
    int current_time = 0,min_time; //从0开始计时
    fl_type *cur,*a;
    initialize(frame_number);  //初始化页表和页面链
    busy_head=free_head;
   
    for(i=0; i<total_pages; i++) {
        if(pl[page[i]].fn==INVALID) { //查页表,若缺页
            diseffect++; //记录缺页数
            if(free_head==NULL){ //若无空闲页面
               cur = busy_head;
               a = busy_head;
               min_time = pl[busy_head->pn].time;
               while(a != NULL)   //遍历忙页框链,找计时最小的页框结点
                {
                    if(pl[a->pn].time < min_time)
                    {   
                         min_time = pl[a->pn].time; 
                         cur = a;
                    }
                       
                     a = a->next;   
                }                     
                       pl[cur->pn].fn=INVALID; //淘汰计时最小的页
                       cur->pn=page[i]; //忙页框装入新页
                       pl[page[i]].fn=cur->fn; //修改页表
                    } else { //若有空闲页框
                           free_head->pn=page[i];       //空闲页框装入新页
                           pl[page[i]].fn=free_head->fn;//修改页表:记录装入的页号
                           free_head=free_head->next;//空闲页框链头指针移到下一结点
                           }
         } 
           pl[page[i]].time = current_time ;//页表中相应页记录当前时间
           current_time++;//时间嘀嗒一下               
     } 


    //打印结果
    printf("LRU_time: %02d次(%.02f%%)  ", diseffect, 100.0*diseffect/total_pages);   
}


//输入访问串
void Input_reference_string(void)
{
    int i;
   
    printf("输入的访问串,页号间以空格分开。例如: 7 0 2 6\n");
    printf("请输入访问串: ");
    for(i=0;i<512;i++) {
        if(scanf("%d", &page[i])!=1) break;
        if(getchar()==0x0a && i>0) break;
    }
    total_pages=i+1;  //访问串大小
   
    printf("访问串大小:%d\n访问串为: ", total_pages);
    for(i=0;i<total_pages;i++) printf("%d ", page[i]);
    printf("\n\n");
}

int main()
{
    int frames;
   
    system("color f0");
    Input_reference_string();
    printf("缺页数(缺页率)为\n");
    for(frames=2;frames<=total_pages;frames++){ //从2个页面到total_pages个页面
        printf(" 分配%2d个页面时,", frames);
	LRU_time(frames);
        printf("\n");
    }
    return 0;
}


程序运行结果截图:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值