实验内容: 假设内存容量为256KB,并且划分成2KB大小的块,也即每个内存单元为2KB。一个进程所需要的内存为3到10个单元。同时假设一个作业在运行过程中所需内存的大小不变。 模拟包括3部分:1)实现特定的内存分配算法2)实现内存回收模拟3)每种内存分配策略对应的碎片数统计
操 作 系 统 课 程 实 验 报 告
实验内容: 假设内存容量为256KB,并且划分成2KB大小的块,也即每个内存单元为2KB。一个进程所需要的内存为3到10个单元。同时假设一个作业在运行过程中所需内存的大小不变。 模拟包括3部分:1)实现特定的内存分配算法2)实现内存回收模拟3)每种内存分配策略对应的碎片数统计
(1)首次适应算法实现 从空闲分区表的第一个表目起查找该表,把最先能够满足要求的空闲区分配给作业,这种方法目的在于减少查找时间。为适应这种 算法,空闲分区表(空闲区链)中的空闲分区要按地址由低到高迚行排序。该算法优先使用低址部分空闲区,在低址空间造成许多 小的空闲区,在高地址空间保留大的空闲区。 (2)最佳适应算法实现 它从全部空闲区中找出能满足作业要求的、且大小最小的空闲分区,这种方法能使碎片尽量小。为适应此算法,空闲分区表(空闲 区链)中的空闲分区要按从小到大迚行排序,自表头开始查找到第一个满足要求的自由分区分配。 数据结构体分析 (3)内存回收 将释放作业所在内存块的状态改为空闲状态,删除其作业名,设置为空。并判断该空闲块是否不其他空闲块相连,若释放的内存空 间不空闲块相连时,则合并为同一个空闲块,同时修改分区大小及
起始地址。
首次适配算法流程图:
最佳适配算法流程图:
实验内容: 假设内存容量为256KB,并且划分成2KB大小的块,也即每个内存单元为2KB。一个进程所需要的内存为3到10个单元。同时假设一个作业在运行过程中所需内存的大小不变。 模拟包括3部分:1)实现特定的内存分配算法2)实现内存回收模拟3)每种内存分配策略对应的碎片数统计
内存回收:
#include <stdio.h> #include <stdlib.h> #include <iostream.h> #define M 20 #define N 101 #define max_size 128 int i = 0,choose; int id= 0,size = 0,countOfEmpty = 1,countOfNotEmpty = 0,num = 1;
实验内容: 假设内存容量为256KB,并且划分成2KB大小的块,也即每个内存单元为2KB。一个进程所需要的内存为3到10个单元。同时假设一个作业在运行过程中所需内存的大小不变。 模拟包括3部分:1)实现特定的内存分配算法2)实现内存回收模拟3)每种内存分配策略对应的碎片数统计
int Time[N]={1};//迚程分配搜索空闲区次数 struct Empty{ int start; int size; int yesornot; }empty[M]; struct not_Empty{ int id; int size; int start; int yesornot; }not_empty[N]; int cmp(const void *p,const void *q){ int c = (*(struct Empty*)p).start - (*(struct Empty*)q).start; int a = (*(struct Empty*)p).yesornot - (*(struct Empty*)q).yesornot; if(a > 0) return -1; else if(a == 0){ if(c > 0) return 1; else return -1; } else return 1; } int cmp1(const void *p,const void *q){ int c = (*(struct not_Empty*)p).start - (*(struct not_Empty*)q).start; int a = (*(struct not_Empty*)p).yesornot - (*(struct not_Empty*)q).yesornot; if(a > 0) return -1; else if(a == 0){ if(c > 0) return 1; else return -1; } else return 1; } int cmp2(const void *p,const void *q){ int c = (*(struct Empty*)p).size - (*(struct Empty*)q).size; int a = (*(struct Empty*)p).yesornot - (*(struct Empty*)q).yesornot; if(a > 0)
实验内容: 假设内存容量为256KB,并且划分成2KB大小的块,也即每个内存单元为2KB。一个进程所需要的内存为3到10个单元。同时假设一个作业在运行过程中所需内存的大小不变。 模拟包括3部分:1)实现特定的内存分配算法2)实现内存回收模拟3)每种内存分配策略对应的碎片数统计
return -1; else if(a == 0){ if(c > 0) return 1; else return -1; } else return 1; } void state(){ int j = 0,f = 0,k = 0,x = 0, t = 0,a,flog =0,debris = 0;; qsort(not_empty, countOfNotEmpty, sizeof(struct not_Empty), cmp1); qsort(empty, countOfEmpty, sizeof(struct Empty), cmp); for(i = 0; i < countOfEmpty; i++){ if(empty[i].yesornot == 0) j++; } countOfEmpty-=j; for(i = 0; i < countOfNotEmpty; i++){ if(not_empty[i].yesornot == 0) f++; } countOfNotEmpty-=f; if(not_empty[0].start == 0) flog = 1; while(1){ if(flog == 1){ for(f = t; f < countOfNotEmpty; f++){ if(not_empty[f].start%11 == 0) printf("\n"); printf("%2d",not_empty[f].id); for( j = not_empty[f].start+1; j < not_empty[f].size + not_empty[f].start; j++){
if( j%11 == 0&&j!=0) printf("\n+ "); else printf("+ "); } if(not_empty[f].start not_empty[f].size == max_size -2) break; if(not_empty[f].start + not_empty[f].size == empty[x].start){ t = f+1; + not_empty[f].size == max_size||not_empty[f].start +
实验内容: 假设内存容量为256KB,并且划分成2KB大小的块,也即每个内存单元为2KB。一个进程所需要的内存为3到10个单元。同时假设一个作业在运行过程中所需内存的大小不变。 模拟包括3部分:1)实现特定的内存分配算法2)实现内存回收模拟3)每种内存分配策略对应的碎片数统计
break; }
} } for( j = empty[x].start; j < empty[x].size + empty[x].start; j++){ if( j%11 == 0&&j!=0) printf("\n●"); else printf("●"); } flog = 1; if(empty[x].size + empty[x].start == max_size) break; x++; } printf("\n 空闲分区列表:\n"); …… 此处隐藏:5988字,全部文档内容请下载后查看。喜欢就下载吧 ……
