网站后台数据库管理,杭州网站建设seo优化,wordpress更新是乱码,网站建设方案服务器最佳适应算法
从全部空闲区中找出能满足作业要求的#xff0c;且大小最小的空闲分区#xff0c;这种方法能使碎片尽量小。
问题描述
Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600 KB (in order), how would each of the first-fit, best-fit…最佳适应算法
从全部空闲区中找出能满足作业要求的且大小最小的空闲分区这种方法能使碎片尽量小。
问题描述
Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600 KB (in order), how would each of the first-fit, best-fit, and worst-fit algorithms place processes of 212 KB, 417 KB, 112 KB, and 426 KB (in order)? Which algorithm makes the most efficient use of memory?
问题解决
为212k分配空间 找到第一个跟212k大小最接近的空闲区找到第四个空闲区300212k剩余88k空闲区
为417k分配空间 找到第一个跟417k大小最接近的空闲区找到第二个空闲区500417剩余83k空闲区
为112k分配空间 找到第一个跟112k大小最接近的空闲区找到第三个空闲区200112k剩余88k空闲区
为426k分配空间 找到第一个跟426大小最接近的空闲区找到第五个空闲区600k426剩余74k空闲区code
#includestdio.h
#includestdlib.h#define N 5
#define M 5int buf[N]{100,500,200,300,600};
int need_dis[M]{212,417,112,426,200};
typedef struct LNode *List;struct LNode{int order;int buffer;LNode *next;
};List list_init(){List head,p,m;int i;for(i0;iN;i){if(i0){m(LNode*)malloc(sizeof(struct LNode));if(!m){printf(Error:memory!\n);exit(0);}m-orderi;m-bufferbuf[i];m-nextNULL;headpm;}else{m(LNode*)malloc(sizeof(struct LNode));if(!m){printf(Error:memory wrong\n);exit(0);}m-orderi;m-bufferbuf[i];m-nextNULL;p-nextm;pp-next;}}return head;
}void best_fit(List head){int i,j,k;int min;int order;List p;for(i0;iM;i){min-1;order-1;phead;while(p){if(p-bufferneed_dis[i]){if(min0){minp-buffer-need_dis[i];orderp-order;}else{if(minp-buffer-need_dis[i]){minp-buffer-need_dis[i];orderp-order;}}}pp-next;} if(order-1){printf(\n);printf(分配完成%d个\n,i);printf(第%d个进程失败\n,i1);printf(\n);break; }else{phead;while(p){if(p-orderorder)p-buffer-need_dis[i];pp-next;}}}
}void print(List p){while(p){printf(%d:%d\n,p-order,p-buffer);pp-next;}
}int main(){List p;plist_init();printf(分配内存前\n); print(p);best_fit(p);printf(分配内存后\n);print(p);return 0;
}