操作系统课程实验报告_第1页
操作系统课程实验报告_第2页
操作系统课程实验报告_第3页
操作系统课程实验报告_第4页
操作系统课程实验报告_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《操作系统》实验报告学号: 度与内存管理方法。帮助了解在不同的存储管理方式下,应怎样实现主存空间的分配和回收。二、实验要求2、从就绪队首选进程运行:优先权-1/要求运行时间-1要求运行时间=0时,撤销该进程5、规定道数,设置后备队列和挂起状态。若内存中进程少于规定道数,可自动从后备队列调度一作业进入。被挂起进程入挂起队列,设置解挂功能用于将指定挂起进程解挂入就绪队列。表目内容:起址、长度、状态(未分/空表目)PCB指针}10、进程完成后,回收主存,并与相邻空闲分区合并。 三、实验内容选择一个调度算法,实现处理机调度。3、主存储器空间的分配和回收。在可变分区管理方式下,采用最先适应算法实现主存四、实验原理该模拟系统采用java语言实现,要实现的功能有新建进程、进程调度、挂起进程、解waitingsuspend列中的进程的状态为new。CPU每次调度时都从就绪队列中取进程,在进程执行过程中如果下一个操作时IO操作,则将进程插入到waiting队列。在系统运行过程中可以执行进程中的进程从原来的队列中删除并插入到挂起队列。进行解挂操作时将选中的进程从挂起队列中删除并插入该进程原来所处的队列。插入时根据该进程的优先权插入到队列中的合适位置,插入后保持队列按优先权从大到小排最高。当选择按时间片轮转调度时,所有队列都采用先进先出队列,先进先出队列采用一个实现了先进先出。 ➢内存管理该实验基于实验一完成,核心是内存的分配和回收,在实验一的基础上增加内存管理部分,在新建进程的时候增加一个输入内存大小的输入框,在进程进入内存时要分配内存,间大小以及空间使用状态。初始时只有一个空间,当CPU启动时要分配内存,内存分配采用最先适应算法。回收内存时如果有相邻空闲空间,则要进行空闲空间合并。}}1.divDTO:publicclass{privateprivateprivatedivDTOintdivBase;intlength;intdivFlag;publicdivDTO(intdivBase,intlength,intdivFlag){this.divBase=divBase;this.divFlag=divFlag;this.length=length;}publicdivDTO(){}publicvoidsetDivBase(intbase){this.divBase=base;}publicintgetDivBase(){returnthis.divBase;}publicvoidsetLength(intlength){this.length=length;};publicintgetLength(){returnthis.length;}publicvoidsetDivFlag(intflag){this.divFlag=flag;}publicintgetDivFalg(){returnthis.divFlag;}}2.PcbDTO:publicclassPcbDTO{staticfinalintRunning=1;staticfinalintReady=2;staticfinalintWaiting=3;privateStringprocessName;privateintrunTime;privateintprority;privateintprocessState;privateintbase;privateintlimit;privateintpcbFlag;publicPcbDTO(Stringname,inttime,intpro,intbase,intlimit){cessName=name;this.runTime=time;rity=pro;cessState=0;this.limit=limit;this.base=base;}publicPcbDTO(){this.pcbFlag=0;}publicvoidsetProcessName(Stringname){cessName=name;};publicStringgetProcessName(){returnprocessName;}publicvoidsetRunTime(inttime){this.runTime=time;}publicintgetRunTime(){returnthis.runTime;}publicvoidsetPrority(intprority){rity=prority;}publicintgetPrority(){returnrity;}publicvoidsetProcessState(intstate){cessState=state;}publicStringgetProcessState(){Strings=newString();if(cessState==1){s="running";}elseif(cessState==2){s="ready";}elseif(cessState==3){s="waiting";}returns;} publicintgetBase(){returnthis.base;}publicvoidsetBase(intbase){this.base=base;}publicvoidsetLimit(intlimit){this.limit=limit;}publicintgetLimit(){returnthis.limit;}}3.importjavax.swing.*;importjava.util.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.event.*;publicclassMainFrame{privateJListreadyList;privateJListwaitingList;privateJListjobList;privateJButtonsusButton;privateJButtonrelaxButton;privateJButtonstartButton;privateJButtonnewButton;privateJLabelnameLabel;privateJLabelprorityLabel;privateJLabeltimeLabel;privateJLabeljobLabel;privateJLabelreadyLabel;privateJLabelwaitingLabel;privateJLabelrunningLabel;privateJLabelspaceLabel; privateJLabeldivLabel;privateJLabelallocLabel;privateJTablereadyTable;privateJTablerunningTable;privateJTabledivTable;privateJTableallocTable;privateJTextFieldnameText;privateJTextFieldtimeText;privateJTextFieldspaceText;privateJComboBoxprorityCom;privateJPanelnewPanel;privateJPanelwaitingPanel;privateJPanelreadyPanel;VectorjobVectorName;VectorjobDtoVector;VectorwaitingVectorName;VectorwaitingDtoVector;PcbDTO[]readyDtoArray;PcbDTO[]newDtoArray;divDTO[]divDtoArray;PcbDTO[]newSort;Object[][]readydata;Object[][]runningdata;Object[][]divdata;Object[][]allocdata;intfirst;intend;intpoint;PcbDTOa;publicMainFrame(){a=newPcbDTO();first=0;end=0;point=0;JFramejf=newJFrame("进程调度-ws");Containerc=jf.getContentPane();c.setLayout(null);//c.setBackground(Color.pink); newPanel=newJPanel();newPanel.setLayout(null);waitingPanel=newJPanel();waitingPanel.setLayout(null);//waitingPanel.setBackground(Color.pink);readyPanel=newJPanel();readyPanel.setLayout(null);susButton=newJButton("挂起");relaxButton=newJButton("释放");startButton=newJButton("开始");newButton=newJButton("新建进程");nameLabel=newJLabel("进程名");prorityLabel=newJLabel("优先级");timeLabel=newJLabel("运行时间");jobLabel=newJLabel("后备队列");readyLabel=newJLabel("就绪队列");waitingLabel=newJLabel("等待队列");runningLabel=newJLabel("运行进程");spaceLabel=newJLabel("需要空间");divLabel=newJLabel("未分分区表");allocLabel=newJLabel("内存分配表");nameText=newJTextField();timeText=newJTextField();spaceText=newJTextField();prorityCom=newJComboBox();prorityCom.setToolTipText("优先级");readyDtoArray=newPcbDTO[6];newSort=newPcbDTO[6];for(inti=0;i<6;i++){newSort[i]=newPcbDTO();}newDtoArray=newPcbDTO[100];jobDtoVector=newVector();jobVectorName=newVector();waitingDtoVector=newVector();waitingVectorName=newVector();divDtoArray=newdivDTO[20];for(inti=0;i<20;i++){divDtoArray[i]=newdivDTO(); divDtoArray[i].setDivFlag(0);}divDtoArray[0].setDivFlag(1);divDtoArray[0].setDivBase(20);divDtoArray[0].setLength(180);readydata=newObject[6][4];runningdata=newObject[2][3];divdata=newObject[20][3];allocdata=newObject[20][3];String[]col1={"进程","时间","优先级","状态"};String[]col2={"进程","时间","优先级"};String[]col3={"起址","长度","状态"};String[]col4={"起址","长度","占用进程"};readyTable=newJTable(readydata,col1);//readyTable.setEnabled(false);runningTable=newJTable(runningdata,col2);runningTable.setRowHeight(22);runningTable.setEnabled(false);allocTable=newJTable(allocdata,col4);allocTable.setEnabled(false);divTable=newJTable(divdata,col3);divTable.setEnabled(false);divTable.setValueAt(String.valueOf(20),0,0);divTable.setValueAt(String.valueOf(180),0,1);divTable.setValueAt(String.valueOf(1),0,2);JScrollPanerunningSP=newJScrollPane();JScrollPanereadySP2=newJScrollPane();JScrollPanedivSP=newJScrollPane();JScrollPaneallocSP=newJScrollPane();runningSP.getViewport().add(runningTable);readySP2.getViewport().add(readyTable);divSP.getViewport().add(divTable);allocSP.getViewport().add(allocTable);//int[]prorityArray=newint[10];for(inti=0;i<10;i++){prorityCom.addItem(i);//prorityArray[i]=i;}jobList=newJList();waitingList=newJList();JScrollPanereadySP=newJScrollPane(readyList);JScrollPanejobSP=newJScrollPane(jobList);JScrollPanewaitingSP=newJScrollPane(waitingList); newPanel.setSize(450,100);newPanel.setLocation(0,0);nameLabel.setSize(80,20);nameLabel.setLocation(10,5);nameText.setSize(100,25);nameText.setLocation(10,30);prorityLabel.setSize(80,20);prorityLabel.setLocation(120,5);prorityCom.setSize(100,25);prorityCom.setLocation(120,30);timeLabel.setSize(80,20);timeLabel.setLocation(230,5);timeText.setSize(100,25);timeText.setLocation(230,30);spaceLabel.setSize(80,20);spaceLabel.setLocation(340,5);spaceText.setSize(100,25);spaceText.setLocation(340,30);newButton.setSize(100,20);newButton.setLocation(320,70);waitingPanel.setSize(190,410);waitingPanel.setLocation(0,100);jobLabel.setSize(100,20);jobLabel.setLocation(10,2);jobSP.setSize(180,105);jobSP.setLocation(10,25);waitingLabel.setSize(100,20);waitingLabel.setLocation(10,129);waitingSP.setSize(180,105);waitingSP.setLocation(10,150);divLabel.setSize(100,20);divLabel.setLocation(10,253);divSP.setSize(180,113);divSP.setLocation(10,273);relaxButton.setSize(80,20);relaxButton.setLocation(110,388);readyPanel.setSize(260,410);readyPanel.setLocation(190,100);readyLabel.setSize(100,22); readyLabel.setLocation(10,2);allocLabel.setSize(100,20);allocLabel.setLocation(10,232);startButton.setSize(80,20);startButton.setLocation(177,388);susButton.setSize(80,20);susButton.setLocation(95,388);readySP2.setSize(250,117);readySP2.setLocation(10,25);runningLabel.setLocation(10,142);runningLabel.setSize(100,20);runningSP.setSize(250,65);runningSP.setLocation(10,167);allocSP.setSize(250,130);allocSP.setLocation(10,255);c.add(newPanel);newPanel.add(nameLabel);newPanel.add(nameText);newPanel.add(prorityLabel);newPanel.add(prorityCom);newPanel.add(timeText);newPanel.add(timeLabel);newPanel.add(newButton);newPanel.add(spaceLabel);newPanel.add(spaceText);c.add(waitingPanel);waitingPanel.add(jobLabel);waitingPanel.add(jobSP);waitingPanel.add(waitingLabel);waitingPanel.add(waitingSP);waitingPanel.add(divLabel);waitingPanel.add(divSP);waitingPanel.add(relaxButton);c.add(readyPanel);readyPanel.add(readyLabel);readyPanel.add(allocLabel);readyPanel.add(runningLabel);readyPanel.add(startButton);readyPanel.add(susButton);readyPanel.add(allocSP);readyPanel.add(runningSP); readyPanel.add(readySP2);jf.setSize(470,550);jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);jf.setLocationRelativeTo(null);jf.setVisible(true);startButton.addActionListener(newMyActionListener());newButton.addActionListener(newMyActionListener());susButton.addActionListener(newMyActionListener());relaxButton.addActionListener(newMyActionListener());}publicvoidsus(){try{Thread.sleep(1000);}catch(Exceptionex){}}classMyActionListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){intcount=0;PcbDTOtest=newPcbDTO();JButtonjb=(JButton)e.getSource();intmax=-1;if(jb==startButton){//while(true)//{intrunAllocFlag=-1;if((String)runningTable.getValueAt(0,0)==null||(String)runningTable.getValueAt(0,0)==""){try{Thread.sleep(0);}catch(Exceptionex){}//System.out.println("到3");for(intj=first;j!=end;){ if(!readyDtoArray[j].getProcessState().equals("waiting")){max=j;break;}j=(j+1)%6;}for(intj=first;j%6!=end;){if(!readyDtoArray[j].getProcessState().equals("waiting")){if(readyDtoArray[j].getPrority()readyDtoArray[max].getPrority()){max=j;}}j=(j+1)%6;}if(max>=0){a=readyDtoArray[max];readyDtoArray[max]=readyDtoArray[first];readyDtoArray[first]=a;readyTable.setValueAt(readyDtoArray[max].getProcessName(),max,0);readyTable.setValueAt(readyDtoArray[max].getRunTime(),max,1);readyTable.setValueAt(readyDtoArray[max].getPrority(),max,2);readyTable.setValueAt(readyDtoArray[max].getProcessState(),max,3);readyTable.setValueAt("",first,0);readyTable.setValueAt("",first,1);readyTable.setValueAt("",first,2);readyTable.setValueAt("",first,3);runningTable.setValueAt(a.getProcessName(),0,0);runningTable.setValueAt(a.getRunTime(),0,1);runningTable.setValueAt(a.getPrority(),0,2);readyDtoArray[first].setRunTime(readyDtoArray[first] .getRunTime()-1);if(0!=readyDtoArray[first].getPrority()){readyDtoArray[first].setPrority(readyDtoArray[first].getPrority()-1);}first=(first+1)%6;}else{System.out.println("cpu等待中……");}}else{*try{Thread.sleep(2000);}catch(InterruptedException*e1){System.out.println(e1);}//System.out.println("到1");runningTable.setValueAt("",0,0);runningTable.setValueAt("",0,1);runningTable.setValueAt("",0,2);//如果运行时间为0则撤销进程,否则将进程重新添加到就绪队列中if(a.getRunTime()<=0){//收回内存空间for(inti=0;i<point;i++){if(newSort[i].getBase()>=a.getBase()){newSort[i]=newSort[i+1];}}point--;//设置内存分配表的内容for(inti=0;i<point;i++){allocTable.setValueAt(String.valueOf(newSort[i].getBase()),i,);allocTable.setValueAt(String.valueOf(newSort[i].getLimit()),i,1);allocTable.setValueAt(newSort[i].getProcessName(),i,2);}allocTable.setValueAt("",point,0); allocTable.setValueAt("",point,1);allocTable.setValueAt("",point,2);//把收回的内存加入到记录未分分区的数组intmemoryEnd=0;intlocation=0;intup=-1;//intdown=-1;for(inti=0;i<20;i++){if(divDtoArray[i].getDivFalg()==1){memoryEnd=divDtoArray[i].getDivBase()+divDtoArray[i].getLength();if(memoryEnd==a.getBase()){up=i;}if(divDtoArray[i].getDivBase()==(a.getBase()+a.getLimit())){down=i;}}}if(up>=0&&down>=0){divDtoArray[up].setLength((divDtoArray[up].getLength()+a.getLimit()+divDtoArray[down].getLength()));divDtoArray[down].setDivFlag(0);for(inti=(down+1);i<20;i++){if(divDtoArray[i].getDivFalg()==1){divDtoArray[i-1].setDivBase(divDtoArray[i].getDivBase());divDtoArray[i-1].setDivFlag(1);-divDtoArray[i-1].setLength(divDtoArray[i].getLength());divDtoArray[i].setDivFlag(0);}else{divTable.setValueAt("",i-1,0);divTable.setValueAt("",i-1,1);divTable.setValueAt("",i-1,2);break;}} }elseif(up>=0&&down<0){divDtoArray[up].setLength((divDtoArray[up].getLength()+a.getLimit()));}elseif(up<0&&down>=0){divDtoArray[down].setLength((divDtoArray[down].getLength()+a.getLimit()));divDtoArray[down].setDivBase(a.getBase());}elseif(up<0&&down<0){for(inti=0;i<20;i++){if(divDtoArray[i].getDivBase()>a.getBase()||divDtoArray[i].getDivFalg()==0){location=i;break;}}for(inti=20;i>location;i--){if(divDtoArray[i-1].getDivFalg()==1){divDtoArray[i].setDivBase(divDtoArray[i-1].getDivBase());divDtoArray[i].setDivFlag(1);divDtoArray[i].setLength(divDtoArray[i-.getLength());}}divDtoArray[location].setDivBase(a.getBase());divDtoArray[location].setDivFlag(1);divDtoArray[location].setLength(a.getLimit());}//设置未分分区表的内容for(inti=0;i<20;i++){if(divDtoArray[i].getDivFalg()==1){divTable.setValueAt(String.valueOf(divDtoArray[i].getDivBase()),i,0);divTable.setValueAt(String.valueOf(divDtoArray[i].getLength()),i,1);divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()), i,2);}}if(!jobDtoVector.isEmpty()){intrunLength=0;PcbDTOjobToReady=(PcbDTO)jobDtoVector.elementAt(0);for(inti=0;i<20;i++){if(divDtoArray[i].getDivFalg()==1){jobToReady(divDtoArray[i].getLength().getLimit()){runAllocFlag=i;break;}}}if(runAllocFlag>=0){jobDtoVector.removeElementAt(0);jobVectorName.remove(jobVectorName.indexOf(jobToReady.getProcessName()));jobList.setListData(jobVectorName);jobToReady.setProcessState(PcbDTO.Ready);jobToReady.setBase(divDtoArray[runAllocFlag].getDivBase());runLength=divDtoArray[runAllocFlag].getLength()-jobToReady.getLimit();if(runLength==0){inti=runAllocFlag;divDtoArray[i].setDivFlag(0);for(;i<19;i++){if(divDtoArray[i+1].getDivFalg()==1){divDtoArray[i]=divDtoArray[i+divDtoArray[i+1].setDivFlag(0);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivBase()),i,0);divTable.setValueAt(String.valueOf(divDtoArray[i] .getLength()),i,1);divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i,2);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i,2);}elseif(runLength>0){intc2=divDtoArray[runAllocFlag].getDivBase()+jobToReady.getLimit();divDtoArray[runAllocFlag].setDivBase(c2);divDtoArray[runAllocFlag].setLength(runLength);divTable.setValueAt(String.valueOf(c2),runAllocFlag,0);divTable.setValueAt(String.valueOf(runLength),runAllocFlag,1);divTable.setValueAt(String.valueOf(divDtoArray[runAllocFlag].getDivFalg()),runAllocFlag,2);}readyDtoArray[end]=jobToReady;readyTable.setValueAt(jobToReady.getProcessName(),end,0);readyTable.setValueAt(jobToReady.getRunTime(),end,1);readyTable.setValueAt(jobToReady.getPrority(),end,2);readyTable.setValueAt(jobToReady.getProcessState(),end,3);end=(end+1)%6;intruni=0;//用于记录当前新生成的PcbDTO对象for(;runi<point;runi++){if(jobToReady.getBase()<newSort[runi] .getBase()){break;}}//如果不是插入到数组末尾,则把比它大的都向后挪一for(inti=point;i>runi;i--){newSort[i]=newSort[i-1];allocTable.setValueAt(String.valueOf(newSort[i].getBase()),i,0);allocTable.setValueAt(String.valueOf(newSort[i].getLimit()),allocTable.setValueAt(newSort[i].getProcessName(),i,}//插入新生成的对象newSort[runi]=jobToReady;allocTable.setValueAt(String.valueOf(jobToReady.getBase()),runi,0);allocTable.setValueAt(String.valueOf(jobToReady.getLimit()),runi,1);allocTable.setValueAt(jobToReady.getProcessName(),runi,2);point++;}}}else{readyDtoArray[end]=a;readyTable.setValueAt(a.getProcessName(),end,0);readyTable.setValueAt(a.getRunTime(),end,1);readyTable.setValueAt(a.getPrority(),end,2);readyTable.setValueAt(a.getProcessState(),end,3);end=(end+1)%6;} }//}}elseif(jb==newButton){intnewAllocFlag=-1;intnewLength=0;if(nameText.getText().trim().length()==0){JOptionPane.showMessageDialog(null,"进程名不能为空!");}elseif(timeText.getText().trim().length()==0){JOptionPane.showMessageDialog(null,"运行时间不能为空");}elseif(spaceText.getText().trim().length()==0){JOptionPane.showMessageDialog(null,"空间不能为空");}else{test.setRunTime(Integer.parseInt(timeText.getText()));test.setLimit(Integer.parseInt(spaceText.getText()));Strings=prorityCom.getSelectedItem().toString();test.setPrority(Integer.parseInt(s));test.setProcessName(nameText.getText().trim());newDtoArray[count]=test;jobDtoVector.add(newDtoArray[count]);jobVectorName.add(newDtoArray[count].getProcessName());jobList.setListData(jobVectorName);count++;nameText.setText("");timeText.setText("");spaceText.setText("");PcbDTOb=(PcbDTO)jobDtoVector.elementAt(0);for(inti=0;i<20;i++){if(divDtoArray[i].getDivFalg()==1){if(divDtoArray[i].getLength()>=b.getLimit()){newAllocFlag=i;break;}}}//在就绪队列未满且内存有足够空间时将后备队列jobDtoVetor中的对象添加到就绪队列中if((end+2)%6!=first&&newAllocFlag>=0){jobDtoVector.removeElementAt(0);b.setProcessState(PcbDTO.Ready);b.setBase(divDtoArray[newAllocFlag].getDivBase());newLength=divDtoArray[newAllocFlag].getLength() -b.getLimit();if(newLength==0){inti=newAllocFlag;divDtoArray[i].setDivFlag(0);for(;i<19;i++){if(divDtoArray[i+1].getDivFalg()==1){divDtoArray[i]=divDtoArray[i+1];divDtoArray[i+1].setDivFlag(0);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivBase()),i,0);divTable.setValueAt(String.valueOf(divDtoArray[i].getLength()),i,1);divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i,2);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i,2);}elseif(newLength>0){intc1=divDtoArray[newAllocFlag].getDivBase()+b.getLimit();divDtoArray[newAllocFlag].setDivBase(c1);divDtoArray[newAllocFlag].setLength(newLength);divTable.setValueAt(String.valueOf(c1),newAllocFlag,0);divTable.setValueAt(String.valueOf(newLength),newAllocFlag,1);divTable.setValueAt(String.valueOf(divDtoArray[newAllocFlag].getDivFalg()),newAllocFlag,2);}readyDtoArray[end]=b;jobVectorName.remove(jobVectorName.indexOf(b.getProcessName()));readyTable.setValueAt(b.getProcessName(),end,0); readyTable.setValueAt(b.getRunTime(),end,1);readyTable.setValueAt(b.getPrority(),end,2);readyTable.setValueAt("ready",end,3);end=(end+1)%6;intnewi=0;//用于记录当前新生成的PcbDTO对象应该插入for(;newi<point;newi++){if(b.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论