广东海洋大学学生实验报告书(学生用表)实验名称存储管理课程名称操作系统课程号学院(系) 软件学院专业软件工程班级学生姓名学号实验地点实验日期一、实验目的修改MINIX操作系统内存管理的源程序,将MINIX的首次适应算法改为最佳适应和最差适应算法,对修改之后的MINIX源代码进行重新编译和重新启动,以测试你修改的正确性。
二、实验内容1、打开Minix3,进入alloc.c所在目录2、修改原算法为最佳适应算法(BEST_FIT)和最差适应算法(WORST_FIT)的程序如下:#include "pm.h"#include <minix/.h>#include <minix/callnr.h>#include <signal.h>#include <stdlib.h>#include "mproc.h"#include "../../kernel/const.h"#include "../../kernel/config.h"#include "../../kernel/type.h"#define NR_HOLES (2*NR_PROCS) /* max # entries in hole table */#define NIL_HOLE (struct hole *) 0PRIV ATE struct hole {struct hole *h_next; /* pointer to next entry on the list */phys_clicks h_base; /* where does the hole begin? */phys_clicks h_len; /* how big is the hole? */} hole[NR_HOLES];PRIV ATE u32_t high_watermark=0;PRIV ATE struct hole *hole_head; /* pointer to first hole */PRIV ATE struct hole *free_slots;/* ptr to list of unused table slots */#define swap_base ((phys_clicks) -1)FORWARD _PROTOTYPE( void del_slot, (struct hole *prev_ptr, struct hole *hp) );FORWARD _PROTOTYPE( void merge, (struct hole *hp) );#define swap_out() (0)GDOU-B-11-112/*===========================================================================* * alloc_mem **===========================================================================*/ PUBLIC phys_clicks alloc_mem(clicks)phys_clicks clicks; /* amount of memory requested */#define USING_BEST_FIT#ifdef USING_BEST_FIT{//先找到第一个满足要求的空洞,//再以第一个为标准寻找最适合的空洞。
//当最适合的空洞完全吻合//就直接划给它,当空洞较大时就切割。
//首先注册目标指针、目标前一个指针、头指针//记录目标大小和目前最适合大小register struct hole *hp;register struct hole *prevAim_ptr;register struct hole *Aim_ptr;phys_clicks old_base;//如果循环一次都没找到//就把可以退出内存的进程赶出去//再循环do{hp = hole_head;prevAim_ptr = NIL_HOLE;Aim_ptr = NIL_HOLE;for(;hp != NIL_HOLE && hp->h_base < swap_base;prevAim_ptr=hp,hp=hp->h_next){//find the best holeif(hp->h_len == clicks){old_base = hp->h_base; /* remember where it started */hp->h_base += clicks; /* bite off */hp->h_len -= clicks; /* ditto */del_slot(prevAim_ptr, hp);/* Delete the hole which used up completely. *//* Remember new high watermark of used memory. */if(hp->h_base > high_watermark)high_watermark = hp->h_base;return(old_base);}//当从没记录过合适内存时//把遇到的第一个合适节点保存在aim中if(hp->h_len > clicks && Aim_ptr == NIL_HOLE){Aim_ptr=hp;}//当找到一个比原来aim更合适的空间//记录新的空间if(hp->h_len > clicks && hp->h_len < Aim_ptr->h_len){//mark it downAim_ptr=hp;}}//we found itif(Aim_ptr != NIL_HOLE){old_base = Aim_ptr->h_base; /* remember where it started */Aim_ptr->h_base += clicks; /* bite off */Aim_ptr->h_len -= clicks; /* ditto *//* Remember new high watermark of used memory. */if(Aim_ptr->h_base > high_watermark)high_watermark = Aim_ptr->h_base;return(old_base);}}while(swap_out());return(NO_MEM);}#endif#ifdef USING_WORST_FIT{//先找到第一个满足要求的空洞,//再以第一个为标准寻找最适合的空洞。
//当最适合的空洞完全吻合//就直接划给它,当空洞较大时就切割。
//首先注册目标指针、目标前一个指针、头指针//记录目标大小和目前最适合大小register struct hole *hp;register struct hole *prevAim_ptr;register struct hole *Aim_ptr;phys_clicks old_base;//如果循环一次都没找到//就把可以退出内存的进程赶出去//再循环;do{hp = hole_head;prevAim_ptr = NIL_HOLE;Aim_ptr = NIL_HOLE;for(;hp != NIL_HOLE && hp->h_base < swap_base;prevAim_ptr=hp,hp=hp->h_next){//当从没记录过合适内存时//把遇到的第一个合适节点保存在aim中if(hp->h_len >= clicks && Aim_ptr == NIL_HOLE){Aim_ptr=hp;}//当找到一个比原来aim更合适的空间//记录新的空间if(hp->h_len >= clicks && hp->h_len > Aim_ptr->h_len){//mark it downAim_ptr=hp;}}//we found bigestif(hp->h_next==NIL_HOLE){old_base =Aim_ptr->h_base;Aim_ptr->h_base+=clicks;Aim_ptr->h_len-=clicks;/* Remember new high watermark of used memory. */if(Aim_ptr->h_base > high_watermark)high_watermark = Aim_ptr->h_base;return(old_base);}}while(swap_out());return(NO_MEM);}#endif3、保持当前目录不变(即\usr\src\servers\pm),输入make命令,编译程序4、重新编译整个minix,生成新的minix映像文件,并将minix映像文件复制到\boot目录之中。
输入如下命令:cd \usr\src\toolsmake imagecp image \boot\test15、测试新操作系统:重启Minix3,按ESC进入如下画面,输入如图最后两行指令进入新系统三、实验总结通过这次实验,我了解了Minix3存储管理的首次适应算法,学会了将其算法修改为最佳适应和最差适应算法,并学会了如何激活新修改的存储管理算法。
进一步理解了操作系统的存储管理,加强了实验能力。
成绩指导教师日期注:请用A4纸书写,不够另附纸。