操作系统动态内存分配.doc
packagecom.gaojingdian.arithmetic;importjava.io.IOException;importjava.util.ArrayList;importjava.util.LinkedList;importjava.util.List;importcom.gaojingdian.DataFrame.PCB;importcom.gaojingdian.DataFrame.Subarea;importcom.gaojingdian.MainMemorySubarea.MainMemorySubarea;importcom.gaojingdian.MainMemorySubarea.RevokeMemory;/*最佳适应算法*/publicclassBestFitextendsFCFSprivateList<PCB>readyList=newArrayList<PCB>();/就绪队列privateList<Subarea>allocationList=newLinkedList<Subarea>();/已分分配分区队列privatestaticMainMemorySubareamms=null;privatePCBlastPCB=null;privateSubarearevokeSubarea=null;/当前要释放的分区privateList<Subarea>emptySubarea=null;/空闲队列privateList<PCB>list=null;/中间容器staticmms=newMainMemorySubarea();/用FCFS调度算法从后备队列中往内存中调入满足条件的作业,并用最佳适应算法为作业分配内存空间privatevoidenterReadyList()throwsIOExceptionmms.initFreeList();if(emptySubarea=null)emptySubarea=mms.getSubareaList();for(inti=0;i<list.size();i+)intj=0;j=this.getIndex(list.get(i);if(j!=-1)readyList.add(list.get(i);if(emptySubarea.get(j).getSize()-list.get(i).getProcessSize()<=Subarea.unalteredSize)Subarearsb=newSubarea();rsb.setSize(emptySubarea.get(j).getSize();rsb.setBeginAddress(emptySubarea.get(j).getBeginAddress();rsb.setPcbId(list.get(i).getId();allocationList.add(rsb);emptySubarea.remove(emptySubarea.get(j);elseSubareasa=emptySubarea.get(j);Subarearsb=newSubarea();rsb.setSize(list.get(i).getProcessSize();rsb.setBeginAddress(sa.getBeginAddress();rsb.setPcbId(list.get(i).getId();allocationList.add(rsb);sa.setBeginAddress(sa.getBeginAddress()+list.get(i).getProcessSize();sa.setSize(sa.getSize()-list.get(i).getProcessSize();list.get(i).setLogo2(1);break;if(j=-1)list.get(i).setLogo1(1);/执行进程,并回收内存空间publicvoidbestFit()throwsIOExceptionPCBpcb=null;inttemp=10000;intindex=0;while(true)list=super.selectProcess(readyList,lastPCB);this.enterReadyList();temp=10000;if(readyList.size()!=0)if(this.getCount()!=readyList.size()for(inti=0;i<readyList.size();i+)if(readyList.get(i).getArriveTime()<temp&&readyList.get(i).getLogo()=0&&readyList.get(i).getLogo2()=1&&readyList.get(i).getLogo1()=0)temp=readyList.get(i).getArriveTime();index=i;pcb=readyList.get(index);if(lastPCB=null)pcb.setBeginTime(pcb.getArriveTime();elsepcb.setBeginTime(lastPCB.getEndTime();pcb.setEndTime(pcb.getBeginTime()+pcb.getRunTime();pcb.setLogo(1);lastPCB=pcb;revokeSubarea=RevokeMemory.selectRevoke(allocationList,pcb);RevokeMemory.revokeMemory(emptySubarea,revokeSubarea);elsebreak;/判断就绪队列中还有没有为被执行的进程privateintgetCount()intcount=0;for(inti=0;i<readyList.size();i+)if(readyList.get(i).getLogo()=1)count+;returncount;/返回空闲分区中大小最合适的分区的下标privateintgetIndex(PCBpcb)inttemp=10000;intindex=-1;for(inti=0;i<emptySubarea.size();i+)if(pcb.getProcessSize()<=emptySubarea.get(i).getSize()&&(emptySubarea.get(i).getSize()-pcb.getProcessSize()<temp)temp=emptySubarea.get(i).getSize()-pcb.getProcessSize();index=i;returnindex;publicList<Subarea>getAllocationList()returnallocationList;publicvoidsetAllocationList(List<Subarea>allocationList)this.allocationList=allocationList;publicList<Subarea>getEmptySubarea()returnemptySubarea;publicvoidsetEmptySubarea(List<Subarea>emptySubarea)this.emptySubarea=emptySubarea;publicList<PCB>getReadyList()returnreadyList;publicvoidsetReadyList(List<PCB>readyList)this.readyList=readyList;packagecom.gaojingdian.arithmetic;importjava.io.IOException;importjava.util.ArrayList;importjava.util.LinkedList;importjava.util.List;importcom.gaojingdian.DataFrame.PCB;importcom.gaojingdian.DataFrame.Subarea;importcom.gaojingdian.MainMemorySubarea.MainMemorySubarea;importcom.gaojingdian.MainMemorySubarea.RevokeMemory;importcom.gaojingdian.Test.Test;importcom.gaojingdian.WorkList.WorkLists;/*首次适应算法*authorgaojingdian*/publicclassFirstFitextendsFCFSprivateList<PCB>readyList=newArrayList<PCB>();/就绪队列privateList<Subarea>allocationList=newLinkedList<Subarea>();/已分分配分区队列privatestaticMainMemorySubareamms=null;privatePCBlastPCB=null;privateSubarearevokeSubarea=null;/当前要释放的分区privateList<Subarea>emptySubarea=null;/空闲队列privateList<PCB>list=null;/中间容器staticmms=newMainMemorySubarea();/用FCFS调度算法从后备队列中往内存中调入满足条件的作业,并用首次适应算法为作业分配内存空间privatevoidenterReadyList()throwsIOExceptionmms.initFreeList();if(emptySubarea=null)emptySubarea=mms.getSubareaList();for(inti=0;i<list.size();i+)intj=0;for(;j<emptySubarea.size();j+)if(list.get(i).getProcessSize()<=emptySubarea.get(j).getSize()readyList.add(list.get(i);if(emptySubarea.get(j).getSize()-list.get(i).getProcessSize()<=Subarea.unalteredSize)Subarearsb=newSubarea();rsb.setSize(emptySubarea.get(j).getSize();rsb.setBeginAddress(emptySubarea.get(j).getBeginAddress();rsb.setPcbId(list.get(i).getId();allocationList.add(rsb);emptySubarea.remove(emptySubarea.get(j);elseSubareasa=emptySubarea.get(j);Subarearsb=newSubarea();rsb.setSize(list.get(i).getProcessSize();rsb.setBeginAddress(sa.getBeginAddress();rsb.setPcbId(list.get(i).getId();allocationList.add(rsb);sa.setBeginAddress(sa.getBeginAddress()+list.get(i).getProcessSize();sa.setSize(sa.getSize()-list.get(i).getProcessSize();list.get(i).setLogo2(1);break;if(j=emptySubarea.size()list.get(i).setLogo1(1);/执行进程,并回收内存空间publicvoidfirstFit()throwsIOExceptionPCBpcb=null;inttemp=10000;intindex=0;while(true)list=super.selectProcess(readyList,lastPCB);this.enterReadyList();temp=10000;if(readyList.size()!=0)if(this.getCount()!=readyList.size()for(inti=0;i<readyList.size();i+)if(readyList.get(i).getArriveTime()<temp&&readyList.get(i).getLogo()=0&&readyList.get(i).getLogo2()=1&&readyList.get(i).getLogo1()=0)temp=readyList.get(i).getArriveTime();index=i;pcb=readyList.get(index);if(lastPCB=null)pcb.setBeginTime(pcb.getArriveTime();elsepcb.setBeginTime(lastPCB.getEndTime();pcb.setEndTime(pcb.getBeginTime()+pcb.getRunTime();pcb.setLogo(1);lastPCB=pcb;revokeSubarea=RevokeMemory.selectRevoke(allocationList,pcb);RevokeMemory.revokeMemory(emptySubarea,revokeSubarea);elsebreak;privateintgetCount()intcount=0;for(inti=0;i<readyList.size();i+)if(readyList.get(i).getLogo()=1)count+;returncount;publicList<Subarea>getAllocationList()returnallocationList;publicvoidsetAllocationList(List<Subarea>allocationList)this.allocationList=allocationList;publicList<Subarea>getEmptySubarea()returnemptySubarea;publicvoidsetEmptySubarea(List<Subarea>emptySubarea)this.emptySubarea=emptySubarea;publicList<PCB>getReadyList()returnreadyList;publicvoidsetReadyList(List<PCB>readyList)this.readyList=readyList;