实验二 动态分区分配算法的模拟
2.1 首次适应算法的模拟
一、实验目的及要求
1.实验目的
1)加深操作系统内存管理过程的理解
2)掌握内存分配算法的基本应用
二、实验环境(工具、配置等)
1.硬件要求:计算机一台。
2.软件要求:Windows操作系统,使用编程语言不限,如c、c++、Java等。
三、实验内容
1.算法思想:首次适应算法:将空闲分区链以地址递增的顺序连接;在进行内存分配时,从链首开始顺序查找,直到找到一块分区的大小可以满足需求时,按照该作业的大小,从该分区中分配出内存,将剩下的空闲分区仍然链在空闲分区链中。
2.内存状态初始化:用户动态输入空白分区的个数,设置空白分区的大小,这里我采用输入5个空白分区,其大小为:100,200,300,400,500(单位:KB),其内存初始状态设置结果如下图所示:
3.分配内存且查看内存情况:作业A申请内存120KB,作业B申请内存80KB,作业C申请内存320KB,作业D申请内存410KB,作业E申请内存100KB,分配结果如下图所示,符合预期结果:
图2 分配内存(一)
图2 分配内存(二)
图3 分配后结果
4.合并碎片空间:输入4将分配后的内存进行合并碎片空间,再输入3查看内存情况,结果如下图所示:
5.释放内存:输入2,选择释放分区号为1的内存,结果如下图所示,释放后,分区号为1的内存处于空闲状态,符合预期结果
6.分析:由上述模拟可以清楚的看到,该算法倾向于优先利用内存中低址部分的空闲区,从而保留了高址部分的大的空闲分区,为大作业的内存分配创造了条件。但每次都是优先利用低址部分的空闲分区,造成低址部分产生大量的碎片,同时每次都是从低址部分查找,使得查找空闲分区的开销增大。
代码如下所示:
#include<iostream>
#include<stdlib.h>
#include<windows.h>
using namespace std;
#define Free 0 //空闲状态
#define Busy 1 //已用状态
#define OK 1 //完成
#define ERROR 0 //出错
#define MAX_length 2048 //定义最大主存信息2048KB
#define Min_Size 10 //规定的不可分割的剩余分区的大小
int flag;//标志位
//函数声明
int myMalloc();//内存分配
int free(int); //内存回收
int First_fit(int);//首次适应算法
void printNode();//查看分配
int Init();//空闲分区的设置
int Combination();//合并碎片空间
typedef struct Kongxian //定义一个空闲区说明表结构
{
char name[30] ;
long size; //分区大小
long address; //分区地址
int state; //空闲标志
}Elem;
typedef struct LinkNode// 双向链表存储结构
{
Elem data;
struct LinkNode *prior; //前趋指针
struct LinkNode *next; //后继指针
}LinkNode, *LinkList;
LinkList block_first; //头结点
LinkList block_last; //尾结点
LinkNode *pn ; //循环首次适应算法中的指针
void menu(){ //打印操作界面
printf("\n");
printf("\t\t\t\t ***** 首次适应算法的动态分区分配方式模拟 ***** \n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 1. 申请内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 2. 释放内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 3. 查看内存情况 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 4. 合并碎片空间 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 5.退出 │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("\t\t\t\t\t\t 请您选择(1-5):\t"); //打印想要进行的操作
}
int Init()//初始化内存状态设置(空闲分区的设置)
{
int Free_num; //空闲分区的个数
block_first = (LinkList)malloc(sizeof(LinkNode));
block_last = (LinkList)malloc(sizeof(LinkNode));
block_first->prior = NULL;
block_first->next = block_last;
cout << "请输入空闲分区数目:";
cin >> Free_num ;
int Size[Free_num];
printf("\n请分别输入这%d空闲分区大小(单位KB):\n",Free_num );
for(int i = 0; i < Free_num; i++)
{
cin >> Size[i];
if(Size[i] <= Min_Size) {
i--;
cout << "\n输入错误,请重输!\n";
}
int temp = 0;
temp = temp + Size[i];
if(temp >= MAX_length) {
i--;
cout << "\n输入错误,请重输!\n";
}
}
LinkList p = block_first;
for(int i = 0; i < Free_num; i++)
{
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = Size[i];
temp->data.state = Free;
if(p == block_first)
temp->data.address = 0;
else
temp->data.address = p->data.address + p->data.size;
temp->prior =p;
p->next = temp;
p = p->next;
}
block_last = p;
block_last->next = NULL;
pn = block_first->next;
return OK;
}
int myMalloc()//分配主存
{
int request = 0;
cout << "请输入需要分配的主存大小(单位:KB):"<<endl;
cin >> request;
if (request<0 || request == 0)
{
cout << "分配大小不合适,请重试!" << endl;
return ERROR;
}
if (First_fit(request) == OK) cout << "分配成功!" << endl;
else cout << "内存不足,分配失败!" << endl;
return OK;
}
int First_fit(int request)//首次适应算法
{
//为申请作业开辟新空间且初始化
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = request;
temp->data.state = Busy;
cout << "请输入作业名:";
cin >>temp->data.name ;
cout <<endl;
LinkNode *p = block_first->next;
while (p)
{
if (p->data.state == Free && p->data.size - request <= Min_Size && p->data.size - request >= 0) //有大小恰好合适的空闲块
{
p->data.state = Busy;
for(int i = 0; i < 10; i++){
p->data.name[i] = temp->data.name[i];
}
return OK;
break;
}
if (p->data.state == Free && p->data.size - request > Min_Size) //有空闲块能满足需求且有剩余
{
temp->prior = p->prior;
temp->next = p;
temp->data.address = p->data.address;
p->prior->next = temp;
p->prior = temp;
p->data.address = temp->data.address + temp->data.size;
p->data.size -= request;
return OK;
break;
}
p = p->next;
}
return ERROR;
}
int free(int flag)//主存回收
{
LinkNode *p = block_first;
for (int i = 0; i <= flag; i++)
if (p != NULL)
p = p->next;
else
return ERROR;
p->data.state = Free;
if(p == block_last){
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p = p->prior;
p->next = NULL;
block_last = p;
}
}
else{
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p->prior->next = p->next;//去掉原来被合并的p
p->next->prior = p->prior;
p = p->prior;
}
if (p->next != block_last && p->next->data.state == Free )//与后面的空闲块相连
{
p->data.size += p->next->data.size;//空间扩充,合并为一个
p->next->next->prior = p;
p->next = p->next->next;
}
if (p->next == block_last && p->next->data.state == Free )//与最后的空闲块相连
{
p->data.size += p->next->data.size;
p->next = NULL;
}
}
return OK;
}
int Combination() //实现紧凑,合并碎片
{
LinkNode *p = block_first->next;
int size = 0;
while(p){
if(p->data.state ==Free && p != block_last) //去除空闲分区
{
size =size + p->data.size ;
p->next->prior =p->prior ;
p->prior->next = p->next ;
}
else{
p->data.address = p->data.address - size; //改变地址
}
p = p->next;
}
if(block_last->data.state == Free){ //实现最后合并空闲分区
block_last->data.size += size;
}
else if(size != 0){ //将所有空闲分区合并放在最后一个分区后面
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
block_last->next =temp;
temp->prior = block_last;
temp->next = NULL;
temp->data.size = size;
temp->data.address = block_last->data.address + block_last->data.size ;
block_last = temp;
temp->data.state = Free;
}
pn = block_first->next;
if(size == 0) //判断是否有碎片
{
return ERROR;
}
return OK;
}
void printNode()//显示内存分配情况
{
int num = 0;
printf(" 内存情况 \n");
cout << "======================================================\n\n";
LinkNode *p = block_first->next;
cout << "分区号\t起始地址\t分区大小\t状态\t作业名\n\n";
while (p)
{
cout << " " << num++ << "\t";
cout << " " << p->data.address << "\t\t";
cout << " " << p->data.size << "KB\t\t";
if (p->data.state == Free) {
cout << "空闲\n\n";
}
else {
cout << "已分配\t";
cout <<" "<<p->data.name <<"\n\n";
}
p = p->next;
}
cout << "======================================================\n\n";
}
void Operate(){
int choice; //操作选择标记
while (1)
{
menu();
scanf("%d",&choice);
if (choice == 1) myMalloc(); // 分配内存
else if (choice == 2) // 内存回收
{
int flag;
cout << "请输入您要释放的分区号:"<<endl;
cin >> flag;
free(flag);
}
else if (choice == 3) printNode();
else if(choice == 4)
{
if(Combination() == 0){
cout<<"无需合并碎片空间!"<<endl;
}
else{
cout<<"成功合并碎片!"<<endl;
}
}
else if (choice == 5) break; //退出
else //输入操作有误
{
cout << "输入有误,请重试!" << endl;
continue;
}
}
}
int main()//主函数
{
system("color 5E");
Init(); //初始化内存状态
Operate();
return 0;
}
2.2最佳适应算法的模拟
一、实验结果与分析
1.算法思想:将空闲分区链中的空闲分区按照空闲分区由小到大的顺序排序,从而形成空闲分区链。每次从链首进行查找合适的空闲分区为作业分配内存,这样每次找到的空闲分区是和作业大小最接近的,所谓“最佳”。
2.内存状态初始化:用户动态输入空白分区的个数,设置空白分区的大小,这里我采用输入5个空白分区,其大小为:100,200,300,400,500(单位:KB),其内存初始状态设置结果如下图所示:
4.合并碎片空间:输入4将上面分配后的内存进行合并碎片空间,再输入3查看内存情况,结果如下图所示:
6.分析:由上述模拟可以清楚的看出,该算法要求将所有的空闲分区按照其容量以小到大的顺序形成一空闲分区链,加速了找寻。第一次找到的能满足条件的空闲区必然是最佳的。每次分割剩余的空间总是最小的,减少了外部碎片产生的大小。孤立的来看,似乎是最佳的,但宏观上却不一定。而且空间利用率来看,NF算法确实是物尽其用,但是每次分割留下的都是难以利用的外部碎片,又降低了查找效率。
代码如下所示:
#include<iostream>
#include<stdlib.h>
#include<windows.h>
using namespace std;
#define Free 0 //空闲状态
#define Busy 1 //已用状态
#define OK 1 //完成
#define ERROR 0 //出错
#define MAX_length 2048 //定义最大主存信息2048KB
#define Min_Size 10 //规定的不可分割的剩余分区的大小
int flag;//标志位
//函数声明
int myMalloc();//内存分配
int free(int); //内存回收
int Best_fit(int);//最佳适应算法
void printNode();//查看分配
int Init();//空闲分区的设置
int Combination();//合并碎片空间
typedef struct FreeArea //定义一个空闲区说明表结构
{
char name[30] ;
long size; //分区大小
long address; //分区地址
int state; //空闲标志
}Elem;
typedef struct LinkNode// 双向链表存储结构
{
Elem data;
struct LinkNode *prior; //前趋指针
struct LinkNode *next; //后继指针
}LinkNode, *LinkList;
LinkList block_first; //头结点
LinkList block_last; //尾结点
LinkNode *pn ; //循环首次适应算法中的指针
void menu(){ //打印操作界面
printf("\n");
printf("\t\t\t\t ***** 最佳适应算法的动态分区分配方式模拟 ***** \n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 1. 申请内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 2. 释放内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 3. 查看内存情况 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 4. 合并碎片空间 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 5.退出 │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("\t\t\t\t\t\t 请您选择(1-5):\t"); //打印想要进行的操作
}
int Init()//初始化内存状态设置(空闲分区的设置)
{
int Free_num; //空闲分区的个数
block_first = (LinkList)malloc(sizeof(LinkNode));
block_last = (LinkList)malloc(sizeof(LinkNode));
block_first->prior = NULL;
block_first->next = block_last;
cout << "请输入空闲分区数目:";
cin >> Free_num ;
int Size[Free_num];
printf("\n请分别输入这%d空闲分区大小(单位KB):\n",Free_num );
for(int i = 0; i < Free_num; i++)
{
cin >> Size[i];
if(Size[i] <= Min_Size) {
i--;
cout << "\n输入错误,请重输!\n";
}
int temp = 0;
temp = temp + Size[i];
if(temp >= MAX_length) {
i--;
cout << "\n输入错误,请重输!\n";
}
}
LinkList p = block_first;
for(int i = 0; i < Free_num; i++)
{
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = Size[i];
temp->data.state = Free;
if(p == block_first)
temp->data.address = 0;
else
temp->data.address = p->data.address + p->data.size;
temp->prior =p;
p->next = temp;
p = p->next;
}
block_last = p;
block_last->next = NULL;
pn = block_first->next;
return OK;
}
int myMalloc()//分配主存
{
int request = 0;
cout << "请输入需要分配的主存大小(单位:KB):"<<endl;
cin >> request;
if (request<0 || request == 0)
{
cout << "分配大小不合适,请重试!" << endl;
return ERROR;
}
if (Best_fit(request) == OK) cout << "分配成功!" << endl;
else cout << "内存不足,分配失败!" << endl;
return OK;
}
int Best_fit(int request)//最佳适应算法
{
int ch; //记录最小剩余空间
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = request;
temp->data.state = Busy;
LinkNode *p = block_first->next;
LinkNode *q = NULL; //记录最佳插入位置
cout << "请输入作业名:";
cin >>temp->data.name ;
while (p) //初始化最小空间和最佳位置
{
if (p->data.state == Free && (p->data.size >= request))
{
if (q == NULL)
{
q = p;
ch = p->data.size - request;
}
else if (q->data.size > p->data.size)
{
q = p;
ch = p->data.size - request;
}
}
p = p->next;
}
if (q == NULL) return ERROR;//没有找到空闲块
else if (q->data.size - request <= Min_Size)
{
q->data.state = Busy;
for(int i = 0; i < 10; i++){
q->data.name[i] = temp->data.name[i];
}
return OK;
}
else
{
temp->prior = q->prior;
temp->next = q;
temp->data.address = q->data.address;
q->prior->next = temp;
q->prior = temp;
q->data.address += request;
q->data.size = ch;
return OK;
}
return OK;
}
int free(int flag)//主存回收
{
LinkNode *p = block_first;
for (int i = 0; i <= flag; i++)
if (p != NULL)
p = p->next;
else
return ERROR;
p->data.state = Free;
if(p == block_last){
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p = p->prior;
p->next = NULL;
block_last = p;
}
}
else{
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p->prior->next = p->next;//去掉原来被合并的p
p->next->prior = p->prior;
p = p->prior;
}
if (p->next != block_last && p->next->data.state == Free )//与后面的空闲块相连
{
p->data.size += p->next->data.size;//空间扩充,合并为一个
p->next->next->prior = p;
p->next = p->next->next;
}
if (p->next == block_last && p->next->data.state == Free )//与最后的空闲块相连
{
p->data.size += p->next->data.size;
p->next = NULL;
}
}
return OK;
}
int Combination() //实现紧凑,合并碎片
{
LinkNode *p = block_first->next;
int size = 0;
while(p){
if(p->data.state ==Free && p != block_last) //去除空闲分区
{
size =size + p->data.size ;
p->next->prior =p->prior ;
p->prior->next = p->next ;
}
else{
p->data.address = p->data.address - size; //改变地址
}
p = p->next;
}
if(block_last->data.state == Free){ //实现最后合并空闲分区
block_last->data.size += size;
}
else if(size != 0){ //将所有空闲分区合并放在最后一个分区后面
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
block_last->next =temp;
temp->prior = block_last;
temp->next = NULL;
temp->data.size = size;
temp->data.address = block_last->data.address + block_last->data.size ;
block_last = temp;
temp->data.state = Free;
}
pn = block_first->next;
if(size == 0) //判断是否有碎片
{
return ERROR;
}
return OK;
}
void printNode()//显示主存分配情况
{
int num = 0;
printf(" 内存情况 \n");
cout << "======================================================\n\n";
LinkNode *p = block_first->next;
cout << "分区号\t起始地址\t分区大小\t状态\t作业名\n\n";
while (p)
{
cout << " " << num++ << "\t";
cout << " " << p->data.address << "\t\t";
cout << " " << p->data.size << "KB\t\t";
if (p->data.state == Free) {
cout << "空闲\n\n";
}
else {
cout << "已分配\t";
cout <<" "<<p->data.name <<"\n\n";
}
p = p->next;
}
cout << "======================================================\n\n";
}
void Operate(){
int choice; //操作选择标记
while (1)
{
menu();
scanf("%d",&choice);
if (choice == 1) myMalloc(); // 分配内存
else if (choice == 2) // 内存回收
{
int flag;
cout << "请输入您要释放的分区号:"<<endl;
cin >> flag;
free(flag);
}
else if (choice == 3) printNode();
else if(choice == 4)
{
if(Combination() == 0){
cout<<"无需合并碎片空间!"<<endl;
}
else{
cout<<"成功合并碎片!"<<endl;
}
}
else if (choice == 5) break; //退出
else //输入操作有误
{
cout << "输入有误,请重试!" << endl;
continue;
}
}
}
int main()//主函数
{
system("color 5E");
Init(); //初始化内存状态
Operate();
return 0;
}
最差适应算法的模拟
实验结果与分析
1.算法思想:与最佳适应算法刚好相反,将空闲分区链的分区按照从大到小的顺序排序形成空闲分区链,每次查找时只要看第一个空闲分区是否满足即可。
2. 内存状态初始化:用户动态输入空白分区的个数,设置空白分区的大小,这里我采用输入5个空白分区,其大小为:100,200,300,400,500(单位:KB),其内存初始状态设置结果如下图所示:
3. 合并碎片空间:输入4,将分配后的内存进行合并碎片空间,再输入3查看
内存情况,结果如下图所示:
4. 释放内存:输入2,选择释放分区号为1的内存,结果如下图所示,释放后,分区号为2的内存处于空闲状态,符合预期结果
6.分析:由上述模拟可以清楚的看出,最坏适应算法效率高,分区查找方便,但当小作业把大空闲分区分小了,那么,大作业就找不到合适的空闲分区。
7.流程图
#include<iostream>
#include<stdlib.h>
#include<windows.h>
using namespace std;
#define Free 0 //空闲状态
#define Busy 1 //已用状态
#define OK 1 //完成
#define ERROR 0 //出错
#define MAX_length 2048 //定义最大主存信息2048KB
#define Min_Size 10 //规定的不可分割的剩余分区的大小
int flag;//标志位
//函数声明
int myMalloc();//内存分配
int free(int); //内存回收
int Worst_fit(int); //最差适应算法
void printNode();//查看分配
int Init();//空闲分区的设置
int Combination();//合并碎片空间
typedef struct FreeArea //定义一个空闲区说明表结构
{
char name[30] ;
long size; //分区大小
long address; //分区地址
int state; //空闲标志
}Elem;
typedef struct LinkNode// 双向链表存储结构
{
Elem data;
struct LinkNode *prior; //前趋指针
struct LinkNode *next; //后继指针
}LinkNode, *LinkList;
LinkList block_first; //头结点
LinkList block_last; //尾结点
LinkNode *pn ; //循环首次适应算法中的指针
void menu(){ //打印操作界面
printf("\n");
printf("\t\t\t\t ***** 最差适应算法的动态分区分配方式模拟 ***** \n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 1. 申请内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 2. 释放内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 3. 查看内存情况 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 4. 合并碎片空间 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 5.退出 │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("\t\t\t\t\t\t 请您选择(1-5):\t"); //打印想要进行的操作
}
int Init()//初始化内存状态设置(空闲分区的设置)
{
int Free_num; //空闲分区的个数
block_first = (LinkList)malloc(sizeof(LinkNode));
block_last = (LinkList)malloc(sizeof(LinkNode));
block_first->prior = NULL;
block_first->next = block_last;
cout << "请输入空闲分区数目:";
cin >> Free_num ;
int Size[Free_num];
printf("\n请分别输入这%d空闲分区大小(单位KB):\n",Free_num );
for(int i = 0; i < Free_num; i++)
{
cin >> Size[i];
if(Size[i] <= Min_Size) {
i--;
cout << "\n输入错误,请重输!\n";
}
int temp = 0;
temp = temp + Size[i];
if(temp >= MAX_length) {
i--;
cout << "\n输入错误,请重输!\n";
}
}
LinkList p = block_first;
for(int i = 0; i < Free_num; i++)
{
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = Size[i];
temp->data.state = Free;
if(p == block_first)
temp->data.address = 0;
else
temp->data.address = p->data.address + p->data.size;
temp->prior =p;
p->next = temp;
p = p->next;
}
block_last = p;
block_last->next = NULL;
pn = block_first->next;
return OK;
}
int myMalloc()//分配主存
{
int request = 0;
cout << "请输入需要分配的主存大小(单位:KB):"<<endl;
cin >> request;
if (request<0 || request == 0)
{
cout << "分配大小不合适,请重试!" << endl;
return ERROR;
}
if (Worst_fit(request) == OK) cout << "分配成功!" << endl;
else cout << "内存不足,分配失败!" << endl;
return OK;
}
int Worst_fit(int request)//最差适应算法
{
int ch; //记录最大剩余空间
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = request;
temp->data.state = Busy;
LinkNode *p = block_first->next;
LinkNode *q = NULL; //记录最佳插入位置
cout << "请输入作业名:";
cin >>temp->data.name ;
while (p) //初始化最大空间和最佳位置
{
if (p->data.state == Free && (p->data.size >= request))
{
if (q == NULL)
{
q = p;
ch = p->data.size - request;
}
else if (q->data.size < p->data.size)
{
q = p;
ch = p->data.size - request;
}
}
p = p->next;
}
if (q == NULL) return ERROR;//没有找到空闲块
else if (q->data.size - request <= Min_Size)
{
q->data.state = Busy;
for(int i = 0; i < 10; i++){
q->data.name[i] = temp->data.name[i];
}
return OK;
}
else
{
temp->prior = q->prior;
temp->next = q;
temp->data.address = q->data.address;
q->prior->next = temp;
q->prior = temp;
q->data.address += request;
q->data.size = ch;
return OK;
}
return OK;
}
int free(int flag)//主存回收
{
LinkNode *p = block_first;
for (int i = 0; i <= flag; i++)
if (p != NULL)
p = p->next;
else
return ERROR;
p->data.state = Free;
if(p == block_last){
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p = p->prior;
p->next = NULL;
block_last = p;
}
}
else{
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p->prior->next = p->next;//去掉原来被合并的p
p->next->prior = p->prior;
p = p->prior;
}
if (p->next != block_last && p->next->data.state == Free )//与后面的空闲块相连
{
p->data.size += p->next->data.size;//空间扩充,合并为一个
p->next->next->prior = p;
p->next = p->next->next;
}
if (p->next == block_last && p->next->data.state == Free )//与最后的空闲块相连
{
p->data.size += p->next->data.size;
p->next = NULL;
}
}
return OK;
}
int Combination() //实现紧凑,合并碎片
{
LinkNode *p = block_first->next;
int size = 0;
while(p){
if(p->data.state ==Free && p != block_last) //去除空闲分区
{
size =size + p->data.size ;
p->next->prior =p->prior ;
p->prior->next = p->next ;
}
else{
p->data.address = p->data.address - size; //改变地址
}
p = p->next;
}
if(block_last->data.state == Free){ //实现最后合并空闲分区
block_last->data.size += size;
}
else if(size != 0){ //将所有空闲分区合并放在最后一个分区后面
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
block_last->next =temp;
temp->prior = block_last;
temp->next = NULL;
temp->data.size = size;
temp->data.address = block_last->data.address + block_last->data.size ;
block_last = temp;
temp->data.state = Free;
}
pn = block_first->next;
if(size == 0) //判断是否有碎片
{
return ERROR;
}
return OK;
}
void printNode()//显示内存分配情况
{
int num = 0;
printf(" 内存情况 \n");
cout << "======================================================\n\n";
LinkNode *p = block_first->next;
cout << "分区号\t起始地址\t分区大小\t状态\t作业名\n\n";
while (p)
{
cout << " " << num++ << "\t";
cout << " " << p->data.address << "\t\t";
cout << " " << p->data.size << "KB\t\t";
if (p->data.state == Free) {
cout << "空闲\n\n";
}
else {
cout << "已分配\t";
cout <<" "<<p->data.name <<"\n\n";
}
p = p->next;
}
cout << "======================================================\n\n";
}
void Operate(){
int choice; //操作选择标记
while (1)
{
menu();
scanf("%d",&choice);
if (choice == 1) myMalloc(); // 分配内存
else if (choice == 2) // 内存回收
{
int flag;
cout << "请输入您要释放的分区号:"<<endl;
cin >> flag;
free(flag);
}
else if (choice == 3) printNode();
else if(choice == 4)
{
if(Combination() == 0){
cout<<"无需合并碎片空间!"<<endl;
}
else{
cout<<"成功合并碎片!"<<endl;
}
}
else if (choice == 5) break; //退出
else //输入操作有误
{
cout << "输入有误,请重试!" << endl;
continue;
}
}
}
int main()//主函数
{
system("color 5E");
Init(); //初始化内存状态
Operate();
return 0;
}
循环首次适应算法的模拟
一、实验分析及结果
1.算法思想:分配内存时不是从链首进行查找可以分配 内存的空闲分区,而是从上一次分配内存的空闲分区的下一个分区开始查找,直到找到可以为该进程分配内存的空闲分区。
2. 内存状态初始化:用户动态输入空白分区的个数,设置空白分区的大小,这里我采用输入5个空白分区,其大小为:100,200,300,400,500(单位:KB),其内存初始状态设置结果如下图所示:
3. 分配内存且查看内存情况:作业A申请内存120KB,作业B申请内存80KB,作业C申请内存320KB,作业D申请内存410KB,作业E申请内存100KB,分配结果如下图所示,符合预期结果:
4.合并碎片空间:输入4将分配后的内存进行合并碎片空间,再输入3查看内存情况,结果如下图所示:
5.释放内存:输入2,选择释放分区号为1的内存,结果如下图所示,释放后,分区号为1的内存处于空闲状态,符合预期结果
6.分析:由上述模拟可以清楚的看到,循环首次适应算法使得空闲分区分布更加均匀,空闲分区的查找开销小。但高址部分的大空闲分区被分小,使得大作业进入无法分配内存。
7.流程图
代码如下所示:
#include<iostream>
#include<stdlib.h>
#include<windows.h>
using namespace std;
#define Free 0 //空闲状态
#define Busy 1 //已用状态
#define OK 1 //完成
#define ERROR 0 //出错
#define MAX_length 2048 //定义最大主存信息2048KB
#define Min_Size 10 //规定的不可分割的剩余分区的大小
int flag;//标志位
//函数声明
int myMalloc();//内存分配
int free(int); //内存回收
int Next_fit(int);//循环首次适应算法
void printNode();//查看分配
int Init();//空闲分区的设置
int Combination();//合并碎片空间
typedef struct FreeArea //定义一个空闲区说明表结构
{
char name[30] ;
long size; //分区大小
long address; //分区地址
int state; //空闲标志
}Elem;
typedef struct LinkNode// 双向链表存储结构
{
Elem data;
struct LinkNode *prior; //前趋指针
struct LinkNode *next; //后继指针
}LinkNode, *LinkList;
LinkList block_first; //头结点
LinkList block_last; //尾结点
LinkNode *pn ; //循环首次适应算法中的指针
void menu(){ //打印操作界面
printf("\n");
printf("\t\t\t\t ***** 循环首次适应算法的动态分区分配方式模拟 ***** \n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 1. 申请内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 2. 释放内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 3. 查看内存情况 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 4. 合并碎片空间 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 5.退出 │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("\t\t\t\t\t\t 请您选择(1-5):\t"); //打印想要进行的操作
}
int Init()//初始化内存状态设置(空闲分区的设置)
{
int Free_num; //空闲分区的个数
block_first = (LinkList)malloc(sizeof(LinkNode));
block_last = (LinkList)malloc(sizeof(LinkNode));
block_first->prior = NULL;
block_first->next = block_last;
cout << "请输入空闲分区数目:";
cin >> Free_num ;
int Size[Free_num];
printf("\n请分别输入这%d空闲分区大小(单位KB):\n",Free_num );
for(int i = 0; i < Free_num; i++)
{
cin >> Size[i];
if(Size[i] <= Min_Size) {
i--;
cout << "\n输入错误,请重输!\n";
}
int temp = 0;
temp = temp + Size[i];
if(temp >= MAX_length) {
i--;
cout << "\n输入错误,请重输!\n";
}
}
LinkList p = block_first;
for(int i = 0; i < Free_num; i++)
{
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = Size[i];
temp->data.state = Free;
if(p == block_first)
temp->data.address = 0;
else
temp->data.address = p->data.address + p->data.size;
temp->prior =p;
p->next = temp;
p = p->next;
}
block_last = p;
block_last->next = NULL;
pn = block_first->next;
return OK;
}
int myMalloc()//分配主存
{
int request = 0;
cout << "请输入需要分配的主存大小(单位:KB):"<<endl;
cin >> request;
if (request<0 || request == 0)
{
cout << "分配大小不合适,请重试!" << endl;
return ERROR;
}
if (Next_fit(request) == OK) cout << "分配成功!" << endl;
else cout << "内存不足,分配失败!" << endl;
return OK;
}
//循环首次适应算法
int Next_fit(int request){
//为申请作业开辟新空间且初始化
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
temp->data.size = request;
temp->data.state = Busy;
cout << "请输入作业名:";
cin >>temp->data.name ;
while (pn)
{
if (pn->data.state == Free && pn->data.size - request <=Min_Size && pn->data.size - request >= 0)
{//有大小恰好合适的空闲块
pn->data.state = Busy;
for(int i = 0; i < 10; i++){
pn->data.name[i] = temp->data.name[i];
}
return OK;
break;
}
if (pn->data.state == Free && pn->data.size > request + Min_Size)
{//有空闲块能满足需求且有剩余
temp->prior = pn->prior;
temp->next = pn;
temp->data.address = pn->data.address;
pn->prior->next = temp;
pn->prior = temp;
pn->data.address = temp->data.address + temp->data.size;
pn->data.size -= request;
return OK;
break;
}
pn = pn->next;
if(pn == NULL)
pn = block_first;
}
return ERROR;
}
int free(int flag)//主存回收
{
LinkNode *p = block_first;
for (int i = 0; i <= flag; i++)
if (p != NULL)
p = p->next;
else
return ERROR;
p->data.state = Free;
if(p == block_last){
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p = p->prior;
p->next = NULL;
block_last = p;
}
}
else{
if (p->prior != block_first && p->prior->data.state == Free )//与前面的空闲块相连
{
p->prior->data.size += p->data.size;//空间扩充,合并为一个
p->prior->next = p->next;//去掉原来被合并的p
p->next->prior = p->prior;
p = p->prior;
}
if (p->next != block_last && p->next->data.state == Free )//与后面的空闲块相连
{
p->data.size += p->next->data.size;//空间扩充,合并为一个
p->next->next->prior = p;
p->next = p->next->next;
}
if (p->next == block_last && p->next->data.state == Free )//与最后的空闲块相连
{
p->data.size += p->next->data.size;
p->next = NULL;
}
}
return OK;
}
int Combination() //实现紧凑,合并碎片
{
LinkNode *p = block_first->next;
int size = 0;
while(p){
if(p->data.state ==Free && p != block_last) //去除空闲分区
{
size =size + p->data.size ;
p->next->prior =p->prior ;
p->prior->next = p->next ;
}
else{
p->data.address = p->data.address - size; //改变地址
}
p = p->next;
}
if(block_last->data.state == Free){ //实现最后合并空闲分区
block_last->data.size += size;
}
else if(size != 0){ //将所有空闲分区合并放在最后一个分区后面
LinkList temp = (LinkList)malloc(sizeof(LinkNode));
block_last->next =temp;
temp->prior = block_last;
temp->next = NULL;
temp->data.size = size;
temp->data.address = block_last->data.address + block_last->data.size ;
block_last = temp;
temp->data.state = Free;
}
pn = block_first->next;
if(size == 0) //判断是否有碎片
{
return ERROR;
}
return OK;
}
void printNode()//显示主存分配情况
{
int num = 0;
printf(" 内存情况 \n");
cout << "======================================================\n\n";
LinkNode *p = block_first->next;
cout << "分区号\t起始地址\t分区大小\t状态\t作业名\n\n";
while (p)
{
cout << " " << num++ << "\t";
cout << " " << p->data.address << "\t\t";
cout << " " << p->data.size << "KB\t\t";
if (p->data.state == Free) {
cout << "空闲\n\n";
}
else {
cout << "已分配\t";
cout <<" "<<p->data.name <<"\n\n";
}
p = p->next;
}
cout << "======================================================\n\n";
}
void Operate(){
int choice; //操作选择标记
while (1)
{
menu();
scanf("%d",&choice);
if (choice == 1) myMalloc(); // 分配内存
else if (choice == 2) // 内存回收
{
int flag;
cout << "请输入您要释放的分区号:"<<endl;
cin >> flag;
free(flag);
}
else if (choice == 3) printNode();
else if(choice == 4)
{
if(Combination() == 0){
cout<<"无需合并碎片空间!"<<endl;
}
else{
cout<<"成功合并碎片!"<<endl;
}
}
else if (choice == 5) break; //退出
else //输入操作有误
{
cout << "输入有误,请重试!" << endl;
continue;
}
}
}
int main()//主函数
{
system("color 5E");
Init(); //初始化内存状态
Operate();
return 0;
}
希望对大家有所帮助。