操作系统内存分配算法模拟实现.docx
《操作系统内存分配算法模拟实现.docx》由会员分享,可在线阅读,更多相关《操作系统内存分配算法模拟实现.docx(15页珍藏版)》请在冰豆网上搜索。
![操作系统内存分配算法模拟实现.docx](https://file1.bdocx.com/fileroot1/2022-11/17/6c9bba7f-0030-4dec-baa1-178d50be8090/6c9bba7f-0030-4dec-baa1-178d50be80901.gif)
操作系统内存分配算法模拟实现
实验名称
内存分配与回收算法实现
同组人姓名
实验性质
□基本操作•验证性□综合性□设计性
实验日期
2010-5-17
实验成绩
教师评价:
实验预习口实验操作口实验结果口实验报告口其它口
教师签名:
一、实验目的及要求
1)掌握为实现多道程序并发执行,操作系统是如何通过作业调度选择作业进
入内存
2)系统如何为进入内存的作业分配内存空间,实现多道作业冋时驻留内存,
就绪进程队列中的多个进程是如何以分式方式共享CPU作业运行完成离开
系统时,系统如何进行内存回收,计算进程周转时间。
3)掌握各种调度算法,以及实现所需的各种数据结构。
二、实验内容
根据给定的动态分区分配算法流程图,用你熟悉的计算机编程语言编写一程序,该程序实现内存的合理分配后回收。
三、主要设备及软件
PCWindows2000操作系统、Linux操作系统
四、实验流程、操作步骤或核心代码、算法片段
2、算法模拟实现
1相关数据结构定义
空闲分区块类:
classFreeBlock
空闲分区链类:
classFreeList
内存分配回收算法类:
classMemoryManager
测试类(主类):
classTestForMemManage
2具体实现
请允许我先列出核心部分,内存分配回收算法类的实现:
packagecom.kaiping.memorymanage;//个人包
importjava.util.Scanner;
publicclassMemoryManager{
FreeListflist;//空闲分区类对象
publicMemoryManager(){
flist=newFreeList();
flist.InitFBlock();
}
publicvoidmemAllocation(intsize,Stringnew_job_name){//内存分酉己
(首次适应算法)
FreeBlockq=flist.head;
FreeBlockp=flist.head.next;
while(p!
=null){
if(size<=0){
System.out.println("\n申请的空间不能小于1!
");
break;
}
if(p.state==false&&p.size>=size){
q=newFreeBlock(p.size-size);
p.size=size;
p.state=true;
p.job_name=new_job_name;
q.next=p.next;
p.next=q;
break;//完成分配
}
else
{
p=p.next;//移动到足够分配的空闲块
}
}
if(p==null){
if(flist.flistsize>=size){
System.out.println(”目前尚无足够大的空闲块,系统将进行
重定位操作...");
relocation();//重定向
memAllocation(size,new」ob_name);//重新分配内存
}
else{
System.out.println(”作业"+new_job_name+"内存尚未分配成
功!
”);
}
}
else{//分配内存后可能存在大小为0的空间,将其清除
System.out.println(”作业"+new_job_name+"内存分配成功!
”);
p=flist.head.next;
〃q=flist.head;
while(p!
=null){
if(p.size==0){
flist.deleteFBlock(p);
}
p=p.next;
}
}
}
privatevoidmemRecovery(FreeBlocktarget){//内存回收
FreeBlockp=flist.head.next;
while(p!
=null){
//回收区与插入点的前一个空闲分区相邻接
if(p.next==target&&p.state==false){
p.size+=target.size;
p.next=target.next;
//回收区同时与插入点的前后两个空闲分区相邻接
if(!
p.next.state){
p.size+=p.next.size;
p.next=p.next.next;
}
break;
}
if(p==target){
//回收区与插入点的后一空闲分区相邻接
if(!
p.next.state){
target.size+=p.next.size;target.next=p.next.next;
}
break;//若两不邻接,则直接跳出
}
p=p.next;
}
}
privatevoidrelocation(){//
FreeBlockfront_r=flist.head;
FreeBlockr=front_r.next;
FreeBlockbehind_r=r.next;
空闲资源重定向,回收空闲空间
//
//当前重定向空闲块
while(r!
=null){
//将r定位到第一块空闲分区块
if(r.state==false){
break;
r=r.next;
behind_r=r.next;
front」=front_r.next;//记录第一块空闲分区的上一块
}
while(behind_r!
=null){
if(behind_r.state){
front_r.next=behind_r;
r.next=behind_r.next;
behind_r.next=r;
front_r=behind_r;
}
else{
r.size+=behind_r.size;
r.next=behind_r.next;
}
behind_r=r.next;
}
System.out.println(”重定向成功,继续为作业分配内存...");
}
publicvoidaddJob(){//添加作业
intnewSize;//新作业所需内存大小
StringnJobName=newString("");
Scannerscanner=newScanner(System.in);
System.out.print("请输入新任务的名称:
");
nJobName=scanner.nextLine();
System.out.print("请输入新任务所需内存大小:
");
newSize=scanner.nextInt();
memAllocation(newSize,nJobName);
}
publicvoiddelJob(){//销毁作业
Stringcur_job_name=newString("");
booleanflag=false;
//指示作业是否删除成功
FreeBlockq=flist.head.next;
Scannerscanner=newScanner(System.in);
System.out.print("请输入需要回收的作业名称:
”);
cur_job_name=scanner.nextLine();while(q!
=null){
if(q.job_name==cur_job_name){q.state=false;
q.job_name="";memRecovery(q);//回收内存
flag=true;break;
}
else{
q=q.next;
}
}
if(flag){
System.out.println("
}
else{
System.out.println("
}
}
publicvoidprintJobInfo(){//
FreeBlockp=flist.head.next;
//找到要删除的作业的下一个结点
删除作业成功!
");
删除作业未成功!
");
打印作业信息
intpro_num=1;//用户程序号
intmemnum=1;//内存分区块号
System.out.println(”用户程序信息");
while(p!
=null){if(p.state){
System.out.println(”用户程序
"+pro_num+"("+p.job_name+")"
+"\t占用第"+mem_num+分区块”);
pro_num++;
}
mem_num++;
p=p.next;
}
}
publicvoidprintFreeSubarealnfo(){//打印空闲分区信息
FreeBlockp=flist.head.next;
intleav_size=0;II剩余内存大小
intmem_num=1;II内存分区块号
System.out.println(”空闲分区信息");
System.out.println("\t分区块号\t大小");
while(p!
=null){
if(!
p.state){
System.out.println("\t"+mem_num+"\t"+p.size);
leav_size+=p.size;
}
mem_num++;
p=p.next;
}
System.out.println(”剩余内存总打小为"+leav_size);
}
}
其它类的实现
空闲分区块类:
packagecom.kaiping.memorymanage;
publicclassFreeBlock{
intsize;II空闲块大小
booleanstate;IIfalse表示空闲,true表示已经装入作业
Stringjob_name;//装入的作业名称
FreeBlocknext;//下一空闲块的自引用
publicFreeBlock(ints){
size=s;
state=false;
job_name=newString("");
next=null;
}
}
空闲分区链类:
packagecom.kaiping.memorymanage;
importjava.util.Scanner;
publicclassFreeList{
FreeBlockfblock;
FreeBlockhead;
intfblockNum;//空闲块数
intsumMemCount;//内存总大小
intflistsize;//空闲分区总和
publicFreeList