基于顺序搜索的动态分区分配算法模拟内存动态分配--最佳适应算法(best fit,BF)
生活随笔
收集整理的這篇文章主要介紹了
基于顺序搜索的动态分区分配算法模拟内存动态分配--最佳适应算法(best fit,BF)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
BF算法、男朋友算法,哈哈
要實(shí)現(xiàn)動態(tài)分區(qū)分配,需要考慮三個方面的問題。分別是數(shù)據(jù)結(jié)構(gòu)、分區(qū)分配算法、分區(qū)的分配與回收操作。
首數(shù)據(jù)結(jié)構(gòu)
這里我們使用的是空閑分區(qū)鏈,采用雙向鏈表表示空閑分區(qū)。
具體實(shí)現(xiàn)如下:
分配算法
采用基于順序搜索的動態(tài)分區(qū)分配算法中的最佳適應(yīng)(best fit BF)算法。
每次為作業(yè)分配內(nèi)存時,總是把能滿足要求,又是最小的空閑分區(qū)分配給作業(yè),避免“大材小用”。
內(nèi)存的分配與回收
- 分配內(nèi)存
從空閑分區(qū)鏈中找到所需大小的分區(qū)。設(shè)請求的分區(qū)大小為u.size,表中每個空閑分區(qū)的大小可表示為m.size,若m.size-u.size>=0時進(jìn)行內(nèi)存分配操作,若大于0,則申請一新節(jié)點(diǎn),插入到雙向鏈表中,若等于0,則只需修改符合要求的結(jié)點(diǎn)的信息就行了。 - 回收內(nèi)存,四種情況(F:要回收的內(nèi)存區(qū)、F1:F的前一分區(qū)、F2:F的后一分區(qū))
- F與F1地址銜接,且F1空閑,將F與F1合并,合并后結(jié)點(diǎn)首地址為F1首地址,末地址為F末地址,結(jié)點(diǎn)數(shù)減一。
- F與F2地址銜接,且F2空閑,將F與F2合并,合并后結(jié)點(diǎn)首地址為F首地址,末地址為F2末地址地址,結(jié)點(diǎn)數(shù)減一。
- F與F1和F2的地址銜接,且F1,F2空閑,合并后結(jié)點(diǎn)首地址為F1首地址,末地址為F2末地址,結(jié)點(diǎn)數(shù)減二。
- 其他情況,將結(jié)點(diǎn)的state標(biāo)志和process標(biāo)志均設(shè)置為0。
程序解釋
int buf[N]={100,500,200,700,300}; //內(nèi)存塊大小,用來初始化空閑分區(qū)鏈表 int add[N]={20,150,700,950,1700,}; //內(nèi)存塊的初始地址,用來初始化空閑分區(qū)鏈表 int dis[N]={301,400,310,105,190}; //進(jìn)程所需內(nèi)存,下標(biāo)記為進(jìn)程編號List list_init(); //用來初始化空閑分區(qū)鏈表的函數(shù),返回空閑分區(qū)鏈表的頭部 void print(List head); //順序輸出鏈表的信息 List allot_memory(List head,ing i);//為編號為i的進(jìn)程分配內(nèi)存 List free_memory(List head,int i);//釋放編號為i的進(jìn)程所占用的內(nèi)存全部代碼
#include<stdio.h> #include<stdlib.h>#define N 5int buf[N]={100,500,200,700,300}; int add[N]={20,150,700,950,1700,}; int dis[N]={301,400,310,105,190}; typedef struct LNode *List;typedef struct LNode{int order;int start;int end;int size;int state; int process;struct LNode *next;struct LNode *pre; }LNode;List list_init(){List head,p,m;int i;for(i=0;i<N;i++){m=(List)malloc(sizeof(struct LNode));if(!m){printf("error\n");exit(0);}m->order=i+1;m->start=add[i];m->end=m->start+buf[i]-1;m->size=buf[i];m->next=NULL;m->pre=NULL;m->state=0;p->process=0;if(i==0)head=p=m;else{p->next=m;m->pre=p;p=p->next;}}return head; }void print(List head){List p=head;while(p){printf("第%d塊內(nèi)存--->始地址:%-5d--->末地址:%-5d--->大小:%-5d--->狀態(tài):",p->order,p->start,p->end,p->size);if(p->state==1)printf("被%d號進(jìn)程占用中!\n",p->process);else if(p->state==0){printf("空閑中!\n");} p=p->next;}printf("\n"); }List free_memory(List head,int i){List p,m,temp;p=head;while(p){if(p->process==i+1){temp=p;if(p->next){m=p->next;if(p->end+1==m->start){if(!m->state){p->size+=m->size;p->end+=m->size;p->next=m->next;p->state=0;p->process=0;if(m->next){m->next->pre=p;}p=m->next;free(m);while(p){p->order--;p=p->next;}}else{p->state=0;p->process=0;}}else{p->state=0;p->process=0;}}p=temp;if(p->pre){m=p->pre;if(p->start==m->end+1){if(!m->state){m->size+=p->size;m->end+=p->size;m->next=p->next;if(p->next){p->next->pre=m;}free(p);p=m->next;while(p){p->order--;p=p->next;}}else{p->state=0;p->process=0;}}else{p->state=0;p->process=0;}}return head;}p=p->next;} }List allot_memory(List head,int i){int memory_size=dis[i];List p=head;List m;int min=-1;int order=-1;while(p){if(p->process-1==i){printf("內(nèi)存中已有%d號進(jìn)程了\n",i+1);return head;}p=p->next;}p=head;while(p){if(p->size>=memory_size&&p->state==0){if(min<0){min=p->size-memory_size;order=p->order;}else{if(min>p->size-memory_size){min=p->size-memory_size;order=p->order;}}}p=p->next;}if(order==-1){printf("%d號進(jìn)程分配內(nèi)存失敗!\n",i+1);return head;}else{p=head;while(p){if(p->order==order){if(p->size==memory_size){p->state=1;p->process=i+1;return head;}else{m=(List)malloc(sizeof(struct LNode));m->order=p->order;m->start=p->start;m->end=m->start+memory_size-1;m->size=memory_size;m->state=1;m->next=p;m->process=i+1;m->pre=p->pre;p->pre->next=m;p->pre=m;p->start=m->end+1;p->size-=memory_size;while(p){p->order++;p=p->next;}return head;}}p=p->next;}} }int main(){List p,m;int choice1,choice2;int i;p=list_init();print(p);p=allot_memory(p,3);print(p);p=allot_memory(p,3);p=free_memory(p,3);print(p);p=allot_memory(p,0);print(p);p=allot_memory(p,4);print(p);p=free_memory(p,4);print(p);p=allot_memory(p,4);print(p);p=free_memory(p,0);print(p);p=free_memory(p,4);print(p);return 0; }總結(jié)
以上是生活随笔為你收集整理的基于顺序搜索的动态分区分配算法模拟内存动态分配--最佳适应算法(best fit,BF)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《日落望江赠荀丞诗》第五句是什么
- 下一篇: 栈的应用--数制转换