#include <iostream>
#include<stdlib.h>
using namespace std;
#define FREE 0
#define BUSY 1
#define MAX_length 800
typedef struct freeArea//首先定义空闲区分表结构
{
int flag;//状态
int size;//大小
int ID;
int address;//起始地址
}Elemtype;
typedef struct Free_Node
{
Elemtype date;
struct Free_Node* front;
struct Free_Node* next;
}Free_Node, * FNodeList;
FNodeList block_first;
FNodeList block_last;
int alloc(int tag);//内存分配
int free(int ID);//内存回收
int first_fit(int ID, int size);//首次适应算法
int for_fit(int ID, int size);//循环适应算法
int best_fit(int ID, int size);//最佳适应算法
int worst_fit(int ID, int size);//最坏适应算法
void show();//查看分配
void init();//初始化
//初始化
void init()
{
block_first = new Free_Node;
block_last = new Free_Node;
block_first->front = NULL;
block_first->next = block_last;
block_last->front = block_first;
block_last->next = NULL;
block_last->date.address = 0;
block_last->date.flag = FREE;
block_last->date.ID = FREE;
block_last->date.size = MAX_length;
}
//实现内存分配
int alloc(int tag)
{
int ID, size1;
cout << "请输入作业号:";
cin >> ID;
cout << "请输入所需内存大小:";
cin >> size1;
if (ID <= 0 || size1 <= 0)
{
cout << "输入错误!请输入正确的ID和请求大小:" << endl;
return 0;
}
//采用首次适应算法
if (tag == 1)
{
if (first_fit(ID, size1))
{
cout << "分配成功!" << endl;
show();
}
else cout << "分配失败!" << endl;
return 1;
}
//采用循环适应算法
else if(tag==2)
{
if (for_fit(ID, size1))
{
cout << "分配成功!" << endl;
show();
}
else cout << "分配失败!" << endl;
return 1;
}
//采用最好适应算法
else if(tag==3)
{
if (best_fit(ID, size1))
{
cout << "分配成功!" << endl;
show();
}
else cout << "分配失败!" << endl;
return 1;
}
//采用最坏适应算法
else if (tag == 4)
{
if (worst_fit(ID, size1))
{
cout << "分配成功!" << endl;
show();
}
else cout << "分配失败!" << endl;
return 1;
}
}
// 首次适应算法
int first_fit(int ID, int size)
{
FNodeList temp = (FNodeList)malloc(sizeof(Free_Node));//创建一个中间变量
Free_Node* p = block_first->next;//指针
//分配数据
temp->date.ID = ID;
temp->date.size = size;
temp->date.flag = BUSY;
//遍历
while (p)
{
if (p->date.flag == FREE && p->date.size == size)//请求大小刚好满足
{
p->date.flag = BUSY;
p->date.ID = ID;
return 1;
break;
}
if (p->date.flag == FREE && p->date.size > size)//说明还有其他的空闲区间
{ //将temp放入链表
temp->next = p;
temp->front = p->front;
temp->date.address = p->date.address;
p->front->next = temp;
p->front = temp;
//空闲区域减去忙碌区域
p->date.address = temp->date.address + temp->date.size;
p->date.size -= size;
return 1;
break;
}
p = p->next;
}
show();
return 0;
}
// 循环首次适应算法
int for_fit(int ID, int size) {
static Free_Node* last_fit = NULL; // 静态变量,用于记住上一次适应的位置
if (!last_fit) {
last_fit = block_first->next; // 初始化last_fit
}
Free_Node* p = last_fit;//创建一个指针
FNodeList temp = (FNodeList)malloc(sizeof(Free_Node));//创建一个中间变量
//赋值
temp->date.ID = ID;
temp->date.size = size;
temp->date.flag = BUSY;
Free_Node* start = last_fit; // 记录起始点
do {
if (p->date.flag == FREE && p->date.size == size) { // 请求大小刚好满足
p->date.flag = BUSY;
p->date.ID = ID;
last_fit = p->next ? p->next : block_first->next; // 更新last_fit
free(temp); // 不需要新节点
return 1;
}
else if (p->date.flag == FREE && p->date.size > size) { // 请求大小小于空闲块
temp->next = p;
temp->front = p->front;
temp->date.address = p->date.address;
if (p->front) {
p->front->next = temp;
}
p->front = temp;
temp->next = p;
p->front = temp;
p->date.address = temp->date.address + size;
p->date.size -= size;
last_fit = temp->next ? temp->next : block_first->next; // 更新last_fit
return 1;
}
p = p->next ? p->next : block_first->next; // 循环搜索
} while (p != start);
show();
free(temp); // 分配失败时释放temp
return 0;
}
//最坏适应算法
int worst_fit(int ID, int size)//最坏适应算法
{
int surplus;//记录可用内存与需求内存的差值
FNodeList temp = (FNodeList)malloc(sizeof(Free_Node));
Free_Node* p = block_first->next;
temp->date.ID = ID;
temp->date.size = size;
temp->date.flag = BUSY;
Free_Node* q = NULL;//记录最坏位置
int max_surplus = -1; //记录最大的差值
while (p)//遍历链表,找到第一个可用的空闲区间将他给q
{
if (p->date.flag == FREE && p->date.size >= size)
{
q = p;
surplus = p->date.size - size;
max_surplus = surplus;
break;
}
p = p->next;
}
while (p)//继续遍历,找到更大的空闲区块
{
if (p->date.flag == FREE && p->date.size >= size)
{
surplus = p->date.size - size;
if (max_surplus < surplus)
{
max_surplus = surplus;
q = p;
}
}
p = p->next;
}
if (q == NULL)//如果没有找到位置
{
return 0;
}
else//找到了最坏位置
{
//把temp放到p前面
temp->next = q;
temp->front = q->front;
temp->date.address = q->date.address;
q->front->next = temp;
q->front = temp;
//p减去temp数据
q->date.size = max_surplus;
q->date.address += size;
return 1;
}
show();
}
int best_fit(int ID, int size)//最佳适应算法
{
int surplus;//记录可用内存与需求内存的差值
FNodeList temp = (FNodeList)malloc(sizeof(Free_Node));
Free_Node* p = block_first->next;
temp->date.ID = ID;
temp->date.size = size;
temp->date.flag = BUSY;
Free_Node* q = NULL;//记录最佳位置
while (p)//遍历链表,找到第一个可用的空闲区间将他给q
{
if (p->date.flag == FREE && p->date.size >= size)
{
q = p;
surplus = p->date.size - size;
break;
}
p = p->next;
}
while (p)//继续遍历,找到更加合适的位置
{
if (p->date.flag == FREE && p->date.size == size)//正好等于
{
p->date.flag = BUSY;
p->date.ID = ID;
return 1;
break;
}
if (p->date.flag == FREE && p->date.size > size)//空闲区域大
{
if (surplus > p->date.size - size)
{
surplus = p->date.size - size;
q = p;
}
}
p = p->next;
}
if (q == NULL)//如果没有找到位置
{
return 0;
}
else//找到了最佳位置
{//temp放入p前
temp->next = q;
temp->front = q->front;
temp->date.address = q->date.address;
q->front->next = temp;
q->front = temp;
//p减去temp
q->date.size = surplus;
q->date.address += size;
show();
return 1;
}
}
int free(int ID)//主存回收
{
Free_Node* p = block_first->next;
while (p)
{
if (p->date.ID == ID)//找到要回收的ID区域
{
p->date.flag = FREE;
p->date.ID = FREE;
//判断P与前后区域关系
if (p->front->date.flag == FREE && p->next->date.flag != FREE)
{
p->front->date.size += p->date.size;
p->front->next = p->next;
p->next->front = p->front;
}
if (p->front->date.flag != FREE && p->next->date.flag == FREE)
{
p->date.size += p->next->date.size;
if (p->next->next)
{
p->next->next->front = p;
p->next = p->next->next;
}
else p->next = p->next->next;
}
if (p->front->date.flag == FREE && p->next->date.flag == FREE)
{
p->front->date.size += p->date.size + p->next->date.size;
if (p->next->next)
{
p->next->next->front = p->front;
p->front->next = p->next->next;
}
else p->front->next = p->next->next;
}
if (p->front->date.flag != FREE && p->next->date.flag != FREE)
{//
}
break;
}
p = p->next;
}
cout << "回收成功!" << endl;
show();
return 1;
}
void show()
{
cout << "----------------------------------------------------" << endl;
cout << "分区号 起始地址 内存大小 分区状态" << endl;
Free_Node* p = block_first->next;
while (p)
{
if (p->date.ID == FREE)
cout << "FREE" << "\t\t";
else cout << p->date.ID<<"\t\t";
cout << p->date.address << "\t\t";
cout << p->date.size << "\t\t";
if (p->date.flag == FREE)
cout << "空闲" << endl;
else
cout << "已分配" << endl;
p = p->next;
}
cout << "---------------------------------------------------" << endl;
}
int main()
{
int tag = 0;
int ID;
int count;
init();
cout << "\t\t\t\t\t动态分区分配方式的模拟" << endl;
while (tag != 6)
{
cout << "------请选择操作-----" << endl;
cout << "1:首次适应算法\n2:循环适应算法\n3:最佳适应算法\n4:最坏适应算法\n5:内存回收\n6:退出" << endl;
cout << "---------------------" << endl;
cin >> tag;
switch (tag)
{
case 1:
cout << "请输入指令数量" << endl;
cin >> count;
for (int i = 0; i < count; i++) {
alloc(tag);
}
break;
case 2:
cout << "请输入指令数量" << endl;
cin >> count;
for (int i = 0; i < count; i++) {
alloc(tag);
}
break;
case 3:
cout << "请输入指令数量" << endl;
cin >> count;
for (int i = 0; i < count; i++) {
alloc(tag);
}
break;
case 4:
cout << "请输入指令数量" << endl;
cin >> count;
for (int i = 0; i < count; i++) {
alloc(tag);
}
break;
case 5:
cout << "请输入需要回收的作业号:";
cin >> ID;
free(ID);
break;
}
}
return 0;
}/*
1
3
1 130
2 60
3 100
5
2
1
1
4 200
5
3
5
1
1
3
5 140
6 60
7 50
5
6
•作业1申请130KB。
•作业2申请60KB。
•作业3申请100KB。
•作业2释放60KB。
•作业4申请200KB。
•作业3释放100KB。
•作业1释放130KB。
•作业5申请140KB。
•作业6申请60KB。
•作业7申请50KB。
•作业6释放60KB。
*/