动态分区存储管理
实验目的:熟悉并掌握动态分区分配的各种算法。
熟悉并掌握动态分区中分区回收的各种情况,并能够实现分区合并。
实验内容:用高级语言模拟实现动态分区存储管理,要求:
- 分区分配算法至少实现首次适应算法、最佳适应算法和最坏适应算法中的至少一种。熟悉并掌握各种算法的空闲区组织方式。
- 分区的初始化——可以由用户输入初始分区的大小。(初始化后只有一个空闲分区,起始地址为0,大小是用户输入的大小)
- 分区的动态分配过程:由用户输入作业号和作业的大小,实现分区过程。
- 分区的回收:用户输入作业号,实现分区回收,同时,分区的合并要体现出来。(注意:不存在的作业号要给出错误提示!)
- 分区的显示:任何时刻,可以查看当前内存的情况(起始地址是什么,大小多大的分区时空闲的,或者占用的,能够显示出来)
要求考虑:(1)内存空间不足的情况,要有相应的显示;
(2)作业不能同名,但是删除后可以再用这个名字;
(3)作业空间回收是输入作业名,回收相应的空间,如果这个作业名不存在,也要有相应的提示。
此次代码实现了首次适应算法、最佳适应算法。
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
int Maxsize;
int k[50];
typedef struct LinkNode
{
bool flage;//1为分配 0为空闲
string name;
int start;//起始地址
int size;
LinkNode *front;//前指针
LinkNode *next;//后指针
} LinkList;
LinkList *line = new LinkList;
void init()//初始化
{
line->flage = 0;
line->name = "free";
line->start = 0;
line->size = Maxsize;
line->front = NULL;
line->next = NULL;
}
void show()
{
LinkList *lineshow = new LinkList;
lineshow = line;
cout <<"分配状态\t"<< "作业名称\t"<<"起始地址\t"<<"分区大小\t"<<endl;
stack<LinkList*> a;
while(lineshow!=NULL)
{
a.push(lineshow);
lineshow = lineshow->next;
}
while (!a.empty())
{
LinkList *lin = new LinkList;
lin=a.top();
cout << lin->flage << "\t\t" << lin->name << "\t\t" << lin->start << "\t\t" << lin->size << endl;
a.pop();
}
}
void firstfit()
{
cout << "请输入申请的主存大小: " << endl;
cin >> Maxsize;
init();//初始化
show();//首次展示
while (true)
{
string operate;//操作字符串
LinkList *node = new LinkList;
node->front = NULL;
node->next = NULL;
node->flage = 1;
cout << "请求类型( 请输入申请或删除或over)" << endl;
cin >> operate;
if (operate.compare("over")==0)
{
cout << "此次首次适应算法分配结束!" << endl;
return ;
}
if (operate == "申请")
{
cout << "请求进程名称" << endl;
cin >> node->name;
int flag=0;
LinkList *adjust = new LinkList;
adjust=line;
while(adjust!=NULL)
{
if(adjust->name==node->name)
flag=1;
adjust=adjust->next;
}
if(flag==1)
{
cout<<"名字已经被使用,请重新输入!"<<endl;
cin >> node->name;
}
node->start = 0;
cout << "请求大小" << endl;
cin >> node->size;
LinkList *index = new LinkList;
index = line;
while (index != NULL)
{
if (index->flage==0 && index->size >= node->size)
{
node->start = index->start;
index->start = index->start+node->size;
index->size -= node->size;
if (index->next != NULL)
{
node->next=index->next;
index->next->front=node;
}
node->front = index;
index->next = node;
break;
}
index = index->next;
}
show();
}
else if (operate == "删除")
{
cout << "请求进程名称" << endl;
cin >> node->name;
LinkList *ad = new LinkList;
ad=line;
int flge=0;
while(ad!=NULL)
{
if(ad->name==node->name)
flge=1;
ad=ad->next;
}
if(flge==0)
{
cout << "不存在该进程,请重新输入!" << endl;
cin >> node->name;
}
LinkList *op = new LinkList;
op = line;
while (op != NULL)
{
if (op->name == node->name)
{
LinkList *q = new LinkList;
q->flage = 0;
q->name = "free";
q->start = 0;
q->size = Maxsize;
q->front = NULL;
q->next = NULL;
if (op->front != NULL)
{
q = op->front;
if (op->front->front != NULL)
q->front = op->front->front;
else
q->front = NULL;
}
if (!q->flage) //空 op 空 的情况
{
q->size += op->size;
q->start=min(q->start,op->start);
op->flage=0;
if (op->next != NULL && !op->next->flage)
{
q->size += op->next->size;
q->start=min(q->start,op->next->start);
if (q->next->next->next != NULL)
{
q->next->next->next->front = q;
q->next = q->next->next->next;
}
else
{
q->next = NULL;
op=q->next;
break;
}
}
else if(q->front!=NULL && !q->front->flage)//空 空 op
{
q->size += q->front->size;
q->start=min(q->start,q->front->size);
}
else if (op->next != NULL)
{
q->next = op->next;
op->next->front=q;
}
op = q->next;
cout<<op->name<<" "<<endl;
}
else if (op->next != NULL && !op->next->flage)//op 空
{
op->size += op->next->size;
op->start=min(op->next->start,op->start);
op->flage = 0;
op->name = "free";
if (op->next->next != NULL)
{
op->next->next->front = op;
op->next = op->next->next;
}
else
op->next=NULL;
}
else if (((op->front != NULL && op->front->flage) || (op->front == NULL) && ((op->next != NULL && op->next->flage) || (op->next == NULL))))//单独op
{
op->flage = 0;
op->name = "free";
}
if(op->front != NULL && op->next==NULL && !op->front->flage && node->name == op->name)
{
op->flage = 0;
op->name = "free";
op->front->next=NULL;
}
}
op = op->next;
}
show();
}
}
}
void bestfit()
{
cout << "请输入申请的主存大小: " << endl;
cin >> Maxsize;
init();//初始化
show();//首次展示
while (true)
{
string operate;//操作字符串
LinkList *node = new LinkList;
node->front = NULL;
node->next = NULL;
node->flage = 1;
cout << "请求类型( 请输入申请或删除或 over)" << endl;
cin >> operate;
if (operate.compare("over")==0)
{
cout << "此次最佳适应算法分配结束!" << endl;
return ;
}
if (operate == "申请")
{
cout << "请求进程名称" << endl;
cin >> node->name;
LinkList *adjust = new LinkList;
adjust=line;
int flag=0;
while(adjust!=NULL)
{
if(adjust->name==node->name)
flag=1;
adjust=adjust->next;
}
if(flag==1)
{
cout<<"名字已经被使用,请重新输入"<<endl;
cin >> node->name;
}
node->start = 0;
cout << "请求大小" << endl;
cin >> node->size;
memset(k,0,sizeof(k));
int i = 0;
int ok;
LinkList *order = new LinkList;
order = line;
while (order != NULL)
{
if(!order->flage)
k[i++] = order->size;
order=order->next;
}
sort(k, k + i);
for (int j = 0; j < i; j++)
{
if (k[j] >= node->size)
{
ok = k[j];
break;
}
}
LinkList *q = new LinkList;
q->front = NULL;
q->next = NULL;
q->size = 0;
q->flage = 0;
q->start = 0;
q->name = node->name;
LinkList *p = new LinkList;
p = line;
i = 0;
while (p != NULL)
{
if (p->size==ok)
{
q = p;
break;
}
p = p->next;
}
if (q->size >= node->size)
{
node->start = q->start;
q->start = q->start+node->size;
q->size -= node->size;
cout << node->start << " " << q->size << " " << line->size << endl;
if (q->next != NULL)
{
node->next = q->next;
q->next->front = node;
}
else
{
node->next = NULL;
}
node->front = q;
q->next = node;
}
else
{
cout << "申请失败!!!" << endl;
}
show();
}
else if (operate == "删除")
{
cout << "请求进程名称" << endl;
cin >> node->name;
LinkList *ad = new LinkList;
ad=line;
int flge=0;
while(ad!=NULL)
{
if(ad->name==node->name)
flge=1;
ad=ad->next;
}
if(flge==0)
{
cout << "不存在该进程,请重新输入!" << endl;
cin >> node->name;
}
LinkList *op = new LinkList;
op = line;
while (op != NULL)
{
if (op->name == node->name)
{
cout<<op->name<<" "<<endl;
LinkList *q = new LinkList;
q->flage = 0;
q->name = "free";
q->start = 0;
q->size = Maxsize;
q->front = NULL;
q->next = NULL;
if (op->front != NULL)
{
q = op->front;
cout<<q->name<<" "<<q->flage<<endl;
if (op->front->front != NULL)
q->front = op->front->front;
else
q->front = NULL;
}
if (!q->flage) //空 op 空 的情况
{
q->size += op->size;
q->start=min(q->start,op->start);
op->flage=0;
if (op->next != NULL && !op->next->flage)
{
q->size += op->next->size;
q->start=min(q->start,op->next->start);
if(q->next->next->next != NULL)
{
q->next->next->next->front = q;
q->next = q->next->next->next;
}
else
{
q->next = NULL;
op=q->next;
break;
}
}
else if(q->front!=NULL && !q->front->flage)//空 空 op
{
q->size += q->front->size;
q->start=min(q->start,q->front->size);
}
else if (op->next != NULL)
{
q->next = op->next;
op->next->front=q;
}
op = q->next;
}
else if (op->next != NULL && !op->next->flage)//op 空
{
op->size += op->next->size;
op->start=min(op->next->start,op->start);
op->flage = 0;
op->name = "free";
if (op->next->next != NULL)
{
op->next->next->front = op;
op->next = op->next->next;
}
else
op->next=NULL;
}
else if (((op->front != NULL && op->front->flage) || (op->front == NULL) && ((op->next != NULL && op->next->flage) || (op->next == NULL))))//单独op
{
op->flage = 0;
op->name = "free";
}
if(op->front != NULL && op->next==NULL && !op->front->flage && node->name == op->name)
{
op->flage = 0;
op->name = "free";
op->front->next=NULL;
}
}
op = op->next;
}
show();
}
}
}
int main()
{
cout << " 本次为动态内存分配算法" << endl;
cout << " 请选择算法1.首次适应算法2.最佳适应算法" << endl;
int n;
scanf("%d",&n);
switch(n)
{
case 1:
firstfit();
break;
case 2:
bestfit();
break;
default :
cout<<"请输入正确的序号!"<<endl;
}
return 0;
}