#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:\n");
srand((unsigned)time(NULL));
for(int i=0;i<NUM;i++)
printf("%d ",pageList[i].id=random(TYPE));
printf("\n");
} 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\t");
}else
printf("%d\t",cache[i].id);
printf("\n");
}
} 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("\nFIFO:\n");
cacheClear();
int hitsum=0;
for(int i=0;i<NUM;i++){
if(LOG)
printf("input:%d\t",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\n",hitsum);
printf("PageFault:%d\n",NUM-hitsum);
}
printf("HitRate:%lf\n",ret=(double)hitsum/NUM);
return ret;
} double LRU(){//time:from last use to now
double ret;
if(LOG)
printf("\nLRU:\n");
cacheClear();
int hitsum=0;
for(int i=0;i<NUM;i++){
if(LOG)
printf("input:%d\t",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\n",hitsum);
printf("PageFault:%d\n",NUM-hitsum);
}
printf("HitRate:%lf\n",ret=(double)hitsum/NUM);
return ret;
} double NUR(){//time:notuse-0 use-1
double ret;
if(LOG)
printf("\nNUR:\n");
cacheClear();
int hitsum=0,clockcur=0;//cur of the cache
for(int i=0;i<NUM;i++){
if(LOG)
printf("input:%d\t",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\n",hitsum);
printf("PageFault:%d\n",NUM-hitsum);
}
printf("HitRate:%lf\n",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("\nOPT:\n");
cacheClear();
int hitsum=0;
for(int i=0;i<NUM;i++){
if(LOG)
printf("input:%d\t",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\n",hitsum);
printf("PageFault:%d\n",NUM-hitsum);
}
printf("HitRate:%lf\n",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("\n");
printf("FIFO:%lf\n",arr[0]/alltime);
printf("LRU:%lf\n",arr[1]/alltime);
printf("NUR:%lf\n",arr[2]/alltime);
printf("OPT:%lf\n",arr[3]/alltime);
return 0;
}

【操作系统】编程模拟FIFO,LRU,NUR,OPT页面置换算法的更多相关文章

  1. [Operate System & Algorithm] 页面置换算法

    页面置换算法是什么?我们看一下百度百科对页面置换算法给出的定义:在地址映射过程中,若在页面中发现所要访问的页面不在内存中,则产生缺页中断.当发生缺页中断时,如果操作系统内存中没有空闲页面,则操作系统必 ...

  2. 操作系统页面置换算法(opt,lru,fifo,clock)实现

    选择调出页面的算法就称为页面置换算法.好的页面置换算法应有较低的页面更换频率,也就是说,应将以后不会再访问或者以后较长时间内不会再访问的页面先调出. 常见的置换算法有以下四种(以下来自操作系统课本). ...

  3. (待续)C#语言中的动态数组(ArrayList)模拟常用页面置换算法(FIFO、LRU、Optimal)

    目录 00 简介 01 算法概述 02 公用方法与变量解释 03 先进先出置换算法(FIFO) 04 最近最久未使用(LRU)算法 05 最佳置换算法(OPT) 00 简介 页面置换算法主要是记录内存 ...

  4. 操作系统笔记(六)页面置换算法 FIFO法 LRU最近最久未使用法 CLOCK法 二次机会法

    前篇在此: 操作系统笔记(五) 虚拟内存,覆盖和交换技术 操作系统 笔记(三)计算机体系结构,地址空间.连续内存分配(四)非连续内存分配:分段,分页 内容不多,就不做index了. 功能:当缺页中断发 ...

  5. FIFO、LRU、OPT页面调度算法及样例

    网上非常多介绍3种页面置换算法的样例和过程是不对的, 本文依据<操作系统概念>第七版对三种算法做介绍,并给出正确的样例以验证算法. 一.FIFO先进先出页面置换算法,创建一个FIFO队列来 ...

  6. 操作系统 页面置换算法LRU和FIFO

    LRU(Least Recently Used)最少使用页面置换算法,顾名思义,就是替换掉最少使用的页面. FIFO(first in first out,先进先出)页面置换算法,这是的最早出现的置换 ...

  7. 虚存管理页面置换算法 — FIFO和RUL算法模拟实现

    本篇博文为追忆以前写过的算法系列第一篇(20081021) 温故知新 目的: 为了解决内存容量有限与多作业执行的冲突.运用了虚拟存储技术.能从逻辑上对内存进行扩充,达到扩充内存的效果.分页存储管理是实 ...

  8. 页面置换算法 - FIFO、LFU、LRU

    缓存算法(页面置换算法)-FIFO. LFU. LRU 在前一篇文章中通过leetcode的一道题目了解了LRU算法的具体设计思路,下面继续来探讨一下另外两种常见的Cache算法:FIFO. LFU ...

  9. 页面置换算法(最佳置换算法、FIFO置换算法、LRU置换算法、LFU置换算法)

    页面置换产生的原因是:分页请求式存储管理(它是实现虚拟存储管理的方法之一,其中一个特性是多次性-->多次将页面换入或换出内存) 效果最好的页面置换算法:最佳置换算法 比较常用的页面置换算法有:F ...

随机推荐

  1. WebRTC视频分辨率设置

    前面我们能够打开摄像头.getUserMedia()时会传入参数,在参数里我们可以指定宽高信息.通过宽高参数控制输出的视频分辨率. html 在页面上摆放一些元素,下面是主要部分 <div id ...

  2. Shell学习(二)——变量和基本数据类型

    参考博客: [1]LinuxShell脚本--变量和数据类型 [2]shell只读变量删除 一.变量 定义变量的语法 定义变量时,变量名和变量值之间使用"="分隔,并且等号两边不能 ...

  3. 节省内存的循环banner(一)

    循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...

  4. 字节数与字符数mysql_mysql里一个中文汉字占多少字节数?

    在mysql中,如果是latin1字符集下,一个中文汉字占2个字节数:如果是utf8字符集下,一个中文汉字占3个字节数:如果是gbk字符集下,一个中文汉字占2个字节数. mysql各字符集下汉字和字母 ...

  5. 在隐藏导航栏的控制器中,调用UIIMagePickerController,出现导航栏变透明的问题

    在隐藏导航栏的控制器中,调用UIIMagePickerController,出现导航栏变透明的问题 解决办法 #pragma mark - UIImagePickerController Delega ...

  6. OC-私有方法,构造方法,类的本质及启动过程

    总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...

  7. 【Spring Framework】spring管理自己new的对象

    使用AutowireCapableBeanFactory手动注入 使用.newInstance();创建对象的话,如果其他对象都使用Spring Autowired,还需要手动创建所有依赖的Bean: ...

  8. 启动Springboot 报错 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jan 12 15:50:25 CST 2019 There was an unexpected error (type=Not

    解决方案:http://www.cnblogs.com/michaelShao/p/6675186.html

  9. 声临其境,轻松几步教你把音频变成3D环绕音

    在音乐创作.音视频剪辑和游戏等领域中,给用户带来沉浸式音频体验越来越重要.开发者如何在应用内打造3D环绕声效?华为音频编辑服务6.2.0版本此次带来了空间动态渲染功能,可以将人声.乐器等音频元素渲染到 ...

  10. Mysql资料 锁机制

    目录 一.简介 二.类型 三.操作 四.死锁 第一种情况 第二种情况 第三种情况 一.简介 数据库和操作系统一样,是一个多用户使用的共享资源.当多个用户并发地存取数据 时,在数据库中就会产生多个事务同 ...