《操作系统》实验二.docx

上传人:b****7 文档编号:9459811 上传时间:2023-02-04 格式:DOCX 页数:21 大小:1.05MB
下载 相关 举报
《操作系统》实验二.docx_第1页
第1页 / 共21页
《操作系统》实验二.docx_第2页
第2页 / 共21页
《操作系统》实验二.docx_第3页
第3页 / 共21页
《操作系统》实验二.docx_第4页
第4页 / 共21页
《操作系统》实验二.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

《操作系统》实验二.docx

《《操作系统》实验二.docx》由会员分享,可在线阅读,更多相关《《操作系统》实验二.docx(21页珍藏版)》请在冰豆网上搜索。

《操作系统》实验二.docx

《操作系统》实验二

实验二存储管理

1、实验目的

通过模拟实现内存分配的伙伴算法和请求页式存储管理的几种基本页面置换算法,了解存储技术的特点。

掌握虚拟存储请求页式存储管理中几种基本页面置换算法的基本思想和实现过程,并比较它们的效率。

2、实验内容

◆实现一个内存管理的伙伴算法,实现内存块申请时的分配和释放后的回收。

◆设计一个虚拟存储区和内存工作区,并使用下述算法计算访问命中率。

1)最佳置换算法(Optimal)

2)先进先出法(FisrtInFirstOut)

3)最近最久未使用(LeastRecentlyUsed)

4)最不经常使用法(LeastFrequentlyUsed)

5)最近未使用法(NoUsedRecently)

其中,命中率=1-页面失效次数/页地址流长度。

试对上述算法的性能加以较各:

页面个数和命中率间的关系;同样情况下的命中率比较。

3、实验分析

◆对于伙伴算法,用随机函数仿真进程进行内存申请,并且以较为随机的次序进行释放。

对其碎片进行统计,当申请分配内存失败时区分实际空间不足和由于碎片而不能满足。

◆对于虚拟存储区和内存工作区的不同算法,其中主要的流程:

首先用srand()和rand()函数定义和产生指令序列,然后将指令序列变换成相应的页地址流,并针对不同的算法计算出相应的命中率。

实验可先从一个具体的例子出发。

1)通过随机数产生一个指令序列,共320条指令。

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

A:

50%的指令是顺序执行的

B:

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

C:

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

具体的实施方法是:

A:

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

B:

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

C:

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

D:

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

E:

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

F:

重复步骤A-E,直到320次指令

2)将指令序列变换为页地址流

设:

页面大小为1K;

用户内存容量4页到32页;

用户虚存容量为32K。

在用户虚存中,按每K存放10条指令排列虚存地址,即320条指令在虚存中的存放方式为:

第0条-第9条指令为第0页(对应虚存地址为[0,9])

第10条-第19条指令为第1页(对应虚存地址为[10,19])

………………………………

第310条-第319条指令为第31页(对应虚存地址为[310,319])

按以上方式,用户指令可组成32页。

4、实验代码

1、内存分配与释放:

#include 

#include 

#include 

#define MEMORY 2048

int available[100], avaiNum = 0, avaiMemory = MEMORY;

int fragment[100], fragNum = 0;

typedef struct MemoryTree{

struct MemoryTree *left;

struct MemoryTree *right;

struct MemoryTree *father;

int tab;

int thisMemory;

int used;

}TreeNode, *Mtree;

int AlloMemory(int toBeAllo, Mtree root)

{

if(toBeAllo > avaiMemory){

return 0;

}else if(toBeAllo > root->thisMemory){

return -1;

}else if(toBeAllo > root->thisMemory/2){

if((root->left == NULL) && (root->tab == 0)){

root->tab = 1;

root->used = toBeAllo;

return 1;

}else

return -1;

}else if(root->tab == 1){

return -1;

}else{

if(root->left == NULL){

root->left = (Mtree)malloc(sizeof(TreeNode));

root->left->tab = 0;

root->left->thisMemory = root->thisMemory/2;

root->left->used = 0;

root->left->left = NULL;

root->left->right = NULL;

root->left->father = root;

root->right = (Mtree)malloc(sizeof(TreeNode));

root->right->tab = 0;

root->right->thisMemory = root->thisMemory/2;

root->right->used = 0;

root->right->left = NULL;

root->right->right = NULL;

root->right->father = root;

}

if(AlloMemory(toBeAllo, root->left) !

= 1)

return AlloMemory(toBeAllo, root->right);

else

return 1;

}

}

void ReleaseMemory(int toBeRele, Mtree root)

{

Mtree temp;

if(root !

= NULL){

ReleaseMemory(toBeRele, root->left);

ReleaseMemory(toBeRele, root->right);

if(root->left == NULL){

if((root->tab == 0) && (root->father !

= NULL)){

if((root->father->left->tab == 0) && (root->father->right->tab == 0) && (root->father->left->left == NULL) && (root->father->right->left == NULL)){

temp = root->father;

temp->left = NULL;

temp->right = NULL;

}

}else if((root->tab == 1) && (root->used == toBeRele)){

root->used = 0;

root->tab = 0;

if((root->father->left->tab == 0) && (root->father->right->tab == 0) && (root->father->left->left == NULL) && (root->father->right->left == NULL)){

temp = root->father;

temp->left = NULL;

temp->right = NULL;

}

void Delete(int a[], int num)

{

int i = num;

while(a[i] !

= '\0'){

a[i] = a[i+1];

i++;

}

}

void ScanMemory(Mtree root)

{

if(root->left == NULL){

if(root->tab == 0)

available[avaiNum++] = root->thisMemory;

else

fragment[fragNum++] = root->thisMemory - root->used;

}else{

ScanMemory(root->left);

ScanMemory(root->right);

}

}

int main()

{

int times, i, j, n, releNum;

int allocated, released;

int label; //表明分配后的结果

int haveAllo[256];

Mtree init = NULL;

printf("The size of memory is:

%d.\nPlease input the times you will allocate the memory:

", MEMORY);

scanf("%d", ×);

n = times;

releNum = 0;

init = (Mtree)malloc(sizeof(TreeNode));

init->tab = 0;

init->thisMemory = MEMORY;

init->used = 0;

init->left = NULL;

init->right = NULL;

init->father = NULL;

srand(time(NULL));

for(i = 0; i < times; i++){

allocated = rand() % 600;

printf("The size of memory to be allocated :

 %d\n", allocated);

label = AlloMemory(allocated, init);

ScanMemory(init);

avaiMemory = avaiMemory - allocated;

fragment[fragNum] = '\0';

available[avaiNum] = '\0';

if(label == 1){

haveAllo[releNum++] = allocated;

printf("Allocation succeed!

\n");

j = 0;

if(available[0] == '\0')

printf("There is no available memory.");

else{

printf("Now,the memory available are:

\n")

while(available[j] !

= '\0')

printf("%d ",available[j++]);

}

avaiNum = 0;

printf("\n");

j = 0;

if(fragment[0] == '\0')

printf("There is no fragment");

else{

printf("The framgment are:

\n");

while(fragment[j] !

= '\0')

printf("%d ",fragment[j++]);

}

printf("\n");

fragNum = 0;

}else if(label == 0){

n--;

printf("Allocation failed!

Short of memory!

\n");

}else{

n--;

printf("Allocation failed!

Too many fragment!

\n");

}

}

haveAllo[i] = '\0';

printf("\n\n\nNow,release!

!

\n");

times = n;

for(i = 0; i < times; i++){

releNum = rand() % n;

n--;

released = haveAllo[releNum];

Delete(haveAllo, releNum);

ReleaseMemory(released, init);

ScanMemory(init);

avaiMemory = avaiMemory + released;

fragment[fragNum] = '\0';

available[avaiNum] = '\0';

printf("The memory to be released is:

 %d\nReleasion succeed!

\n", released);

j = 0;

if(available[0] == '\0')

printf("There is no available memory.");

else{

printf("Now,the memory available are:

\n");

while(available[j] !

= '\0')

printf("%d ",available[j++]);

}

avaiNum = 0;

printf("\n");

j = 0;

if(fragment[0] == '\0')

printf("There is no fragment");

else{

printf("The framgment are:

\n");

while(fragment[j] !

= '\0')

printf("%d ",fragment[j++]);

}

printf("\n");

fragNum = 0;

}

system("pause");

}

2、页面置换算法:

#include 

#include 

#include 

#define ADDRESS 320

#define PAGENUM 32

#define FRAMENUM 10

#define MAX ADDRESS * 2

int addr[ADDRESS], page[PAGENUM];

void init_addr(void)

{

int i = 0, x, m1, m2;

srand(time(NULL));

while(i < ADDRESS){

x = rand() % ADDRESS;

if( x == ADDRESS - 1)

addr[i++] = x;

else

addr[i++] = x + 1;

m1 = rand() % (x + 1);

addr[i++] = m1;

addr[i++] = m1 + 1;

m2 = (rand() % (ADDRESS - (m1 + 2) - 1)) + (m1 + 2);

if( m2 == ADDRESS - 1)

addr[i++] = m2;

else

addr[i++] = m2 + 1;

}

}

void init_page(void)

{

int i;

for(i = 0; i < PAGENUM; i++)

page[i] = -1;

}

int isIn(int a) //判断地址为a的指令是否在页中

{

int i

for(i = 0; i < PAGENUM; i++)

if(page[i] == a / FRAMENUM)

return i;

return -1;

}

int find_max(int num, int data[]) //找出data序列中第一个最大值的序号

{

int i, temp, max = -1;

for(i = 0; i < num; i++)

if(max < data[i]){

max = data[i];

temp = i;

}

return temp;

}

int find_min(int num, int data[]) //找出data序列中第一个最小值的序号

{

int i, temp, min = MAX;

for(i = 0; i < num; i++)

if(min > data[i]){

min = data[i];

temp = i;

}

return temp;

}

float optimal(void)

{

intpage_fault=0;

inti,t,j,bad;

inttemp[PAGENUM];

for(i=0;i

temp[i]=MAX;//初始化距离所有页面最大

for(i=0;i

t=isIn(addr[i]);

if(t==-1){

bad=find_max(PAGENUM,temp);

page[bad]=addr[i]/FRAMENUM;

for(j=0;j

if(temp[j]

temp[j]--;

for(j=i+1;j

if((addr[j]/FRAMENUM)==page[bad]){

temp[bad]=j-i;

break;

}

temp[bad]=MAX;

}

page_fault++;

}else{

for(j=0;j

if(temp[j]

temp[j]--;

for(j=i+1;j

if((addr[j]/FRAMENUM)==page[t]){

temp[t]=j-i;

break;

}

temp[t]=MAX;

}

}

}

return1-(float)(page_fault)/ADDRESS;

}

floatFIFO(void)

{

inti,count=0,page_fault=0;

for(i=0;i

if(isIn(addr[i])==-1){

page[count]=addr[i]/FRAMENUM;

count=(count+1)%PAGENUM;

page_fault++;

}

return1-(float)(page_fault)/ADDRESS

}

floatLRU(void)

{

inttoo_long=0,i,page_fault=0,t,j;

inttemp[PAGENUM];

for(i=0;i

temp[i]=0;

for(i=0;i

t=isIn(addr[i]);

if(t==-1){

too_long=find_max(PAGENUM,temp);

page[too_long]=addr[i]/FRAMENUM;

for(j=0;j

temp[j]++;

temp[too_long]=0;

page_fault++;

}else{

for(j=0;j

temp[j]++;

temp[t]=0;

}

}

return1-(float)page_fault/ADDRESS;

}

floatLFU(void)

{

inttemp1[ADDRESS/FRAMENUM]={0},temp2[PAGENUM]={0};

inti,t,bad,page_fault=0;

for(i=0;i

temp1[addr[i]/FRAMENUM]++;

for(i=0;i

t=isIn(addr[i]);

if(t==-1){

bad=find_min(PAGENUM,temp2);

page[bad]=addr[i]/FRAMENUM;

temp2[bad]=temp1[addr[i]/FRAMENUM];

page_fault++;

}

}

return1.0-(float)(page_fault)/ADDRESS;

}

intmain()

{

floatoptimal_fault,FIFO_fault,LRU_fault,LFU_fault;

init_addr();

init_page();

optimal_fault=optimal();

init_page();

FIFO_fault=FIFO();

init_page();

LRU_fault=LRU();

init_page();

LFU_fault=LFU();

printf("Thepageis%d.\nTheoptimal_faultis%.2f.\nTheFIFO_faultis%.2f.\nTheLRU_faultis%.2f.\nTheLFU_faultis%.2f.\n",PAGENUM,optimal_fault,FIFO_fault,LRU_fault,LFU_fault);

system("pause");

return0;

}

5、测试结果

【下载本文档,可以自由复制内容或自由编辑修改内容,更多精彩文章,期待你的好评和关注,我将一如既往为您服务】

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1