文档介绍:实验三内存管理 1
1
1
1
2
3
5
7
9
12
15
实验三内存管理
构造一个没有虚存功能的内存管理系统,并进行测试和对不同分配策略的性能展开比较评估。本次实验,选择的分配策略:First-fit 和 next-fit
1、设计一个内存管理器,支持至少两种分配策略(本实验使用firstfit策略和nextfit策略);
2、分别对不同的策略进行性能评估
unsigned char mm[65536];
用户接口
用户接口函数:
int mm_init() //初始化
int mm_request(int n) //申请空间
void mm_release(int p) //释放空间
srand((unsigned char)time(NULL));
tmp = (rand() % 1024) + 1; //最大申请1024
tmp = (rand() % j);
while(p[tmp] == -1)
{
tmp = rand() % j;
}
requestsize = requestsize - BinToInt(&mm[p[tmp] + 4]);
mm_release(p[tmp]);
cout << "释放指针" << p[tmp] << endl;
p[tmp] = -1;
实现两个版本的内存管理器,分配策略分别为:first-fit和next-fit
#include <iostream>
#include <>
#include <>
#include <>
#include ""
using namespace std;
int main()
{
int i;
int j = 0; //分配指针数
int p[1000]; //用于存放分配出的指针
int tmp;
int requestsize = 0; //统计申请空间
int k = 0; //统计搜索步数
srand((unsigned char)time(NULL));
step = 0;
mm_init();
for(i = 0; i < 30; i++) //模拟30步
{
cout << "第" << i + 1 << " 步" << endl;
do
{
tmp = (rand() % 1024) + 1; //最大申请1024
cout << "申请空间" << tmp << " 字节" << endl;
p[j] = mm_request(tmp);
if(p[j] != -1)
{
requestsize = requestsize+tmp;
k = k + step;
cout << "分配指针" << p[j] << endl;
cout << endl;
j++;
}
else
{
cout << "剩余空间不足,分配失败" << endl;
}
}
while(p[j] != -1);
tmp = (rand() % j);
while(p[tmp] == -1)
{
tmp = rand() % j;
}
requestsize = requestsize - BinToInt(&mm[p[tmp] + 4]);
mm_release(p[tmp]);
cout << "释放指针" << p[tmp] << endl;
p[tmp] = -1;
//以下为性能统计指标
cout << endl;
cout << "平均申请空间:" << (double)requestsize / (double)j << " 字节" << endl;
cout << "平均空间利用率:" << (double)requestsize/(double)65536 << endl;
cout << "平均搜索步数:" << (double)k/(double)j << endl;
cout << endl;
if((i + 1) % 5 == 0)
{
system("pause");
}
}