操作系统实验一可变分区存储管理(含代码)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验一:可变分区存储管理(一)实验题目(二)实验目的1.加深对可变分区的存储管理的理解;2.提高用C语言编制大型系统程序的能力,特别是掌握C语言编程的难点:指针和指针作为函数参数;3.掌握用指针实现链表和在链表上的基本操作。(三)程序代码#includemalloc.h#includestdio.h#includestring.h#definenew(type)(type*)malloc(sizeof(type))typedefstruct_map{unsignedintsize;char*address;struct_map*next;struct_map*prev;}map;typedefmap*pmap;typedefstruct_mem{unsignedinttotalSize;前空闲区释放区后空闲区(a)前空闲区释放区后空闲区(b)前空闲区释放区后空闲区(c)前空闲区释放区后空闲区(d)图2-9释放区与前后空闲区相邻的情况char*space;pmaphead;pmapcMap;}mem;typedefmem*pmem;pmemcreateMem(unsignedintto_size)//创建内存区域{pmemnewMem=new(mem);pmapnewHead=new(map);newMem-totalSize=to_size;newHead-size=to_size;newHead-address=newMem-space;newHead-next=newHead;newHead-prev=newHead;newMem-head=newHead;newMem-cMap=newHead;returnnewMem;}voidfreeMem(pmemm){pmapmap,cMap;pmaphead=m-head;free(map-address);for(map=head;map-next!=head;){cMap=map;map=cMap-next;free(cMap);}free(m);}char*lmalloc(pmemcMem,unsignedintsize)//分配函数{if(size1000){printf(内存容量超出范围!\n);//当需要分配的内存空间已经大于实际空间时出错}else{pmapp=cMem-cMap;char*rAddr;if(size==0)returnNULL;while(p-sizesize){if(p-next==cMem-cMap)returnNULL;p=p-next;}rAddr=p-address;p-size-=size;p-address+=size;if(p-size==0){p-prev-next=p-next;p-next-prev=p-prev;cMem-cMap=p-next;if(cMem-head==p)cMem-head=p-next;if(p-next!=cMem-head)free(p);}else{cMem-cMap=p;}returnrAddr;}}voidlfree(pmemm,unsignedintsize,char*addr)//释放函数{pmapnextMap,prevMap,newMap;if(addrm-space||addr=m-space+m-totalSize){fprintf(stderr,地址越界\n);//释放空间时,大小输入出错return;}nextMap=m-head;while(nextMap-addressaddr){nextMap=nextMap-next;if(nextMap==m-head)break;}prevMap=nextMap-prev;if(nextMap!=m-head&&prevMap-address+prevMap-size==addr)//第一种情况{prevMap-size+=size;if(addr+size==nextMap-address)//第二种情况{prevMap-size+=nextMap-size;prevMap-next=nextMap-next;prevMap-next-prev=prevMap;if(nextMap==m-cMap){m-cMap=prevMap;}free(nextMap);nextMap=NULL;}}else{if(addr+size==nextMap-address)//第三种情况{nextMap-address-=size;nextMap-size+=size;}else//第四种情况{newMap=new(map);newMap-address=addr;newMap-size=size;prevMap-next=newMap;newMap-prev=prevMap;newMap-next=nextMap;nextMap-prev=newMap;if(nextMap==m-head)m-head=newMap;}}}voidprintMem(pmemm)//打印函数{pmapmap=m-head;printf(\空闲内存空间:\n\-----------------------\n\大小起始地址\n);do{if(map==m-cMap)printf(-);elseprintf();printf(%10u%10u\n,map-size,map-address);map=map-next;}while(map!=m-head);printf(-----------------------\n);}voidmain()//主函数{printf(--------------------------------------------------------\n);printf(请选择操作:分配内存(m)or释放内存(f)or打印内存表(p)\n);printf(--------------------------------------------------------\n);typedefenum{cmdMalloc,cmdFree,cmdPrint,cmdHelp,cmdQuit,cmdInvalid}cmdType;pmemm=createMem(1000);charcmd[20];char*addr;unsignedintsize;cmdTypetype;while(1){scanf(%s,cmd);if(cmd[1]=='\0'){switch(cmd[0]){case'm':case'M':type=cmdMalloc;break;case'f':case'F':type=cmdFree;break;case'p':case'P':type=cmdPrint;break;}}else{if(!strcmp(cmd,malloc))type=cmdMalloc;elseif(!strcmp(cmd,free))type=cmdFree;elseif(!strcmp(cmd,print))type=cmdPrint;}switch(type){casecmdMalloc:scanf(%u,&size);lmalloc(m,size);printMem(m);break;casecmdFree:scanf(%u%u,&size,&addr);lfree(m,size,addr);printMem(m);break;casecmdPrint:printMem(m);break;return;}}}(四)程序结果

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功