操作系统 内存管理代码.docx
- 文档编号:6889335
- 上传时间:2023-01-12
- 格式:DOCX
- 页数:10
- 大小:16.14KB
操作系统 内存管理代码.docx
《操作系统 内存管理代码.docx》由会员分享,可在线阅读,更多相关《操作系统 内存管理代码.docx(10页珍藏版)》请在冰豆网上搜索。
操作系统内存管理代码
操作系统内存管理代码
程序代码:
#include
#include
#defineMAXMSIZE99
usingnamespacestd;
structNode{
intstartAddress;
charname;
intsize;
intendAddress;
structNode*prior;
structNode*next;
};
structBlocklist
{
Node*head;
};
Blocklist*freelist=newBlocklist;
Blocklist*busylist=newBlocklist;
voidinitial();
voidallocateBlock();
voidprint();
voidreclaimBlock();
voidmain()
{
intslct;
initial();
print();
cout<<"selectallocetion
(1)orreclaim
(2):
"< slct=1; while(slct) { cin>>slct; switch(slct) { case1: allocateBlock(); print();break; case2: reclaimBlock(); print();break; default: break; } } } voidallocateBlock()//分配分区 { charnm; intstrtadd,sz; Node*ftemp,*delt,*btemp; ftemp=freelist->head; btemp=busylist->head; cout<<"plseaeinputanewblockinformation: "< cout<<"--name--startAdd--size--"< cin>>nm>>strtadd>>sz; boolbfinish=false; boolffinish=false; Node*p=newNode; p->name=nm; p->startAddress=strtadd; 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->next=NULL; p->prior=btemp; btemp->next=p; bfinish=true; } elseif(btemp->endAddress p->next=btemp->next; p->prior=btemp; btemp->next->prior=p; btemp->next=p; bfinish=true; } elseif(btemp->prior==NULL&&btemp->startAddress>p->endAddress&&p->startAddress>=0){//加入使用链表链头 p->next=btemp; p->prior=NULL; btemp->prior=p; busylist->head=p; bfinish=true; } btemp=btemp->next; } } while(ftemp! =NULL&&ffinish==false&&bfinish==true)//增加结点修改空闲链表 { if(ftemp->startAddress<=p->startAddress&&ftemp->endAddress>=p->endAddress) { Node*q=newNode; q->startAddress=p->endAddress+1; q->endAddress=ftemp->endAddress; q->size=q->endAddress-q->startAddress+1; q->prior=ftemp; q->next=ftemp->next; ftemp->endAddress=p->startAddress-1; ftemp->size=ftemp->endAddress-ftemp->startAddress+1; ftemp->next=q; ffinish=true; } ftemp=ftemp->next; } if(ffinish==true)//查找需要删除的结点并删除之 { 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; deletedelt; } else ftemp=ftemp->next; } cout<<"ithasbeenallocated! "< } else cout<<"itcannotallocate! "< } voidinitial()//初始分配 { cout<<"initial..."< Node*fl=newNode; fl->startAddress=0; fl->endAddress=MAXMSIZE; fl->size=fl->endAddress-fl->startAddress+1; fl->prior=NULL; fl->next=NULL; freelist->head=fl; busylist->head=NULL; } voidprint()//打印执行结果 { Node*ftemp=freelist->head; Node*btemp=busylist->head; cout<<"freelistinformation--startadd--endadd--size--"< while(ftemp! =NULL){ cout<<"---"< ftemp=ftemp->next; } cout<<"busylistinformation--name--startadd--endadd--size--"< while(btemp! =NULL){ cout<<"---"< btemp=btemp->next; } cout< voidreclaimBlock()//回收 { charnm; boolfinish=false; Node*ftemp,*btemp; ftemp=freelist->head; btemp=busylist->head; cout<<"pleaseinputthenameofreclaimedblock: "< cin>>nm; while(btemp! =NULL)//确定要回收的内存空间 { if(btemp->name==nm) break; btemp=btemp->next; } while(ftemp! =NULL)//回收选定的内存空间 { if(btemp->endAddress { 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; btemp->next=freelist->head; btemp->prior=NULL; freelist->head->prior=btemp; freelist->head=btemp; freelist->head->prior=NULL; finish=true; } } elseif(btemp->startAddress>ftemp->endAddress&&btemp->endAddress {//要回收的内存在空闲链表之中 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; } elseif(btemp->startAddress==ftemp->endAddress+1) {//上邻 ftemp->size+=btemp->size; ftemp->endAddress=ftemp->startAddress+ftemp->size-1; } elseif(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; } elseif(finish==false){ if(btemp->next) btemp->next->prior=btemp->prior; btemp->prior->next=btemp->next; } } 运行结果: initial... freelistinformation--startadd--endadd--size-- ---0----99----100 busylistinformation--name--startadd--endadd--size-- selectallocetion (1)orreclaim (2): 1 plseaeinputanewblockinformation: --name--startAdd--size-- a2025 ithasbeenallocated! freelistinformation--startadd--endadd--size-- ---0----19----20 ---45----99----55 busylistinformation--name--startadd--endadd--size-- ---a---20---44---25 1 plseaeinputanewblockinformation: --name--startAdd--size-- b4820 ithasbeenallocated! freelistinformation--startadd--endadd--size-- ---0----19----20 ---45----47----3 ---68----99----32 busylistinformation--name--startadd--endadd--size-- ---a---20---44---25 ---b---48---67---20 1 plseaeinputanewblockinformation: --name--startAdd--size-- c7010 ithasbeenallocated! freelistinformation--startadd--endadd--size-- ---0----19----20 ---45----47----3 ---68----69----2 ---80----99----20 busylistinformation--name--startadd--endadd--size-- ---a---20---44---25 ---b---48---67---20 ---c---70---79---10 2 pleaseinputthenameofreclaimedblock: b freelistinformation--startadd--endadd--size-- ---0----19----20 ---45----69----25 ---80----99----20 busylistinformation--name--startadd--endadd--size-- ---a---20---44---25 ---c---70---79---10
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 内存管理代码 内存 管理 代码