存储管理-请求页式管理中页面置换算法模拟设计

目录

 

目标

具体设计:

要求

分析

代码实现

运行结果

算法命中率比较


 

目标

通过请求页式管理中页面置换算法模拟设计,计算并输出下属算法在不同内存容量下的命中率,比较算法优劣。

1)先进先出的算法(FIFO);

2)最近最少使用算法(LRU);

具体设计:

要求

a、 终端先显示:

Start memory management.

Producing address flow, wait for while, please.

b、 地址流、地址页号流生成后,终端显示:

There are algorithms in the program

1、 Optimization algorithm

2、 Least recently used algorithm

Select an algorithm number, please.

用户输入适当淘汰算法的号码,并按回车,若是第一次选择,输出相应的

地址页号流。然后输出该算法分别计算的用户内存从 2k~32k 时的命中率,

若输入的号码不再 1~2 中,则显示:

there is not the algorithm in the program,并重复 b。

c、 输出结果后,终端显示 “do you try again with anther algorithm(y/n)”。若键

入 y 则重复 b,否则结束

分析

使指令的地址按下述原则生成:

1) 50%的指令是顺序执行的

2)25%的指令是均匀分布在前地址部分

3)25%的指令是均匀分布在后地址部分

解决方案:

1) 在[0,319]的指令地址之间随机选取一起点 m;

2) 顺序执行一条指令,即执行地址为 m+1 的指令;

3) 在前地址[0,m]中随机选取一条指令并执行,该指令的地址为 m’;

4)顺序执行一条指令,地址为 m’+1 的指令

5) 在后地址[m’+2,319]中随机选取一条指令并执行;

6) 重复上述步骤 1)~5),直到执行 320 次指令

public void commandSequence() {
	Random rand=new Random();
		int i=0;	
		while(i<320) {
			int m=rand.nextInt(320);		
			num[i++]=m+1;
			int m1=rand.nextInt(m+1);
			num[i++]=m1;
			num[i++]=m1+1;
			if(m1<317) {
			int m2=rand.nextInt(317-m1)+m1+2;
			num[i++]=m2;}
		}

FIFO

该算法先淘汰的是最先进入内存的页面,考虑把进入内存的页面按进入时间先后次序链接成队列, 如果即将访问的数据在物理块中,跳过; 如果不在物理块中,顺序添到物理块中,缺页数+1;如果物理块满了,替换最先进的队头,缺页数+1;

public void FIFO(int msize) {
		/*
		 * 如果即将访问的数据在物理块中,跳过; 如果不在物理块中,添到物理块中,缺页数+1;如果物理块满了,替换最先进的,缺页数+1;
		 */
		
		LinkedList<Integer> list = new LinkedList<Integer>();
		
		//commandSequence();
		int misspage = 0;// 缺页数
		//int page[]=new int[32];
		for (int i = 0; i < page.length; i++) {

			if (list.contains(page[i])) {
				continue;
			} else {
				if (list.size() !=msize) {
					list.addLast(page[i]);
					misspage++;
				} else {
					list.removeFirst();
					list.addLast(page[i]);
					misspage++;
				}
			}
		}
		double targetpage=1.0-(misspage/10*1.0/32);
		System.out.println("内存空间(物理块)为" + msize + "时\n,缺页数为:" +misspage + "\n命中率为:" +targetpage + "\n");
	}

LRU

它淘汰的是最近最久未被访问的页面,还是先把他们先链接起来,如果即将要访问的数据在物理块内,标记其已存在的序列下标,删除它再在末尾重新添加;如果将要访问的数据不在物理块内,添加到物理块,缺页数+1;如果满了,替换最久未访问的头,缺页数+1;

public void LRU(int msize) {
		/*
		 * 如果即将要访问的数据在物理块内,标记其已存在的序列下标,删除它再在末尾重新添加
		 * 如果将要访问的数据不在物理块内,添加到物理块,缺页数+1;如果满了,替换最久未访问的
		 */
		
		int misspage = 0;
		LinkedList<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < page.length; i++) {
			if (list.contains(page[i])) {
				int flag = list.indexOf(page[i]);// 标记序列下标
				list.remove(flag);
				list.addLast(page[i]);
			} else {
				if (list.size() != msize) {
					list.addLast(page[i]);
					misspage++;
				} else {
					list.removeFirst();
					list.addLast(page[i]);
					misspage++;
				}
			}

		}
		double targetpage=1.0-(misspage*1.0/10/32);
		System.out.println("内存空间(物理块)为" + msize + "时\n,缺页数为:" + misspage + "\n命中率为:" +targetpage+ "\n");
		
	}

代码实现

package com.as.storage;

import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;

public class Manage {
	int num[] = new int[320];// 指令序列
	int page[] = new int[320];// 对应的页码
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int choice = 0;
		System.out.println("Start memory management \n Producing address flow ,wait for while,please.\n");
		Manage my = new Manage();
		my.commandSequence();
		
		while (true) {
			System.out.println(
					"There are algorithms in the program\n1、First in first out algorithm\n2、Least recently used algorithm\nSelect an algotithm number,please.(0-exit)\n");
			try {
				choice = sc.nextInt();
			} catch (InputMismatchException e) {
				System.out.println("输入数据格式有误,请重新输入!");
				sc.nextInt();
				continue;
			}
			switch (choice) {
			case 1:
				for (int msize = 4; msize <= 32; msize++) {
					my.FIFO(msize);
				}
				
				break;
			case 2:
				for (int msize = 4; msize <= 32; msize++) {
					my.LRU(msize);
				}
				break;
			default:
				System.out.println("there is not the algorithm in the program,please select the number again(1-4)");
				break;
			}
			System.out.println("do you try again with another algorithm(y/n)?");
			String cho;
			try{
				cho=sc.next().toLowerCase();
			}catch (InputMismatchException e) {
				System.out.println("输入数据格式有误,请重新输入!");
				sc.nextInt();
				continue;
			}
			if(cho.equals("n")) {
				sc.close();break;
			}else if(cho.equals("y")) {sc.nextLine();}
			else {System.out.println("无效输入,请输入y/n!!!");}

		}

	}
	

	public void commandSequence() {
	Random rand=new Random();
		int i=0;	
		while(i<320) {
			int m=rand.nextInt(320);		
			num[i++]=m+1;
			int m1=rand.nextInt(m+1);
			num[i++]=m1;
			num[i++]=m1+1;
			if(m1<317) {
			int m2=rand.nextInt(317-m1)+m1+2;
			num[i++]=m2;}
		}
		
		
		System.out.println("***************生成320个随机数据*****************");
		for (int j = 0; j < 320; j++) {
			System.out.print(num[j] + "\t");
			if ((j+1)% 10 == 0) {
				System.out.println();
			}
		}
		System.out.println("*********************对应的页码数************************");
		for (int k = 0; k < 320; k++) {
			page[k] = num[k] / 10;
			System.out.print(page[k] + "\t");
			if ((k+1) % 10 == 0) {
				System.out.println();
			}
		}

	}

	/**
	 * 先进先出
	 */
	public void FIFO(int msize) {
		/*
		 * 如果即将访问的数据在物理块中,跳过; 如果不在物理块中,添到物理块中,缺页数+1;如果物理块满了,替换最先进的,缺页数+1;
		 */
		
		LinkedList<Integer> list = new LinkedList<Integer>();
		
		//commandSequence();
		int misspage = 0;// 缺页数
		//int page[]=new int[32];
		for (int i = 0; i < page.length; i++) {

			if (list.contains(page[i])) {
				continue;
			} else {
				if (list.size() !=msize) {
					list.addLast(page[i]);
					misspage++;
				} else {
					list.removeFirst();
					list.addLast(page[i]);
					misspage++;
				}
			}
		}
		double targetpage=1.0-(misspage/10*1.0/32);
		System.out.println("内存空间(物理块)为" + msize + "时\n,缺页数为:" +misspage + "\n命中率为:" +targetpage + "\n");
	}

	/**
	 * 最近最少使用
	 */
	public void LRU(int msize) {
		/*
		 * 如果即将要访问的数据在物理块内,标记其已存在的序列下标,删除它再在末尾重新添加
		 * 如果将要访问的数据不在物理块内,添加到物理块,缺页数+1;如果满了,替换最久未访问的
		 */
		
		int misspage = 0;
		LinkedList<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < page.length; i++) {
			if (list.contains(page[i])) {
				int flag = list.indexOf(page[i]);// 标记序列下标
				list.remove(flag);
				list.addLast(page[i]);
			} else {
				if (list.size() != msize) {
					list.addLast(page[i]);
					misspage++;
				} else {
					list.removeFirst();
					list.addLast(page[i]);
					misspage++;
				}
			}

		}
		double targetpage=1.0-(misspage*1.0/10/32);
		System.out.println("内存空间(物理块)为" + msize + "时\n,缺页数为:" + misspage + "\n命中率为:" +targetpage+ "\n");
		
	}

}

运行结果

f6672d67e3684b54a3d9e1df00d33d00.png

 71d0dcac9c9e4842aeff796504d67c5f.png      bb3c2596810d4763b0fbb5b22ab75598.png

 f584f8e0e3b64414ac82f733a50fb0b0.png                    2cc9634e31324110bb5073e124387e4a.png

 

      aa20d55e8c9b449c849ffa5ca80a7a82.png                           739bab32086243a780806b336e401954.png

 

算法命中率比较

当用户的内存容量(物理块大小)增加时,命中率也随之增加,到内存容量32的时候,基本在90%的命中率。

比较下来内存容量在20左右以下的时候,FIFO命中率大多更高些,以上的话,LRU更高些;

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
假设每个可存放10条指令,分配给作业的内存块数为4。 用C语言语言模拟一个作业的执行过程,该作业共有320条指令, 即它的地址空间为32,目前它的所有都还未调入内存。在模拟过程,如果所访问的指令已在内存,则显示其物理地址,并转下一条指令。如果所访问的指令还未装入内存,则发生缺,此时需要记录缺的次数,并将相应调入内存。如果4个内存块均已装入该作业,则需要进行面置换,最后显示其物理地址,并转向下一条指令。在所有320条指令执行完毕后,请计算并显示作业运行过程发生的缺率。 置换算法:请分别考虑最佳置换算法(OPT)、先进先出(FIFO)算法和最近最久未使用算法(LRU)。 作业指令的访问次序按下述原则生成: 50%的指令是顺序执行的; 25%的指令是均匀分布在前地址部分; 25%的指令是均匀分布在后地址部分; 具体的实施方法是:    在[0,319]的指令地址之间随机选取一起点m;    顺序执行下一条指令,即执行地址序号为m+1的指令;    通过随机数,跳转到前地址部分[0,m+1]的某条指令处,其序号为m1;    顺序执行下一条指令,其地址序号为m1+1的指令;    通过随机数,跳转到后地址部分[m1+2,319]的某条指令处,其序号为m2;    顺序执行下一条指令,其地址序号为m2+1的指令; 重复跳转到前地址部分,顺序执行,跳转到后地址部分,顺序执行的过程直至执行320条指令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

42341352315125115

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值