实验四 动态分区分配算法.docx

上传人:b****7 文档编号:8759000 上传时间:2023-02-01 格式:DOCX 页数:19 大小:18.60KB
下载 相关 举报
实验四 动态分区分配算法.docx_第1页
第1页 / 共19页
实验四 动态分区分配算法.docx_第2页
第2页 / 共19页
实验四 动态分区分配算法.docx_第3页
第3页 / 共19页
实验四 动态分区分配算法.docx_第4页
第4页 / 共19页
实验四 动态分区分配算法.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

实验四 动态分区分配算法.docx

《实验四 动态分区分配算法.docx》由会员分享,可在线阅读,更多相关《实验四 动态分区分配算法.docx(19页珍藏版)》请在冰豆网上搜索。

实验四 动态分区分配算法.docx

实验四动态分区分配算法

实验内容:

存储器管理实验

一、    实验目的

采用首次适应算法(FF),最佳适应算法(BF),最坏适应算法(WF)三种不同的算法,实现对系统空闲区的动态分区分配。

二、实验题目

给予顺序搜索的动态分区算法的程序。

 三、实验要求

读懂给出的核心代码,进行适当的修改,编译通过后,完成实验报告。

四、核心代码

#include

#include

#include

//常量定义

#definePROCESS_NAME_LEN32

#defineMIN_SLICE10

#defineDEFAULT_MEM_SIZE1024

#defineDEFAULT_MEM_START0

#defineMA_FF1

#defineMA_BF2

#defineMA_WF3

intmem_size=DEFAULT_MEM_SIZE;

intma_algorithm=MA_FF;

staticintpid=0;

intflag=0;

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;

//函数声明

structfree_block_type*init_free_block(intmem_size);

voiddisplay_menu();

intset_mem_size();

voidset_algorithm();

voidrearrange(intalgorithm);

intrearrange_FF();

intrearrange_BF();

intrearrange_WF();

intnew_process();

intallocate_mem(structallocated_block*ab);

voidkill_process();

intfree_mem(structallocated_block*ab);

intdispose(structallocated_block*free_ab);

intdisplay_mem_usage();

voiddo_exit();

structallocated_block*find_process(intpid);

intmain(){

charchoice;pid=0;

free_block=init_free_block(mem_size);//初始化空闲区

while

(1){

display_menu();//显示菜单

fflush(stdin);

choice=getchar();//获取用户输入

switch(choice){

case'1':

set_mem_size();break;//设置内存大小

case'2':

set_algorithm();flag=1;break;//设置算法

case'3':

new_process();flag=1;break;//创建新进程

case'4':

kill_process();flag=1;break;//删除进程

case'5':

display_mem_usage();flag=1;break;//显示内存使用

case'0':

do_exit();exit(0);//释放链表并退出

default:

break;

}

}

return1;

}

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;

}

voiddisplay_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");

}

intset_mem_size(){

intsize;

if(flag!

=0){//防止重复设置

printf("Cannotsetmemorysizeagain\n");

return0;

}

printf("Totalmemorysize=");

scanf("%d",&size);

if(size>0){

mem_size=size;

free_block->size=mem_size;

}

flag=1;

return1;

}

voidset_algorithm(){

intalgorithm;

while

(1){

printf("\t1-FirstFit\n");

printf("\t2-BestFit\n");

printf("\t3-WorstFit\n");

scanf("%d",&algorithm);

if(algorithm>=1&&algorithm<=3){

ma_algorithm=algorithm;

break;

}

else

printf("输入有误,请重新输入!

\n");

}

//按指定算法重新排列空闲区链表

rearrange(ma_algorithm);

}

voidrearrange(intalgorithm){

switch(algorithm){

caseMA_FF:

rearrange_FF();break;

caseMA_BF:

rearrange_BF();break;

caseMA_WF:

rearrange_WF();break;

}

}

//首次适应算法

intrearrange_FF(){

structfree_block_type*temp;

//使用头插法,thead为临时头,p为最小地址的数据块的前一个结点

structfree_block_type*thead=NULL,*p=NULL;

//当前的最小地址

intmin_addr=free_block->start_addr;

temp=free_block;

while(temp->next!

=NULL){

if(temp->next->start_addr

min_addr=temp->next->start_addr;

p=temp;

}

temp=temp->next;

}

if(NULL!

=p){

temp=p->next;

p->next=p->next->next;

temp->next=free_block;

free_block=temp;

}

thead=free_block;

p=free_block;

temp=free_block->next;

while(thead->next!

=NULL){

min_addr=thead->next->start_addr;

while(temp->next!

=NULL){

if(temp->next->start_addr

min_addr=temp->next->start_addr;

p=temp;

}

temp=temp->next;

}

if(p->next!

=thead->next){

temp=p->next;

p->next=p->next->next;

temp->next=thead->next;

thead->next=temp;

}

thead=thead->next;

p=thead;

temp=thead->next;

}

return1;

}

//最佳适应算法

intrearrange_BF(){

structfree_block_type*temp;

//使用头插法,thead为临时头,p为最小内存的数据块的前一个结点

structfree_block_type*thead=NULL,*p=NULL;

//当前的最小内存

intmin_size=free_block->size;

temp=free_block;

while(temp->next!

=NULL){

if(temp->next->size

min_size=temp->next->size;

p=temp;

}

temp=temp->next;

}

if(NULL!

=p){

temp=p->next;

p->next=p->next->next;

temp->next=free_block;

free_block=temp;

}

thead=free_block;

p=free_block;

temp=free_block->next;

while(thead->next!

=NULL){

min_size=thead->next->size;

while(temp->next!

=NULL){

if(temp->next->size

min_size=temp->next->size;

p=temp;

}

temp=temp->next;

}

if(p->next!

=thead->next){

temp=p->next;

p->next=p->next->next;

temp->next=thead->next;

thead->next=temp;

}

thead=thead->next;

p=thead;

temp=thead->next;

}

return1;

}

//最坏适应算法

intrearrange_WF(){

structfree_block_type*temp;

//使用头插法,thead为临时头,p为最大内存的数据块的前一个结点

structfree_block_type*thead=NULL,*p=NULL;

//当前的最大内存

intmax_size=free_block->size;

temp=free_block;

while(temp->next!

=NULL){

if(temp->next->size>max_size){

max_size=temp->next->size;

p=temp;

}

temp=temp->next;

}

if(NULL!

=p){

temp=p->next;

p->next=p->next->next;

temp->next=free_block;

free_block=temp;

}

thead=free_block;

p=free_block;

temp=free_block->next;

while(thead->next!

=NULL){

max_size=thead->next->size;

while(temp->next!

=NULL){

if(temp->next->size>max_size){

max_size=temp->next->size;

p=temp;

}

temp=temp->next;

}

if(p->next!

=thead->next){

temp=p->next;

p->next=p->next->next;

temp->next=thead->next;

thead->next=temp;

}

thead=thead->next;

p=thead;

temp=thead->next;

}

return1;

}

intnew_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-d",pid);

ab->pid=pid;

while

(1){

printf("Memoryfor%s:

",ab->process_name);

scanf("%d",&size);

if(size>0){

ab->size=size;

break;

}

elseprintf("输入大小有误,请重新输入\n");

}

ret=allocate_mem(ab);

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");

pid--;

free(ab);

return-1;

}

return3;

}

intallocate_mem(structallocated_block*ab){

structfree_block_type*fbt,*pre,*head,*temp,*tt;

structallocated_block*tp;

intrequest_size=ab->size;

intsum=0;

intmax;

head=(structfree_block_type*)malloc(sizeof(structfree_block_type));

pre=head;

fbt=free_block;

pre->next=fbt;

if(ma_algorithm==MA_WF){

if(NULL==fbt||fbt->size

return-1;

}

else{

while(NULL!

=fbt&&fbt->size

pre=fbt;

fbt=fbt->next;

}

}

if(NULL==fbt||fbt->size

if(NULL!

=free_block->next){

sum=free_block->size;

temp=free_block->next;

while(NULL!

=temp){

sum+=temp->size;

if(sum>=request_size)

break;

temp=temp->next;

}

if(NULL==temp)

return-1;

else{

pre=free_block;

max=free_block->start_addr;

fbt=free_block;

while(temp->next!

=pre){

if(maxstart_addr){

max=pre->start_addr;

fbt=pre;

}

pre=pre->next;

}

pre=free_block;

while(temp->next!

=pre){

tp=allocated_block_head;

tt=free_block;

if(pre!

=fbt){

while(NULL!

=tp){

if(tp->start_addr>pre->start_addr)

tp->start_addr=tp->start_addr-pre->size;

tp=tp->next;

}

while(NULL!

=tt){

if(tt->start_addr>pre->start_addr)

tt->start_addr=tt->start_addr-pre->size;

tt=tt->next;

}

}

pre=pre->next;

}

pre=free_block;

while(pre!

=temp->next){

if(pre!

=fbt)

free(pre);

pre=pre->next;

}

free_block=fbt;

free_block->size=sum;

free_block->next=temp->next;

if(free_block->size-request_size

ab->size=free_block->size;

ab->start_addr=free_block->start_addr;

pre=free_block;

free_block=free_block->next;

free(pre);

}

else{

ab->start_addr=fbt->start_addr;

free_block->start_addr=free_block->start_addr+request_size;

free_block->size=free_block->size-request_size;

}

}

}

else

return-1;

}

else{

//将内存块全部分配

if(fbt->size-request_size

ab->size=fbt->size;

ab->start_addr=fbt->start_addr;

if(pre->next==free_block){

free_block=fbt->next;

}

else

pre->next=fbt->next;

free(fbt);

}

else{

ab->start_addr=fbt->start_addr;

fbt->start_addr=fbt->start_addr+request_size;

fbt->size=fbt->size-request_size;

}

}

free(head);

rearrange(ma_algorithm);

return1;

}

voidkill_process(){

structallocated_block*ab;

intpid;

printf("KillProcess,pid=");

scanf("%d",&pid);

ab=find_process(pid);

if(ab!

=NULL){

free_mem(ab);

dispose(ab);

}

else{

printf("没有pid为%d的进程!

\n",pid);

}

}

structallocated_block*find_process(intpid){

structallocated_blo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1