《2022年操作系统内存管理代码 2.pdf》由会员分享,可在线阅读,更多相关《2022年操作系统内存管理代码 2.pdf(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、程序代码:#include#include#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()
2、;void reclaimBlock();void main()int slct;initial();print();coutselect allocetion(1)or reclaim(2):slct;switch(slct)case 1:allocateBlock();print();break;case 2:reclaimBlock();print();break;default:break;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 8 页 -void allocateBlock()/分配分区 char nm;int strtadd,sz;Node*ftemp,*de
3、lt,*btemp;ftemp=freelist-head;btemp=busylist-head;coutplseae input a new block information:endl;cout-name-startAdd-size-nmstrtaddsz;bool bfinish=false;bool ffinish=false;Node*p=new Node;p-name=nm;p-startAddress=strtadd;p-size=sz;p-endAddress=p-startAddress+p-size-1;if(busylist-head=NULL&bfinish=fals
4、e)/分配使用链表的头结点btemp=p;btemp-prior=NULL;btemp-next=NULL;bfinish=true;busylist-head=btemp;else while(btemp!=NULL&bfinish=false)/查找使用链表的可分配点 if(btemp-endAddress startAddress&btemp-next=NULL)/加入使用链表链尾p-next=NULL;p-prior=btemp;btemp-next=p;bfinish=true;else if(btemp-endAddress startAddress&btemp-next-star
5、tAddress p-endAddress)/加入使用链表链中p-next=btemp-next;p-prior=btemp;btemp-next-prior=p;btemp-next=p;bfinish=true;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 8 页 -else if(btemp-prior=NULL&btemp-startAddress p-endAddress&p-startAddress=0)/加入使用链表链头p-next=btemp;p-prior=NULL;btemp-prior=p;busylist-head=p;bfinish=true;btem
6、p=btemp-next;while(ftemp!=NULL&ffinish=false&bfinish=true)/增加结点修改空闲链表 if(ftemp-startAddress startAddress&ftemp-endAddress=p-endAddress)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;ftemp-endAddress=p-s
7、tartAddress-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 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 8 页 -ftemp-prior-next=fte
8、mp-next;ftemp-next-prior=ftemp-prior;delt=ftemp;ftemp=ftemp-next;delete delt;else ftemp=ftemp-next;coutit has been allocated!endl;else coutit cannot allocate!endl;void initial()/初始分配 coutinitial.startAddress=0;fl-endAddress=MAXMSIZE;fl-size=fl-endAddress-fl-startAddress+1;fl-prior=NULL;fl-next=NULL;
9、freelist-head=fl;busylist-head=NULL;void print()/打印执行结果 Node*ftemp=freelist-head;Node*btemp=busylist-head;coutfreelist information-startadd-endadd-size-endl;while(ftemp!=NULL)cout-startAddress -endAddress-size next;coutbusylist information-name-startadd-endadd-size-endl;while(btemp!=NULL)cout-name-s
10、tartAddress-endAddress-size next;couthead;btemp=busylist-head;coutplease input the name of reclaimed block:nm;while(btemp!=NULL)/确定要回收的内存空间 if(btemp-name=nm)break;btemp=btemp-next;while(ftemp!=NULL)/回收选定的内存空间 if(btemp-endAddress head-startAddress)/要回收的内存在空闲链表之前 if(ftemp-startAddress=btemp-endAddress
11、+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;名师资料总结-精品资料欢迎下载-名师精心整理
12、-第 5 页,共 8 页 -else if(btemp-startAddress ftemp-endAddress&btemp-endAddress 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(fte
13、mp-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=fte
14、mp-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
15、=btemp-prior;btemp-prior-next=btemp-next;名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 8 页 -运行结果: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
16、 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 informa
17、tion-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 名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 8 页 -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 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 8 页 -
限制150内