实验四 动态分区分配算法文档格式.docx
《实验四 动态分区分配算法文档格式.docx》由会员分享,可在线阅读,更多相关《实验四 动态分区分配算法文档格式.docx(14页珍藏版)》请在冰点文库上搜索。
![实验四 动态分区分配算法文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-4/28/914b28ba-06ab-4959-b6cf-7d7bc37fd341/914b28ba-06ab-4959-b6cf-7d7bc37fd3411.gif)
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;
设//置算法case'
3'
new_process();
flag=1;
break创;
//建新进程case'
4'
kill_process();
break删;
//除进程
5'
display_mem_usage();
flag=1;
/显/示内存使用
0'
do_exit();
exit(0);
//释放链表并退出default:
}
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;
start_addr=DEFAULT_MEM_START;
fb->
next=NULL;
returnfb;
voiddisplay_menu(){printf("
\n"
1-Setmemorysize(default=%d)\n"
DEFAULT_MEM_SIZE);
printf("
2-Selectmemoryallocationalgorithm\n"
3-Newprocess\n"
4-Terminateaprocess\n"
5-Displaymemoryusage\n"
0-Exit\n"
intset_mem_size(){intsize;
if(flag!
=0){ //防止重复设置
Cannotsetmemorysizeagain\n"
return0;
Totalmemorysize="
scanf("
%d"
&
size);
if(size>
0){mem_size=size;
free_block->
voidset_algorithm(){intalgorithm;
while
(1){
\t1-FirstFit\n"
\t2-BestFit\n"
\t3-WorstFit\n"
algorithm);
if(algorithm>
=1&
&
algorithm<
=3){ma_algorithm=algorithm;
else
输入有误,请重新输入!
//按指定算法重新排列空闲区链表rearrange(ma_algorithm);
voidrearrange(intalgorithm){switch(algorithm){
caseMA_FF:
rearrange_FF();
caseMA_BF:
rearrange_BF();
caseMA_WF:
rearrange_WF();
//首次适应算法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){min_addr=temp->
p=temp;
temp=temp->
next;
if(NULL!
=p){
temp=p->
p->
next=p->
temp->
next=free_block;
free_block=temp;
thead=free_block;
p=free_block;
temp=free_block->
while(thead->
min_addr=thead->
while(temp->
if(p->
=thead->
next){temp=p->
next=thead->
thead->
next=temp;
thead=thead->
p=thead;
temp=thead->
//最佳适应算法intrearrange_BF(){
//使用头插法,thead为临时头,p为最小内存的数据块的前一个结点
//当前的最小内存intmin_size=free_block->
size;
size<
min_size){min_size=temp->
min_size=thead->
//最坏适应算法intrearrange_WF(){
//使用头插法,thead为临时头,p为最大内存的数据块的前一个结点
//当前的最大内存intmax_size=free_block->
size>
max_size){max_size=temp->
while(thead->
=NULL){max_size=thead->
}
intnew_process(){structallocated_block*ab;
intsize;
intret;
ab=(structallocated_block*)malloc(sizeof(structallocated_block));
if(!
ab)exit(-5);
ab->
pid++;
sprintf(ab->
process_name,"
PROCESS-d"
pid);
ab->
pid=pid;
Memoryfor%s:
"
ab->
process_name);
0){
size=size;
elseprintf("
输入大小有误,请重新输入\n"
ret=allocate_mem(ab);
if((ret==1)&
(allocated_block_head==NULL)){allocated_block_head=ab;
elseif(ret==1){
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->
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->
request_size)return-1;
else{
while(NULL!
=fbt&
fbt->
request_size){pre=fbt;
fbt=fbt->
request_size){
=free_block->
next){sum=free_block->
temp=free_block->
while(NULL!
=temp){
sum+=temp->
if(sum>
=request_size)
break;
if(NULL==temp)
return-1;
pre=free_block;
max=free_block->
fbt=free_block;
=pre){if(max<
start_addr){
max=pre->
fbt=pre;
pre=pre->
=pre){
tp=allocated_block_head;
tt=free_block;
if(pre!
=fbt){
=tp){
if(tp->
start_addr>
start_addr)
tp->
start_addr=tp->
start_addr-pre->
tp=tp->
=tt){
if(tt->
tt->
start_addr=tt->
tt=tt->
while(pre!
=temp->
next){
=fbt)
free(pre);
pre=pre->
free_block=fbt;
free_block->
size=sum;
next=temp->
if(free_block->
size-request_size<
MIN_SLICE){ab->
size=free_block->
start_addr=free_block->
pre=free_block;
free_block=free_block->
start_addr=fbt->
start_addr+request_size;
size-request_size;
//将内存块全部分配
if(fbt->
size=fbt->
if(pre->
next==free_block){
free_block=fbt->
next=fbt->
free(fbt);
fbt->
free(head);
rearrange(ma_algorithm);
voidkill_process(){structallocated_block*ab;
intpid;
KillProcess,pid="
pid);
ab=find_process(pid);
if(ab!
=NULL){
free_mem(ab);
dispose(ab);
没有pid为%d的进程!
pid);
structallocated_block*find_process(intpid){
structallocated_block*ab=NULL;
ab=allocated_block_head;
=ab&
pid!
=pid)
ab=ab->
returnab;
intfree_mem(structallocated_block*ab){intalgorithm=ma_algorithm;
structfree_block_type*fbt,*pre=NULL,*head;
fbt=(structfree_block_type*)malloc(sizeof(structfree_block_type));
pre=(structfree_block_type*)malloc(sizeof(structfree_block_type));
fbt)return-1;
//进行可能的合并,基本策略如下
//1.将新释放的结点插入到空闲分区队列末尾
//2.对空闲链表按照地址有序排列
//3.检查并合并相邻的空闲分区
//4.将空闲链表重新按照当前算法排序
head=pre;
start_addr=ab->
size=ab->
//新释放的结点插入到空闲分区链表的表头
rearrange_FF();
//对空闲链表按照地址有序排列
//求的pre为fbt的前一个结点
size=0;
while(pre->
start_addr!
=fbt->
start_addr)pre=pre->
//左右分区都存在
if(0!
=pre->
size&
NULL!
//左右分区都可合并
if((pre->
start_addr+pre->
size)==fbt->
start_addr&
(fbt->
start_addr+fbt->
size