操作系统内存分配算法模拟实现Word下载.docx
《操作系统内存分配算法模拟实现Word下载.docx》由会员分享,可在线阅读,更多相关《操作系统内存分配算法模拟实现Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
![操作系统内存分配算法模拟实现Word下载.docx](https://file1.bdocx.com/fileroot1/2022-11/17/6c9bba7f-0030-4dec-baa1-178d50be8090/6c9bba7f-0030-4dec-baa1-178d50be80901.gif)
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;
//完成分配
else
{
p=p.next;
//移动到足够分配的空闲块
if(p==null){
if(flist.flistsize>
System.out.println(”目前尚无足够大的空闲块,系统将进行
重定位操作..."
);
relocation();
//重定向
memAllocation(size,new」ob_name);
//重新分配内存
else{
System.out.println(”作业"
+new_job_name+"
内存尚未分配成
功!
”);
else{//分配内存后可能存在大小为0的空间,将其清除
内存分配成功!
p=flist.head.next;
〃q=flist.head;
if(p.size==0){
flist.deleteFBlock(p);
privatevoidmemRecovery(FreeBlocktarget){//内存回收
FreeBlockp=flist.head.next;
=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;
if(p==target){
//回收区与插入点的后一空闲分区相邻接
target.size+=p.next.size;
target.next=p.next.next;
//若两不邻接,则直接跳出
privatevoidrelocation(){//
FreeBlockfront_r=flist.head;
FreeBlockr=front_r.next;
FreeBlockbehind_r=r.next;
空闲资源重定向,回收空闲空间
//
//当前重定向空闲块
while(r!
//将r定位到第一块空闲分区块
if(r.state==false){
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;
r.size+=behind_r.size;
System.out.println(”重定向成功,继续为作业分配内存..."
publicvoidaddJob(){//添加作业
intnewSize;
//新作业所需内存大小
StringnJobName=newString("
Scannerscanner=newScanner(System.in);
System.out.print("
请输入新任务的名称:
nJobName=scanner.nextLine();
请输入新任务所需内存大小:
"
newSize=scanner.nextInt();
memAllocation(newSize,nJobName);
publicvoiddelJob(){//销毁作业
Stringcur_job_name=newString("
booleanflag=false;
//指示作业是否删除成功
FreeBlockq=flist.head.next;
请输入需要回收的作业名称:
”);
cur_job_name=scanner.nextLine();
while(q!
if(q.job_name==cur_job_name){q.state=false;
q.job_name="
;
memRecovery(q);
//回收内存
flag=true;
break;
q=q.next;
if(flag){
System.out.println("
publicvoidprintJobInfo(){//
//找到要删除的作业的下一个结点
删除作业成功!
删除作业未成功!
打印作业信息
intpro_num=1;
//用户程序号
intmemnum=1;
//内存分区块号
System.out.println(”用户程序信息"
=null){if(p.state){
System.out.println(”用户程序
+pro_num+"
("
+p.job_name+"
)"
+"
\t占用第"
+mem_num+分区块”);
pro_num++;
mem_num++;
publicvoidprintFreeSubarealnfo(){//打印空闲分区信息
intleav_size=0;
II剩余内存大小
intmem_num=1;
II内存分区块号
System.out.println(”空闲分区信息"
\t分区块号\t大小"
p.state){
\t"
+mem_num+"
+p.size);
leav_size+=p.size;
System.out.println(”剩余内存总打小为"
+leav_size);
其它类的实现
publicclassFreeBlock{
intsize;
II空闲块大小
booleanstate;
IIfalse表示空闲,true表示已经装入作业
Stringjob_name;
//装入的作业名称
FreeBlocknext;
//下一空闲块的自引用
publicFreeBlock(ints){
size=s;
state=false;
job_name=newString("
next=null;
publicclassFreeList{
FreeBlockfblock;
FreeBlockhead;
intfblockNum;
//空闲块数
intsumMemCount;
//内存总大小
intflistsize;
//空闲分区总和
publicFreeList