洛阳理工学院实验报告
第二篇:操作系统 内存管理代码
程序代码:
#include<iostream>
#include<fstream>
#define MAXMSIZE 99
using namespace std;
struct Node{
int startAddress;
char name;
int size;
int endAddress;
struct Node *prior;
struct Node *next;
};
struct Blocklist
{
Node* head;
};
Blocklist* freelist=new Blocklist;
Blocklist* busylist=new Blocklist;
void initial();
void allocateBlock();
void print();
void reclaimBlock();
void main()
{
int slct;
initial();
print();
cout<<"select allocetion (1) or reclaim (2):"<<endl;
} slct=1; while(slct) { } cin>>slct; switch( slct ) { case 1: allocateBlock(); print(); break; case 2: reclaimBlock(); } print(); break; default: break;
void allocateBlock() //分配分区
{
char nm;
int strtadd,sz;
Node *ftemp,*delt,*btemp;
ftemp=freelist->head ; btemp=busylist->head ; cout<<"plseae input a new block information: "<<endl; cout<<" --name--startAdd--size-- "<<endl; bool bfinish=false; bool ffinish=false; Node* p=new Node; p->name =nm; p->startAddress =strtadd; cin>>nm>>strtadd>>sz; p->size =sz; p->endAddress =p->startAddress +p->size-1 ; if( busylist->head ==NULL && bfinish ==false){ //分配使用链表的头结点 btemp =p; btemp->prior =NULL; btemp->next =NULL; bfinish =true; busylist->head =btemp; } else{ while( btemp !=NULL && bfinish==false) //查找使用链表的可分配点 {
if( btemp->endAddress < p->startAddress && btemp->next ==NULL){ //加入使用链表链尾
else } p->next =NULL; p->prior =btemp; btemp->next =p; bfinish =true; if(btemp->endAddress < p->startAddress && btemp->next->startAddress >p->endAddress ){ //加入使用链表链中
p->next = btemp->next ;
p->prior =btemp; btemp->next->prior =p; btemp->next =p; bfinish =true;
}
else if( btemp->prior ==NULL && btemp->startAddress > p->endAddress && p->startAddress >=0){ //加入使用链表链头
p->next =btemp;
p->prior =NULL;
} } } btemp=btemp->next ; btemp->prior =p; busylist->head =p; bfinish =true;
while( ftemp!=NULL && ffinish==false && bfinish ==true ) //增加结点修改空闲链表 {
if( ftemp->startAddress
ftemp->endAddress >=p->endAddress )
{
} if( ffinish==true) //查找需要删除的结点并删除之 { Node* q=new Node; q->startAddress =p->endAddress +1; q->endAddress =ftemp->endAddress; q->size =q->endAddress- q->startAddress +1; q->prior =ftemp; q->next =ftemp->next ; <=p->startAddress && ftemp->endAddress =p->startAddress-1; ftemp->size =ftemp->endAddress -ftemp->startAddress +1 ; ftemp->next =q ; ffinish =true; } ftemp= ftemp->next;
ftemp =freelist->head ;
while( ftemp!=NULL)
{
if( ftemp->size==0) { if( ftemp == freelist->head ) freelist->head =ftemp->next ; else{
} ftemp->prior ->next =ftemp->next ; ftemp->next ->prior =ftemp->prior ; } delt =ftemp; ftemp =ftemp->next ; delete delt;
else
ftemp =ftemp->next ;
}
void initial() //初始分配
{
cout<<"initial... "<<endl;
Node* fl=new Node;
fl->startAddress =0; fl->endAddress =MAXMSIZE; fl->size =fl->endAddress-fl->startAddress+1 ; fl->prior =NULL; fl->next =NULL; } } cout<<"it has been allocated !"<<endl; else cout<<"it cannot allocate!"<<endl;
freelist->head =fl;
busylist->head =NULL;
}
void print() //打印执行结果
{
Node* ftemp=freelist->head; Node* btemp=busylist->head ; cout<<"freelist information--startadd--endadd--size--"<<endl; while( ftemp!=NULL ){ cout<<" ---"<<ftemp->startAddress } cout<<"busylist information--name--startadd--endadd--size--"<<endl; ftemp=ftemp->next ; <<" ---- "<<ftemp->endAddress <<"----"<<ftemp->size <<endl;
while( btemp!=NULL ){
cout<<" ---"<<btemp->name <<"---"<<btemp->startAddress <<"---"<<btemp->endAddress<<"---"<<btemp->size <<endl;
btemp=btemp->next ;
}
cout<<endl;}
void reclaimBlock() //回收
{
char nm; bool finish=false; Node* ftemp,*btemp; ftemp=freelist->head ; btemp=busylist->head ;
cout<<"please input the name of reclaimed block:"<<endl;
cin>>nm;
while( btemp!=NULL) // 确定要回收的内存空间
{ if( btemp->name==nm)
break;
btemp =btemp->next;
}
while( ftemp!=NULL) // 回收选定的内存空间
{
if( btemp->endAddress < freelist->head->startAddress ) // 要回收的内存在空闲链表之前
btemp->next =freelist->head ; btemp->prior =NULL; freelist->head ->prior =btemp; freelist->head =btemp; freelist->head ->prior =NULL; finish =true; } { if( ftemp->startAddress == btemp->endAddress+1) { ftemp->startAddress = btemp->startAddress ; ftemp->size +=btemp->size ; ftemp->prior =NULL; } else { busylist->head =btemp->next ; if(busylist->head) busylist->head ->prior =NULL;
}
else if( btemp->startAddress > ftemp->endAddress && btemp->endAddress <ftemp->next ->startAddress )
{ //要回收的内存在空闲链表之中
if( btemp->startAddress ==ftemp->endAddress +1 && btemp->endAddress ==ftemp->next->startAddress -1)
} } { //上下皆邻 ftemp->size = ftemp->size +btemp->size +ftemp->next->size; ftemp->endAddress =ftemp->startAddress +ftemp->size -1; if ( ftemp->next ->next) ftemp->next->next->prior =ftemp; ftemp->next = ftemp->next ->next ; } else if(btemp->startAddress ==ftemp->endAddress +1) { //上邻 ftemp->size +=btemp->size ; ftemp->endAddress =ftemp->startAddress +ftemp->size -1; } else if(btemp->endAddress ==ftemp->next->startAddress -1) { //下邻 ftemp->next->startAddress =btemp->startAddress ; ftemp->size =ftemp->next ->endAddress -ftemp->next ->startAddress +1; } else{ //上下都不相邻 btemp->next =ftemp->next ; } ftemp->next ->prior =btemp; ftemp->next =btemp; btemp->prior =ftemp; ftemp=ftemp->next ;
if( !btemp->prior && finish==false )
{
} busylist->head =btemp->next ; if(busylist->head) busylist->head ->prior =NULL; } else if( finish==false){ } if( btemp->next ) btemp->next ->prior =btemp->prior; btemp->prior->next =btemp->next ;
运行结果:
initial...
freelist information--startadd--endadd--size-- ---0 ---- 99----100
busylist information--name--startadd--endadd--size--
select allocetion (1) or reclaim (2):
1
plseae input a new block information:
--name--startAdd--size--
a 20 25
it has been allocated !
freelist information--startadd--endadd--size-- ---0 ---- 19----20
---45 ---- 99----55
busylist information--name--startadd--endadd--size-- ---a---20---44---25
1
plseae input a new block information:
--name--startAdd--size--
b 48 20
it has been allocated !
freelist information--startadd--endadd--size-- ---0 ---- 19----20
---45 ---- 47----3
---68 ---- 99----32
busylist information--name--startadd--endadd--size-- ---a---20---44---25
---b---48---67---20
1
plseae input a new block information:
--name--startAdd--size--
c 70 10
it has been allocated !
freelist information--startadd--endadd--size-- ---0 ---- 19----20
---45 ---- 47----3
---68 ---- 69----2
---80 ---- 99----20
busylist information--name--startadd--endadd--size-- ---a---20---44---25
---b---48---67---20
---c---70---79---10
2
please input the name of reclaimed block: b
freelist information--startadd--endadd--size-- ---0 ---- 19----20
---45 ---- 69----25
---80 ---- 99----20
busylist information--name--startadd--endadd--size-- ---a---20---44---25
---c---70---79---10