#includewindows.h#includeconio.h#includestdlib.h#includetime.h#includestdio.h#includeio.h#includestring.h#defineMAX_THREAD3typedefstructfreearea{//表示空闲区域的数据结构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();//显示线程驻留区表voiddisplay_freearea_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();//最先适应分配算法的初始化函数#includevariable_partition.hvoidprint_space(intnum){//显示若干个空格inti;for(i=0;inum;i++){printf();}}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);}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=tail-next;}};returnhead;}//最先适应分配法:删除空闲区链表voidFF_delete_freearea_list(){FREEAREA*temp;temp=p_free_area_list;while(temp!=NULL){temp=p_free_area_list-next;free(p_free_area_list);p_free_area_list=temp;}p_free_area_list=NULL;}//最先适应分配法:初始化内存申请链表REQUIRE_MEMORY*FF_initialize_require_memory_list(REQUIRE_MEMORY*init_table,intnum){REQUIRE_MEMORY*temp;REQUIRE_MEMORY*head=NULL;REQUIRE_MEMORY*tail=NULL;inti;for(i=0;inum;i++){temp=(REQUIRE_MEMORY*)malloc(sizeof(REQUIRE_MEMORY));strcpy(temp-thread_name,init_table[i].thread_name);temp-size=init_table[i].size;temp-duration=init_table[i].duration;temp-next=NULL;if(head==NULL)head=tail=temp;else{tail-next=temp;tail=tail-next;}};returnhead;}//最先适应分配法:删除内存申请链表voidFF_delete_require_memory_list(){REQUIRE_MEMORY*temp;temp=p_thread_require_memory_queue;while(temp!=NULL){temp=p_thread_require_memory_queue-next;free(p_thread_require_memory_queue);p_thread_require_memory_queue=temp;}p_thread_require_memory_queue=NULL;}//最先适应分配法:初始化线程驻留区链表THREAD_RESIDENCE_MEMORY*FF_initialize_thread_residence_memory_list(THREAD_RESIDENCE_MEMORY*init_table,intnum){THREAD_RESIDENCE_MEMORY*temp;THREAD_RESIDENCE_MEMORY*head=NULL;THREAD_RESIDENCE_MEMORY*tail=NULL;inti;for(i=0;inum;i++){temp=(THREAD_RESIDENCE_MEMORY*)malloc(sizeof(THREAD_RESIDENCE