实现内存动态分区分配首次算法.docx

上传人:b****7 文档编号:11514342 上传时间:2023-03-02 格式:DOCX 页数:32 大小:444.59KB
下载 相关 举报
实现内存动态分区分配首次算法.docx_第1页
第1页 / 共32页
实现内存动态分区分配首次算法.docx_第2页
第2页 / 共32页
实现内存动态分区分配首次算法.docx_第3页
第3页 / 共32页
实现内存动态分区分配首次算法.docx_第4页
第4页 / 共32页
实现内存动态分区分配首次算法.docx_第5页
第5页 / 共32页
点击查看更多>>
下载资源
资源描述

实现内存动态分区分配首次算法.docx

《实现内存动态分区分配首次算法.docx》由会员分享,可在线阅读,更多相关《实现内存动态分区分配首次算法.docx(32页珍藏版)》请在冰豆网上搜索。

实现内存动态分区分配首次算法.docx

实现内存动态分区分配首次算法

实验报告

课程名称:

计算机操作系统

实验名称:

用首次适应算法实现内存动态分区分配

任课教师:

霍林林专业:

计算机科学与技术

班级:

学号:

__

姓名:

蓝冠恒__完成日期:

2009年10月15日

一、实验目的:

了解和掌握操作系统的动态分区分配的基本原理和实现方法,包括内存的动态分配和内存的回收。

二、主要实验内容及要求:

用首次适应算法编程实现对操作系统内存动态分区分配,主要功能包括分配内存和回收内存。

三、设计思路:

1.内存分配:

(1)若非法输入或请求内存过大或内存分配块数达到上限,则提示相关信息,需重新输入或等待。

(2)若输入数据合法,则按首次适应算法分配内存,将分配的内存地址和大小存BusyTable(内存分配表)中;并在SpareTable(内存空闲管理表)修改剩余内存地址和大小,若是首次分配,需要同时建立这两张表。

(3)将刚分配的内存地址写入consumeCombo(等待消费的内存地址下拉框)。

2.消费内存(释放内存):

(1)从consumeCombo选出可消费内存地址,进行消费。

(2)从consumeCombo中删除刚消费的内存地址,并将该地址写入recoverCom(等待回收的内存地址下拉框)。

3.回收内存:

(1)若回收区同时与插入点的前、后两分区邻接,将三个分区合并,合并后分区地址为此插入点的前一分区地址,大小为三个分区大小之和;然后从SpareTable表中去掉分区前后一分区的表项。

(2)若回收区与插入点的前分区邻接,则将前分区与回收分区合并,合并后的分区地址为此前前一分区的内存地址,大小为合并前两者大小之和,SpareTable表长度不变。

(3)若回收区与插入点的后分区邻接,则将后分区与回收分区合并,合并后的分区地址为回收的内存地址,大小为合并前两者大小之和,SpareTable表长度不变。

(4)若都不满足上述情况,这是应在SpareTable表插入点处单独建立一个表项,填写回收的地址和大小。

(5)执行以上任何一步后,立即从BusyTable中删除刚回收的内存所在的表项,同时从recoverCombo中删除刚回收的内存的地址。

(6)显示回收过程以及回收的内存地址和大小、合并后的内存地址和大小的信息。

四、实验结果与结论:

(经调试正确的源程序和程序的运行结果)

编程员:

蓝冠恒

程序源代码:

packagemyPackage;

importorg.eclipse.swt.widgets.Display;

importorg.eclipse.swt.widgets.Shell;

importorg.eclipse.swt.widgets.Composite;

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.widgets.Label;

importorg.eclipse.swt.widgets.Text;

importorg.eclipse.swt.widgets.Button;

importorg.eclipse.swt.widgets.Combo;

importorg.eclipse.swt.events.SelectionAdapter;

importorg.eclipse.swt.events.SelectionEvent;

importcom.swtdesigner.SWTResourceManager;

publicclassMemoryManagement{

protectedShellshell;

privateCompositemainComposite;

privateCompositefirstComposite;

privateLabelrequestLabel;

privateTextrequestText;

privateButtonrequestOK;

privateLabelconsumeLabel;

privateComboconsumeCombo;

privateButtonconsumeOK;

privateLabelrecoverLabel;

privateComborecoverCombo;

privateButtonrecoverOK;

privateLabelinformationLabel;

privateTextinformationText;

privateLabelCanUseLabel;

privatefinalintMinLimitSize=200;

SpareNodeSpareTable[];

privatestaticintSpareTLength=0;

BusyNodeBusyTable[];

privatestaticintBusyTLength=0;

privateTextCanUseText;

privateTextHaveUseText;

privateLabelHaveUseLabel;

privatefinalstaticintMaxProcessNum=100;

publicclassSpareNode{

privateintAddress;

privateintSize;

protectedvoidCopy(SpareNodeSN){

this.Address=SN.Address;

this.Size=SN.Size;

}

protectedSpareNode(intaddress,intsize){

super();

Address=address;

Size=size;

}

protectedintgetAddress(){returnAddress;}

protectedvoidsetAddress(intaddress){Address=address;}

protectedintgetSize(){returnSize;}

protectedvoidsetSize(intsize){Size=size;}

}

publicclassBusyNode{

privateintAddress;

privateintSize;

privatebooleanISConsume;

protectedBusyNode(intaddress,intsize){

super();

Address=address;

Size=size;

ISConsume=false;

}

protectedvoidCopy(BusyNodeBN){

this.Address=BN.Address;

this.Size=BN.Size;

this.ISConsume=BN.ISConsume;

}

protectedintgetAddress(){returnAddress;}

protectedvoidsetAddress(intaddress){Address=address;}

protectedbooleangetIsISConsume(){returnISConsume;}

protectedvoidsetISConsume(booleanconsume){ISConsume=consume;}

protectedintgetSize(){returnSize;}

protectedvoidsetSize(intsize){Size=size;}

}

publicstaticvoidmain(String[]args){

try{

MemoryManagementwindow=newMemoryManagement();

window.open();

}catch(Exceptione){

e.printStackTrace();

}

}

/*Openthewindow.*/

publicvoidopen(){

Displaydisplay=Display.getDefault();

createContents();

shell.open();

shell.layout();

while(!

shell.isDisposed()){

if(!

display.readAndDispatch()){

display.sleep();

}

}

}

/**Createcontentsofthewindow.*/

protectedvoidcreateContents(){

shell=newShell();

shell.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));

shell.setSize(680,418);

shell.setText("操作系统内存管理");

SpareTable=newSpareNode[MaxProcessNum+1];

SpareTable[0]=newSpareNode(0,20000);

SpareTLength=1;

BusyTable=newBusyNode[MaxProcessNum];

mainComposite=newComposite(shell,SWT.NONE);

mainComposite.setBounds(0,0,664,382);

firstComposite=newComposite(mainComposite,SWT.BORDER);

firstComposite.setBounds(0,0,295,382);

requestLabel=newLabel(firstComposite,SWT.NONE);

requestLabel.setForeground(SWTResourceManager.getColor(0,0,255));

requestLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

requestLabel.setBounds(0,25,97,27);

requestLabel.setText("申请资源大小");

requestText=newText(firstComposite,SWT.BORDER);

requestText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));

requestText.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

requestText.setBounds(103,26,104,27);

requestOK=newButton(firstComposite,SWT.NONE);

requestOK.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));

requestOK.addSelectionListener(newSelectionAdapter(){

publicvoidwidgetSelected(SelectionEvente){

intsize=0;booleanY=false;

if(BusyTLength==MaxProcessNum){

informationText.setText("");

informationText.insert("进程数量达到上限,不能再申请!

");

}

else{try{size=Integer.parseInt(requestText.getText());

requestText.setText("");

}catch(Exceptionex){

Y=true;

informationText.setText("");

informationText.insert("输入有误,请重新输入(整数)!

\n");

}

if(!

Y)AllocateResources(size);

}

}

});

requestOK.setBounds(229,23,52,29);

requestOK.setText("OK");

consumeLabel=newLabel(firstComposite,SWT.NONE);

consumeLabel.setForeground(SWTResourceManager.getColor(0,0,255));

consumeLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

consumeLabel.setBounds(0,59,97,27);

consumeLabel.setText("消费资源地址");

consumeCombo=newCombo(firstComposite,SWT.READ_ONLY);

consumeCombo.setFont(SWTResourceManager.getFont("微软雅黑",10,SWT.NORMAL));

consumeCombo.setBounds(103,61,104,27);

consumeOK=newButton(firstComposite,SWT.NONE);

consumeOK.addSelectionListener(newSelectionAdapter(){

publicvoidwidgetSelected(SelectionEvente){

Stringstr=consumeCombo.getText();

informationText.setText("");

try{intConsumeAddress=Integer.parseInt(str);

Consume(ConsumeAddress);

}catch(Exceptionex){}

}

});

consumeOK.setBounds(229,58,52,27);

consumeOK.setText("OK");

recoverLabel=newLabel(firstComposite,SWT.NONE);

recoverLabel.setForeground(SWTResourceManager.getColor(0,0,255));

recoverLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

recoverLabel.setBounds(0,99,97,27);

recoverLabel.setText("回收资源地址");

recoverCombo=newCombo(firstComposite,SWT.READ_ONLY);

recoverCombo.setForeground(SWTResourceManager.getColor(0,255,0));

recoverCombo.setFont(SWTResourceManager.getFont("微软雅黑",10,SWT.NORMAL));

recoverCombo.setBounds(103,101,104,27);

recoverOK=newButton(firstComposite,SWT.NONE);

recoverOK.addSelectionListener(newSelectionAdapter(){

publicvoidwidgetSelected(SelectionEvente){

Stringstr=recoverCombo.getText();

booleanIsNull=false;

intRecoverAddress=0;

informationText.setText("");

try{RecoverAddress=Integer.parseInt(str);

}catch(Exceptionex){IsNull=true;}

if(!

IsNull)Recover(RecoverAddress);

}

});

recoverOK.setBounds(229,101,52,27);

recoverOK.setText("OK");

informationLabel=newLabel(firstComposite,SWT.CENTER);

informationLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

informationLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

informationLabel.setBounds(0,142,253,27);

informationLabel.setText("操作信息提示");

informationText=newText(firstComposite,SWT.BORDER|SWT.READ_ONLY|SWT.H_SCROLL|SWT.V_SCROLL|SWT.CANCEL);

informationText.setForeground(SWTResourceManager.getColor(255,0,0));

informationText.setFont(SWTResourceManager.getFont("微软雅黑",10,SWT.NORMAL));

informationText.setBounds(10,175,271,193);

CanUseLabel=newLabel(mainComposite,SWT.HORIZONTAL|SWT.SHADOW_IN|SWT.CENTER);

CanUseLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));

CanUseLabel.setForeground(SWTResourceManager.getColor(0,0,255));

CanUseLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

CanUseLabel.setBounds(624,9,40,167);

CanUseLabel.setText("可\n用\n内\n存\n状\n态\n信\n息");

CanUseText=newText(mainComposite,SWT.BORDER|SWT.READ_ONLY|SWT.H_SCROLL|SWT.V_SCROLL|SWT.CANCEL);

CanUseText.setForeground(SWTResourceManager.getColor(0,0,255));

CanUseText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

CanUseText.setBounds(310,10,316,167);

HaveUseText=newText(mainComposite,SWT.BORDER|SWT.READ_ONLY|SWT.H_SCROLL|SWT.V_SCROLL|SWT.CANCEL);

HaveUseText.setForeground(SWTResourceManager.getColor(0,0,205));

HaveUseText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

HaveUseText.setBounds(310,183,316,189);

HaveUseLabel=newLabel(mainComposite,SWT.CENTER);

HaveUseLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));

HaveUseLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));

HaveUseLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL));

HaveUseLabel.setBounds(624,183,40,188);

HaveUseLabel.setText("内\n存\n占\n用\n情\n况\n信\n息");

}//createContents

publicvoidAllocateResources(intsize){

booleansuccess=false;

if(SpareTLength==1){

intAddressTemp=SpareTable[0]

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 韩语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1