您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 操作系统课程设计——动态异长分区的存储分配与回收算法
//该文件所含代码是课设需要学生自己写的代码和补充的代码,包含部分需要修改的课程设计指导书中的代码,不包含不需修改的代码//1.显示空闲区表voiddisplay_freearea_list(){FREEAREA*p;charbuffer[20];p=p_free_area_list;printf(|--------------------|------------------|\n);printf(|start_address(kB)|size(KB)|\n);printf(|--------------------|------------------|\n);while(p!=NULL){printf(|%d,p-start_address);itoa(p-start_address,buffer,10);print_space(19-strlen(buffer));printf(|%d,p-size);itoa(p-size,buffer,10);print_space(17-strlen(buffer));printf(|\n);p=p-next;};printf(|--------------------|------------------|\n\n);}//2.最先适应分配法:内存释放函数voidFF_release_memory(intstart_address,intsize){EnterCriticalSection(&CS_FREEAREA_LIST);__int64t1,t2;//记录该算法起止时间t1=GetCycleCount();//记录起始时间FREEAREA*temp,*p,*pp;//将空闲区按start_address由小到大排序,以便整合相邻空闲区while(1){intchange=0;p=p_free_area_list;if(p-next!=NULL){if(p-start_addressp-next-start_address){pp=p-next;p-next=pp-next;pp-next=p;p_free_area_list=pp;change=1;}}if(p-next!=NULL){while(p-next-next!=NULL){if(p-next-start_addressp-next-next-start_address){pp=p-next-next;p-next-next=pp-next;pp-next=p-next;p-next=pp;change=1;}p=p-next;}}if(change==0){break;}}//插入空闲区temp=newFREEAREA;p=newFREEAREA;temp-start_address=start_address;temp-size=size;temp-next=NULL;p-next=p_free_area_list;while(p-next!=NULL){if(p-next-start_addresstemp-start_address){temp-next=p-next;p-next=temp;break;}else{p=p-next;}}if(p-next==NULL){p-next=temp;}elseif(temp-next==p_free_area_list){p_free_area_list=temp;}//整合碎片while(1){intchange=0;p=p_free_area_list;if(p==NULL){break;}while(p-next!=NULL){if((p-start_address+p-size)==(p-next-start_address)){p-size=p-next-size+p-size;change=1;if(p-next-next==NULL){free(p-next);p-next=NULL;}else{p-next=p-next-next;}}if(p-next==NULL){break;}else{p=p-next;}}if(change==0){break;}}//整理线程结束后的驻留链表THREAD_RESIDENCE_MEMORY*q;q=p_thread_residence_memory_list;if(q-start_address==start_address){p_thread_residence_memory_list=p_thread_residence_memory_list-next;}else{while(q-next!=NULL){if(q-next-start_address==start_address){if(q-next==tail_thread_residence_memory_list){tail_thread_residence_memory_list=q;}q-next=q-next-next;break;}q=q-next;}}//记录结束时间,并将运行时间存入对应数组t2=GetCycleCount();if(time[0][0]t2-t1){time[0][0]=t2-t1;}if(time[0][1]t2-t1){time[0][1]=t2-t1;}LeaveCriticalSection(&CS_FREEAREA_LIST);}//3.最佳适应分配算法的内存释放函数voidBF_release_memory(intstart_address,intsize){EnterCriticalSection(&CS_FREEAREA_LIST);__int64t1,t2;//记录该算法起止时间t1=GetCycleCount();//记录起始时间FREEAREA*temp,*p,*pp;//将空闲区按start_address由小到大排序,以便整合相邻空闲区while(1){intchange=0;p=p_free_area_list;if(p-next!=NULL){if(p-start_addressp-next-start_address){pp=p-next;p-next=pp-next;pp-next=p;p_free_area_list=pp;change=1;}}if(p-next!=NULL){while(p-next-next!=NULL){if(p-next-start_addressp-next-next-start_address){pp=p-next-next;p-next-next=pp-next;pp-next=p-next;p-next=pp;change=1;}p=p-next;}}if(change==0){break;}}//插入空闲区temp=newFREEAREA;p=newFREEAREA;temp-start_address=start_address;temp-size=size;temp-next=NULL;p-next=p_free_area_list;while(p-next!=NULL){if(p-next-start_addresstemp-start_address){temp-next=p-next;p-next=temp;break;}else{p=p-next;}}if(p-next==NULL){p-next=temp;}elseif(temp-next==p_free_area_list){p_free_area_list=temp;}//整合碎片while(1){intchange=0;p=p_free_area_list;if(p==NULL){break;}while(p-next!=NULL){if((p-start_address+p-size)==(p-next-start_address)){p-size=p-next-size+p-size;change=1;if(p-next-next==NULL){free(p-next);p-next=NULL;}else{p-next=p-next-next;}}if(p-next==NULL){break;}else{p=p-next;}}if(change==0){break;}}//将空闲区按SIZE由小到大排序,以便符合BF算法while(1){intchange=0;p=p_free_area_list;if(p-sizep-next-size){pp=p-next;p-next=pp-next;pp-next=p;p_free_area_list=pp;change=1;}while(p-next-next!=NULL){if(p-next-sizep-next-next-size){pp=p-next-next;p-next-next=pp-next;pp-next=p-next;p-next=pp;change=1;}p=p-next;}if(change==0){break;}}//整理线程结束后的驻留链表THREAD_RESIDENCE_MEMORY*q;q=p_thread_residence_memory_list;if(q-start_address==start_address){p_thread_residence_memory_list=p_thread_residence_memory_list-next;}else{while(q-next!=NULL){if(q-next-start_address==start_address){if(q-next==tail_thread_residence_memory_list){tail_thread_residence_memory_list=q;}q-next=q-next-next;break;}q=q-next;}}//记录结束时间,并将运行时间存入对应数组t2=GetCycleCount();if(time[1][0]t2-t1){time[1][0]=t2-t1;}if(time[1][1]t2-t1){time[1][1]=t2-t1;}LeaveCriticalSection(&CS_FREEAREA_LIST);}//4.最坏适应分配算法:内存释放函数voidWF_release_memory(intstart_address,intsize){EnterCriticalSection(&CS_FREEAREA_LIST);__int64t1,t2;//记录该算法起止时间t1=GetCycleCount();//记录起始时间FREEAREA*temp,*p,*pp;//将空闲区按start_address由小到大排序,以便整合相邻空闲区while(1){intchange=0;p=p_free_area_list;if(p-next!=NULL){if(p-start_addressp-next-start_address){pp=p-next;p-next=pp-next;pp-next=p;p_free_area_list=pp;change=1;}}if(p-next!=NULL){while(p-next-next!=NULL){if(p-next-start_addressp-next-next-start_address){pp=p-next-next;p-next
本文标题:操作系统课程设计——动态异长分区的存储分配与回收算法
链接地址:https://www.777doc.com/doc-7337272 .html