模拟内存分配

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include<iostream>
using namespace std;
//----------------------------
void add_process();
void destroy_process();
void output_free();
void output_busy();
void backup();
//----------------------------
struct free_distribute{
int num;
int addr;
int size;
free_distribute *ahead,*next;
};
struct busy_distribute{
int num;
int addr;
int size;
busy_distribute *ahead,*next;
};
//----------------------------
typedef struct free_distribute A;
typedef struct busy_distribute B;
A *FREE,*temp1;
B *BUSY,*temp2,*tail;
//----------------------------
int main(){
//设置初始空分区表,初始为1整块
temp1 = (A*)malloc(sizeof(A));
FREE = temp1;
FREE->num = 1;
FREE->addr = 0;
FREE->ahead = NULL;
FREE->next = NULL;
FREE->size = 1000;
BUSY = NULL;
//===========
while(1){
int choice;
cout<<"请选择操作: 1、加入新进程2、撤销进程"<<endl;
cout<<" 3、查看空闲分区表4、查看已分配分区表"<<endl;
cout<<" 5、退出"<<endl;
cin>>choice;
switch(choice){
case 1:add_process();break;
case 2:destroy_process();break;
case 3:output_free();break;
case 4:output_busy();break;
case 5:return 0;break;
default:break;
}
}
return 0;
}
//--------------------------------
void add_process(){
temp2 = (B*)malloc(sizeof(B));
//设置进程相干信息
cout<<"请输出进程的名字:"<<endl;
cin>>temp2->num;
cout<<"请输出进程的大小:"<<endl;
cin>>temp2->size;
temp2->ahead = NULL;
temp2->next = NULL;
//申请内存
temp1 = FREE;
while(temp1 != NULL && temp1->size < temp2->size)
temp1 = temp1->next;
if(temp1 == NULL){
cout<<"内存不足,申请失败"<<endl;return;}
else{//找到并修改相关信息
temp2->addr = temp1->addr;
temp1->addr = temp1->addr + temp2->size;
temp1->size = temp1->size - temp2->size;
}
//将新进程插入到已分配分区表的尾巴上
if(BUSY == NULL){//第一个结点单独处理
BUSY = temp2;
tail = BUSY;
}
else{
tail->next = temp2;
temp2->ahead = tail;
tail = tail->next;
}
}
//--------------------------------
void destroy_process(){
temp2 = BUSY;
int tt;
//定位
cout<<"请输出撤销的进程的名字:"<<endl;
cin>>tt;
while(temp2 != NULL && temp2->num != tt)
temp2 = temp2->next;
if(temp2 == NULL)
{cout<<"不存在该进程"<<endl;return;}
//找到了,且是第一个结点
if(temp2->ahead == NULL)
{BUSY = BUSY ->next;if(BUSY != NULL)BUSY->ahead = NULL;}
else if(temp2->next == NULL) //最后一个
{temp2->ahead->next = temp2->next;}
else //在中间
{temp2->ahead->next = temp2->next;temp2->next->ahead = temp2->ahead;} //归还内存
backup();
//撤销进程
free(temp2);
}
void backup(){
temp1 = FREE;
int i = 0;//表示从temp1后的空闲分区的序号是该加还是该减以及数目
bool mark = false;
while(temp1 != NULL && temp1->addr <= temp2->addr){
//前空分区后回收区
if(temp1->size + temp1->addr == temp2->addr){
i = 0;
//还要检查是不是前空分区中回收区后空分区
if(temp2 ->addr + temp2->size == temp1->next->addr )
i = 1;
if(i == 0)
temp1 ->size = temp1->size + temp2->size;
if(i == 1){
A *q;
q = temp1->next;//保村删除结点
temp1 ->size = temp1->size + temp2->size + temp1->next->size;
temp1->next = temp1->next->next;//
if(temp1->next != NULL)
temp1->next->ahead = temp1;
free(q);//
}
mark = true;
break;
}//if
temp1 = temp1->next;
}
if(mark == false){
temp1 = FREE;
while(temp1 != NULL){
if(temp1->addr == temp2->addr + temp2->size){
temp1->addr = temp2->addr;
temp1->size = temp1->size + temp2->size;
mark = true;
i = 0;
break;
}
temp1 = temp1->next;
}//while
}//if
//不是上面情况
if(mark == false){
A *p;
p = (A*)malloc(sizeof(A));
//设置临时变量,大小,地址等于temp2
p->addr = temp2->addr;
p->size = temp2->size;
p->ahead = NULL;
p->next = NULL;
//插入新的分区.再头结点的情况,特殊处理
temp1 = FREE;
//再头结点的情况,特殊处理。

相关文档
最新文档