操作系统 内存管理代码.doc_第1页
操作系统 内存管理代码.doc_第2页
操作系统 内存管理代码.doc_第3页
操作系统 内存管理代码.doc_第4页
操作系统 内存管理代码.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

此文档收集于网络,如有侵权,请联系网站删除程序代码:#include#include#define MAXMSIZE 99using namespace std;struct Nodeint 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(); coutselect allocetion (1) or reclaim (2):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 ;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 =false) /分配使用链表的头结点btemp =p;btemp-prior =NULL;btemp-next =NULL;bfinish =true;busylist-head =btemp;elsewhile( 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-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-prior =p;busylist-head =p;bfinish =true;btemp=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-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 ; delete delt; else ftemp =ftemp-next ;coutit has been allocated !endl;elsecoutit cannot allocate!endl;void initial() /初始分配coutinitial. startAddress =0;fl-endAddress =MAXMSIZE;fl-size =fl-endAddress-fl-startAddress+1 ;fl-prior =NULL;fl-next =NULL;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 -startAddress -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+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;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 ( 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-100busylist information-name-startadd-endadd-size-select allocetion (1) or reclaim (2):1plseae input a new block information: -name-startAdd-size-a 20 25it has been allocated !freelist information-startadd-endadd-size- -0 - 19-20 -45 - 99-55busylist information-name-startadd-endadd-size- -a-20-44-251plseae input a new block information: -name-startAdd-size-b 48 20it has been allocated !freelist information-startadd-endadd-size- -0 - 19-20 -45 - 47-3 -68 - 99-32busylist information-name-startadd-endadd-size- -a-20-44-25 -b-48-67-201plseae input a new block information: -name-startAdd-size-c 70 10it has been allocated !freelist information-startadd-endadd-size- -0 - 19-20 -45 - 47-3 -68 - 69-2 -80 - 99-20busylist in

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论