您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > linux内存管理实验报告
操作系统实验报告院别:XXXXXX班级:XXXXXX学号:XXXXXX姓名:稻草人实验题目:内存管理实验一、实验目的1、通过本次试验体会操作系统中内存的分配模式;2、掌握内存分配的方法(FF,BF,WF);3、学会进程的建立,当一个进程被终止时内存是如何处理被释放块,并当内存不满足进程申请时是如何使用内存紧凑;4、掌握内存回收过程及实现方法;5、学会进行内存的申请释放和管理;二、实验内容附源代码:/*宏定义*/#includestdio.h#includestdlib.h#includesys/types.h#definePROCESS_NAME_LEN32/*进程名称的最大长度*/#defineMIN_SLICE10/*最小碎片的大小*/#defineDEFAULT_MEM_SIZE1024/*默认内存的大小*/#defineDEFAULT_MEM_START0/*默认内存的起始位置*//*内存分配算法*/#defineMA_FF1#defineMA_BF2#defineMA_WF3intmem_size=DEFAULT_MEM_SIZE;/*内存大小*/intma_algorithm=MA_FF;/*当前分配算法*/intflag=0;/*设置内存大小标志*/staticintpid=0;/*初始pid*/intalgorithm;/*描述每一个空闲块的数据结构*/structfree_block_type{intsize;intstart_addr;structfree_block_type*next;};/*指向内存中空闲块链表的首指针*/structfree_block_type*free_block;/*每个进程分配到的内存块的描述*/structallocated_block{intpid;intsize;intstart_addr;charprocess_name[PROCESS_NAME_LEN];structallocated_block*next;};/*进程分配内存块链表的首指针*/structallocated_block*allocated_block_head=NULL;structallocated_block*find_process(intid){structallocated_block*p;p=allocated_block_head;while(p!=NULL){if(p-pid==id)returnp;}returnNULL;}voidswap(int*p,int*q){inttemp;temp=*p;*p=*q;*q=temp;return;}voiddo_exit(){exit(0);}/*初始化空闲块,默认为一块,可以指定大小及起始地址*/structfree_block_type*init_free_block(intmem_size){structfree_block_type*fb;fb=(structfree_block_type*)malloc(sizeof(structfree_block_type));if(fb==NULL){printf(Nomem\n);returnNULL;}fb-size=mem_size;fb-start_addr=DEFAULT_MEM_START;fb-next=NULL;returnfb;}/*显示菜单*/display_menu(){printf(\n);printf(1-Setmemorysize(default=%d)\n,DEFAULT_MEM_SIZE);printf(2-Selectmemoryallocationalgorithm\n);printf(3-Newprocess\n);printf(4-Terminateaprocess\n);printf(5-Displaymemoryusage\n);printf(0-Exit\n);}/*设置内存的大小*/set_mem_size(){intsize;if(flag!=0){//防止重复设置printf(Cannotsetmemorysizeagain\n);return0;}printf(Totalmemorysize=);scanf(%d,&size);if(size0){mem_size=size;free_block-size=mem_size;}flag=1;return1;}/*按FF算法重新整理内存空闲块链表*/rearrange_FF(){structfree_block_type*tmp,*work;printf(RearrangefreeblocksforFF\n);tmp=free_block;while(tmp!=NULL){work=tmp-next;while(work!=NULL){if(work-start_addrtmp-start_addr){/*地址递增*/swap(&work-start_addr,&tmp-start_addr);swap(&work-size,&tmp-size);}work=work-next;}tmp=tmp-next;}}/*按BF最佳适应算法重新整理内存空闲块链表*/rearrange_BF(){structfree_block_type*tmp,*work;printf(RearrangefreeblocksforBF\n);tmp=free_block;while(tmp!=NULL){work=tmp-next;while(work!=NULL){if(work-sizetmp-size){/*地址递增*/swap(&work-start_addr,&tmp-start_addr);swap(&work-size,&tmp-size);}work=work-next;}tmp=tmp-next;}}/*按WF算法重新整理内存空闲块链表*/rearrange_WF(){structfree_block_type*tmp,*work;printf(RearrangefreeblocksforWF\n);tmp=free_block;while(tmp!=NULL){work=tmp-next;while(work!=NULL){if(work-sizetmp-size){/*地址递增*/swap(&work-start_addr,&tmp-start_addr);swap(&work-size,&tmp-size);}elsework=work-next;}tmp=tmp-next;}}/*按指定的算法整理内存空闲块链表*/rearrange(intalgorithm){switch(algorithm){caseMA_FF:rearrange_FF();break;caseMA_BF:rearrange_BF();break;caseMA_WF:rearrange_WF();break;}}/*设置当前的分配算法*/set_algorithm(){printf(\t1-FirstFit\n);printf(\t2-BestFit\n);printf(\t3-WorstFit\n);scanf(%d,&algorithm);if(algorithm=1&&algorithm=3)ma_algorithm=algorithm;//按指定算法重新排列空闲区链表rearrange(ma_algorithm);}/*分配内存模块*/intallocate_mem(structallocated_block*ab){structfree_block_type*fbt,*pre,*temp,*work;intrequest_size=ab-size;fbt=free_block;while(fbt!=NULL){if(fbt-size=request_size){if(fbt-size-request_size=MIN_SLICE)/*分配后空闲空间足够大,则分割*/{mem_size-=request_size;fbt-size-=request_size;ab-start_addr=fbt-start_addr;fbt-start_addr+=request_size;}elseif(((fbt-size-request_size)MIN_SLICE)&&((fbt-size-request_size)0))/*分割后空闲区成为小碎片,一起分配*/{mem_size-=fbt-size;pre=fbt-next;ab-start_addr=fbt-start_addr;fbt-start_addr+=fbt-size;free(fbt);}else{temp=free_block;while(temp!=NULL){work=temp-next;if(work!=NULL)/*如果当前空闲区与后面的空闲区相连,则合并*/{if(temp-start_addr+temp-size==work-start_addr){temp-size+=work-size;temp-next=work-next;free(work);continue;}}temp=temp-next;}fbt=free_block;break;}rearrange(algorithm);/*重新按当前的算法排列空闲区*/return1;}pre=fbt;fbt=fbt-next;}return-1;}/*创建新的进程,主要是获取内存的申请数量*/new_process(){structallocated_block*ab;intsize;intret;ab=(structallocated_block*)malloc(sizeof(structallocated_block));if(!ab)exit(-5);ab-next=NULL;pid++;sprintf(ab-process_name,PROCESS-%02d,pid);ab-pid=pid;printf(Memoryfor%s:,ab-process_name);scanf(%d,&size);if(size0)ab-size=size;ret=allocate_mem(ab);/*从空闲区分配内存,ret==1表示分配ok*//*如果此时allocated_block_head尚未赋值,则赋值*/if((ret==1)&&(allocated_block_head==NULL)){allocated_block_head=ab;return1;}/*分配成功,将该已分配块的描述插入已分配链表*/elseif(ret==1){ab-next=allocated_block_head;allocated_block_head=ab;return2;}elseif(ret==-1){/*分配不成功*/printf(Allocationfail\n);free(ab);return-1;}return3;}/*将ab所表示的已分配区归还,并进行可能的合并*/intfree_mem(structallocated_block*ab){intalgorithm=ma_algorithm;structfree_block_type*fbt,*work;fbt=(structfree_block_type*)malloc(sizeof(structfree_block_type));if(!fbt)return-1;fbt-size=ab-size;fbt-start_addr=ab-start_addr;/*插入到空闲区链表的头部并将空闲区按地址递增的次序排列*/fbt-next=free_block;
本文标题:linux内存管理实验报告
链接地址:https://www.777doc.com/doc-1892424 .html