您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 存储管理---动态不等长存储资源分配算法
#includewindows.h#includeconio.h#includestdlib.h#includestdio.h#includeio.h#includestring.h#defineMAX_THREAD3#defineBF_initialize_require_memory_listFF_initialize_require_memory_list#defineWF_initialize_require_memory_listFF_initialize_require_memory_list#defineBF_initialize_thread_residence_memory_listFF_initialize_thread_residence_memory_list#defineWF_initialize_thread_residence_memory_listFF_initialize_thread_residence_memory_list#defineWF_delete_freearea_listFF_delete_freearea_list#defineBF_delete_freearea_listFF_delete_freearea_list#defineWF_delete_require_memory_listFF_delete_require_memory_list#defineBF_delete_require_memory_listFF_delete_require_memory_list#defineWF_delete_thread_residence_memory_listFF_delete_thread_residence_memory_list#defineBF_delete_thread_residence_memory_listFF_delete_thread_residence_memory_listtypedefstructfreearea{//表示空闲区域的数据结构structfreearea*next;//指向下一个结点的指针intstart_address;//空闲区起始地址intsize;//空闲区大小}FREEAREA;typedefstructrequire_memory{//记录线程申请内存的数据结构structrequire_memory*next;//指向下一个结点的指针charthread_name[10];//线程名intsize;//申请内存大小(以KB为单位)intduration;//在内存的驻留时间(以秒为单位)}REQUIRE_MEMORY;typedefstructthread_residence_memory{//描述线程驻留区的数据结构structthread_residence_memory*next;//指向下一个结点的指针charthread_name[10];//线程名intstart_address;//驻留区起始地址intsize;//驻留区大小}THREAD_RESIDENCE_MEMORY;FREEAREAinit_free_area_table[5]={//测试数据:初始空闲区表{NULL,10,10},{NULL,40,30},{NULL,80,5},{NULL,145,15},{NULL,180,20}};REQUIRE_MEMORYinit_thread_require_memory_table[3]={//测试数据:初始内存申请表{NULL,thread_1,20,4},{NULL,thread_2,10,5},{NULL,thread_3,5,6}};//测试数据:初始线程驻留区表THREAD_RESIDENCE_MEMORYinit_thread_residence_memory_table[5]={{NULL,a,0,10},{NULL,b,20,20},{NULL,c,70,10},{NULL,d,85,60},{NULL,e,160,20}};FREEAREA*p_free_area_list=NULL;//空闲区链首REQUIRE_MEMORY*p_thread_require_memory_queue=NULL;//内存申请队列队首THREAD_RESIDENCE_MEMORY*p_thread_residence_memory_list=NULL;//线程驻留区链首THREAD_RESIDENCE_MEMORY*tail_thread_residence_memory_list=NULL;//线程驻留区链尾CRITICAL_SECTIONCS_THREAD_MEMORY_LIST;//保护线程驻留区链表的临界区CRITICAL_SECTIONCS_SCREEN;//保护屏幕的临界区CRITICAL_SECTIONCS_FREEAREA_LIST;//保护空闲区链表的临界区HANDLEh_thread[MAX_THREAD];//线程句柄数组voidprint_space(intnum);//输出若干个空格voiddisplay_thread_residence_memory_list();//显示线程驻留区表//最先适应分配法的函数FREEAREA*FF_initialize_freearea_list(FREEAREA*init_table,intnum);//初始化空闲区链表voidFF_delete_freearea_list();//删除空闲区链表REQUIRE_MEMORY*FF_initialize_require_memory_list(REQUIRE_MEMORY*init_table,intnum);//初始化内存申请链表voidFF_delete_require_memory_list();//删除内存申请链表THREAD_RESIDENCE_MEMORY*FF_initialize_thread_residence_memory_list(THREAD_RESIDENCE_MEMORY*init_table,intnum);//初始化线程驻留区链表voidFF_delete_thread_residence_memory_list();//删除线程驻留区链表voidFF_thread(void*data);//线程函数intFF_require_memory(intsize);//内存申请函数voidFF_release_memory(intstart_address,intsize);//内存释放函数voidFF();//最先适应分配算法的初始化函数//最佳适应分配算法的函数voidBF_thread(void*data);//线程函数intBF_require_memory(intsize);//内存申请函数voidBF_release_memory(intstart_address,intsize);//内存释放函数voidBF_insert_freearea(FREEAREA*free_node);//空闲区结点插入函数voidBF();//初始化程序voidBF_initialize_freearea_list(FREEAREA*init_table,intnum);//初始化空闲区链表//最坏适应分配算法的函数voidWF_thread(void*data);//线程函数voidWF_insert_freearea(FREEAREA*free_node);//空闲区结点插入函数voidWF_initialize_freearea_list(FREEAREA*init_table,intnum);//初始化空闲区链表intWF_require_memory(intsize);//内存申请函数voidWF_release_memory(intstart_address,intsize);//内存释放函数voidWF();intmain(intargc,char*argv[]){charselect;while(1){printf(|-----------------------------------|\n);printf(|1:firstfitallocation|\n);printf(|2:bestfitallocation|\n);printf(|3:worstfitallocation|\n);printf(|4:exit|\n);printf(|-----------------------------------|\n);printf(selectafunction(1~4):);do{select=(char)getch();}while(select!='1'&&select!='2'&&select!='3'&&select!='4');system(cls);switch(select){case'1':FF();break;case'2':BF();break;case'3':WF();break;case'4':return0;}printf(\nPressanykeytoreturntomainmenu.);getch();system(cls);}return0;}voidprint_space(intnum){//显示若干个空格inti;for(i=0;inum;i++){printf();}}voiddisplay_thread_residence_memory_list(){//显示驻留线程链表THREAD_RESIDENCE_MEMORY*p;charbuffer[20];p=p_thread_residence_memory_list;printf(|-------------------|--------------------|------------------|\n);printf(|thread_name|start_address(kB)|size(KB)|\n);printf(|-------------------|--------------------|------------------|\n);while(p!=NULL){printf(|%s,p-thread_name);print_space(18-strlen(p-thread_name));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);}//最先适应分配法:初始化空闲区链表FREEAREA*FF_initialize_freearea_list(FREEAREA*init_table,intnum){FREEAREA*temp;FREEAREA*head=NULL;FREEAREA*tail=NULL;inti;for(i=0;inum;i++){temp=(FREEAREA*)malloc(sizeof(FREEAREA));temp-start_address=init_table[i].start_address;temp-size=init_table[i].size;temp-next=NULL;if(head==NULL)head=tail=temp;else{tail-next=temp;tail=t
本文标题:存储管理---动态不等长存储资源分配算法
链接地址:https://www.777doc.com/doc-6450101 .html