操作系统实验 LRU算法

物联网_小D 2011-12-10 02:32:47
实验五 存储管理
相关定义
1、数据结构
(1)页面类型
typedef struct /*页面结构*/
{
int pn,pfn,time;
}pl_type;
其中pn为页面号,pfn为页帧号,time为访问时间
(2)页帧控制结构
struct pfc_struct{ /*页帧控制结构*/
int pn,pfn;
struct pfc_struct *next;
};
typedef struct pfc_struct pfc_type;
pfc_type pfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;

其中pfc_type pfc[total_vp]定义用户进程虚页控制结构
*freepf_head为空闲页帧头的指针
*busypf_head为忙页帧头的指针
*busypf_tail忙页帧尾的指针

2、函数定义
void initialize(int):初始化函数
void OPT(int):计算使用OPT算法时的命中率
void FIFO(int):计算使用FIFO算法时的命中率
void LRU(int):计算使用LRU算法时的命中率
void NRU(int):计算使用NRU算法时的命中率
void LFU(int):计算使用LFU算法时的命中率

3、变量定义
int a[total_instruction]:指令流数组
int diseffect:页面失效次数
int page[total_instruction]:每条指令所属页面号
int offset[total_instruction]:每页装入10条指令后取模运算得出的页内偏移地址
int total_pf:用户进程的内存页面数

参考程序:
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define INVALID -1

#define total_instruction 320 /*指令流长*/
#define total_vp 32 /*虚页长*/

typedef struct /*页面结构*/
{
int pn,pfn,time;
}pl_type;
pl_type pl[total_vp]; /*页帧结构数组*/

struct pfc_struct{ /*页帧控制结构*/
int pn,pfn;
struct pfc_struct *next;
};

typedef struct pfc_struct pfc_type;

pfc_type pfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;

int diseffect,a[total_instruction];
int page[total_instruction],offset[total_instruction];

void initialize(int);
void LRU(int);
从上述算法中任选3种

int main( )
{
int s,i;
/*由于每次运行时进程号不同,故可用来作为初始化随机数队列的“种子”*/
srand(10*getpid());
s=(float)319*rand( )/RAND_MAX+1;
for(i=0;i<total_instruction;i+=4) /*产生指令队列*/
{
a[i]=s; /*任选一指令访问点m*/
a[i+1]=a[i]+1; /*顺序执行一条指令*/
a[i+2]=(float)a[i]*rand( )/RAND_MAX; /*执行前地址指令m'(0-a[i]之间的数*/
a[i+3]=a[i+2]+1; /*顺序执行一条指令*/
s=(float)(318-a[i+2])*rand( )/RAND_MAX+a[i+2]+2;
}
for (i=0;i<total_instruction;i++) /*将指令序列变换成页地址流*/
{
page[i]=a[i]/10;
offset[i]=a[i]%10;
}
for(i=4;i<=32;i++) /*用户内存工作区从4个页帧到32个页帧*/
{
printf("%2d page frames ",i);
LRU(i);
printf("\n");
}
}

void initialize(int total_pf) /*初始化相关数据结构*/
{
int i;
diseffect=0; /*定义页面缺页次数变量*/
for(i=0;i<total_vp;i++)
{
pl[i].pn=i;
pl[i].pfn=INVALID;
pl[i].time=-1;
}
for(i=0;i<total_pf-1;i++)
{
pfc[i].next=&pfc[i+1];
pfc[i].pfn=i;
} /*建立pfc[i-1]和pfc[i]之间的链接*/
pfc[total_pf-1].next=NULL;
pfc[total_pf-1].pfn=total_pf-1;
freepf_head=&pfc[0]; /*空页面队列的头指针为pfc[0]*/
}

置换算法请自行补充完整

void LRU (int total_pf) /*最近最久未使用算法*/
{

}



望解答!!!
...全文
418 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
物联网_小D 2011-12-10
  • 打赏
  • 举报
回复
大哥,补充一下程序啊
zichen0422 2011-12-10
  • 打赏
  • 举报
回复
定义时间……倒了时间进程不做,就移除,换新的进程
树无影 2011-12-10
  • 打赏
  • 举报
回复
我写了个LRU的实现,仅供参考

/************************************************************************/
/* 实现LRU算法:
每次换掉a[i - 3 -num], a[i - 3 - num]就是从前向后的第3个最后访问的元素,因为中间可能有相同的,
故减去num。 */
/************************************************************************/


#include <stdio.h>

#define SUM_NUM 20

int FindIndex(int *BLCOK, int BLOCK_NUM, int data) //查找缓冲块中指定页面的下标
{
int i;

for (i = 0; i < BLOCK_NUM; i ++)
{
if (BLCOK[i] == data)
{
return i;
}
}

return -1;
}

int main()
{
int a[20] = {7, 0, 1, 2, 0, 3, 0, 4, 2,
3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
int BLOCK[3]; //缓冲块的存储
int i;
int change_num = 0; //记录换页之间的次数
int index;
int num = 0; //记录两次换页之间的命中次数

/*for (i = 0; i < 20; i ++)
{
scanf("%d", &a[i]);
}*/

for (i = 0; i < 3; i ++)
{
BLOCK[i] = a[i];
}

for (i = 3; i < 20; i ++)
{
if (FindIndex(BLOCK, 3, a[i]) != -1)
{
num ++; //记录两次换页之间的命中次数
continue;
}

//a[i - 3 - num]就是从前向后的第3个最后访问的元素,因为中间可能有相同的, 故减去num
index = FindIndex(BLOCK, 3, a[i - 3 - num]);

if (index != -1)
{
change_num ++;
num = 0;

BLOCK[index] = a[i];
}
}

printf("置换次数为%d,命中次数为%d\n", change_num, 20 - 3 - change_num);


return 0;
}

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧