您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 实现内存动态分区分配(首次算法)
1实验报告课程名称:计算机操作系统实验名称:用首次适应算法实现内存动态分区分配任课教师:霍林林专业:计算机科学与技术班级:学号:__姓名:蓝冠恒__完成日期:2009年10月15日一、实验目的:了解和掌握操作系统的动态分区分配的基本原理和实现方法,包括内存的动态分配和内存的回收。二、主要实验内容及要求:用首次适应算法编程实现对操作系统内存动态分区分配,主要功能包括分配内存和回收内存。三、设计思路:1.内存分配:(1)若非法输入或请求内存过大或内存分配块数达到上限,则提示相关信息,需重新输入或等待。(2)若输入数据合法,则按首次适应算法分配内存,将分配的内存地址和大小存BusyTable(内存分配表)中;并在SpareTable(内存空闲管理表)修改剩余内存地址和大小,若是首次分配,需要同时建立这两张表。(3)将刚分配的内存地址写入consumeCombo(等待消费的内存地址下拉框)。2.消费内存(释放内存):(1)从consumeCombo选出可消费内存地址,进行消费。(2)从consumeCombo中删除刚消费的内存地址,并将该地址写入recoverCom(等待回收的内存地址下拉框)。3.回收内存:(1)若回收区同时与插入点的前、后两分区邻接,将三个分区合并,合并后分区地址为此插入点的前一分区地址,大小为三个分区大小之和;然后从SpareTable2表中去掉分区前后一分区的表项。(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;3privateButtonconsumeOK;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;}4protectedvoidCopy(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);5SpareTLength=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);}}6});requestOK.setBounds(229,23,52,29);requestOK.setText(OK);consumeLabel=newLabel(firstComposite,SWT.NONE);consumeLabel.setForeground(SWTResourceManager.getColor(0,0,255));co
本文标题:实现内存动态分区分配(首次算法)
链接地址:https://www.777doc.com/doc-3615854 .html