版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
操作系統(tǒng)課程設(shè)計(jì)班級(jí):計(jì)科4班組員:實(shí)驗(yàn)名稱CPU進(jìn)程調(diào)度和內(nèi)存分配實(shí)驗(yàn)內(nèi)容一、選擇一個(gè)調(diào)度算法,實(shí)現(xiàn)處理機(jī)調(diào)度二、處理機(jī)調(diào)度過程中,主存儲(chǔ)器空間的分配和回收實(shí)驗(yàn)?zāi)康募由顚?duì)CPU進(jìn)程調(diào)度和內(nèi)存分配的了解,進(jìn)一步了解操作系統(tǒng)中各功能的實(shí)現(xiàn),了解在不同的存儲(chǔ)管理方式下,應(yīng)怎樣實(shí)現(xiàn)主存空間的分配和回收。實(shí)驗(yàn)內(nèi)容一、(1)設(shè)計(jì)一個(gè)按照優(yōu)先權(quán)調(diào)度算法實(shí)現(xiàn)處理機(jī)調(diào)度的程序(2)設(shè)計(jì)按時(shí)間片輪轉(zhuǎn)實(shí)現(xiàn)處理機(jī)調(diào)度的程序二、在可變分區(qū)管理方式下,采用最先適應(yīng)算法實(shí)現(xiàn)主存空間的分配和回收;實(shí)驗(yàn)過程及分析1.原理知識(shí):(1)作業(yè)進(jìn)入內(nèi)存中,由CPU分配產(chǎn)生PCB屬性,并通過PCB記錄進(jìn)程狀態(tài),實(shí)驗(yàn)即以PCB代表進(jìn)程模擬調(diào)度過程;(2)在多道系統(tǒng)中,當(dāng)就緒進(jìn)程數(shù)大于處理機(jī)數(shù)時(shí),須按照某種策略決定哪些進(jìn)程優(yōu)先占用處理機(jī),本設(shè)計(jì)采用優(yōu)先級(jí);(3)進(jìn)程調(diào)度時(shí),規(guī)定若就緒隊(duì)列進(jìn)程數(shù)少于6個(gè),則自動(dòng)從后備隊(duì)列調(diào)入一個(gè)作業(yè);(4)系統(tǒng)會(huì)將占有較多資源、預(yù)期結(jié)果不符合要求的進(jìn)程自動(dòng)掛起,并回收所占資源,而本實(shí)驗(yàn)設(shè)置為手動(dòng)掛起;(5)在適宜條件下,系統(tǒng)會(huì)將掛起的進(jìn)程自動(dòng)解掛,而且只解掛到就緒隊(duì)列;本設(shè)計(jì)為簡化操作,設(shè)置為手動(dòng)解掛,若解掛條件合適(即CPU各種資源可用),則解掛到就緒隊(duì)列,并分配內(nèi)存;若解掛條件不適宜,則解掛至后備隊(duì)列,但不分配內(nèi)存;2、實(shí)驗(yàn)程序設(shè)計(jì):(1)本實(shí)驗(yàn)采用java語言編程,并實(shí)現(xiàn)界面顯示。(2)為體現(xiàn)java語言面對(duì)對(duì)象程序設(shè)計(jì)的特點(diǎn),實(shí)驗(yàn)設(shè)計(jì)為ProcessPCB、MemoryItem類封裝PCB和所分配的內(nèi)存各自的屬性與方法;用ProcessRecords、MemoryRecords類封裝數(shù)組方法;用SingleCPUScheduling實(shí)現(xiàn)GUI界面顯示;(3)ProcessPCB類中,定義PCB的進(jìn)程名、要求運(yùn)行時(shí)間、優(yōu)先級(jí)、狀態(tài)、主存起始位置、所需內(nèi)存大小這6個(gè)屬性,并定義各屬性的get和set方法,定義equals方法用于對(duì)比類的屬性,定義toString方法得到類屬性的字符串,定義run方法封裝優(yōu)先權(quán)-1/要求運(yùn)行時(shí)間-1的過程;MemoryItem類中,定義可分分區(qū)表每一可分記錄的主存起始位置、內(nèi)存大小及其get和set方法,定義toString方法得到可在界面顯示的字符串;(4)ProcessRecords封裝PCB數(shù)組的添加元素addItem和刪除元素removeItem方法,并構(gòu)造函數(shù)getItem通過參數(shù)ProcessPCB和String查找數(shù)組元素,定義getNumberOfItems取數(shù)組大小,定義getItemsPriorities方法取所有數(shù)組元素的toString方法用于界面顯示,定義iterator方法取得數(shù)組的迭代器;(5)MemoryRecords用同樣的設(shè)計(jì)思想封裝以MemoryItem為數(shù)組元素的各屬性和方法;(6)SingleCPUScheduling類繼承JFrame類,實(shí)現(xiàn)界面化顯示;與上面相對(duì)應(yīng),實(shí)例化ProcessRecords(3次)和MemoryRecords(1次)作為私有變量,分別作為后備隊(duì)列、就緒隊(duì)列、掛起隊(duì)列和內(nèi)存可分分區(qū)表;在界面設(shè)計(jì)中,設(shè)計(jì)后備隊(duì)列、掛起隊(duì)列(附帶解掛umount按鈕)、就緒隊(duì)列(附帶掛起suspend按鈕)可分分區(qū)表列表顯示框,設(shè)置PCB添加框,附帶添加至后備隊(duì)列(addToBackup)、添加至就緒隊(duì)列(addToReady)按鈕,以及CPU當(dāng)前執(zhí)行狀態(tài)顯示框、系統(tǒng)日志顯示框,和開始調(diào)度(systemStart)按鈕,優(yōu)先級(jí)和時(shí)間片單選按鈕,以及時(shí)間片顯示標(biāo)簽和文本編輯框;(7)界面設(shè)計(jì)詳解;后備隊(duì)列顯示框用于顯示已添加至后備隊(duì)列的ProcessRecords屬性信息,其中主存起始位置默認(rèn)為-1,表示未分配;掛起隊(duì)列顯示框用于顯示從就緒隊(duì)列掛起的PCB,其中屬性“主存起始位置”(MemoryBase)將由非負(fù)數(shù)變?yōu)?1,表示掛起后收回內(nèi)存;就緒隊(duì)列顯示框中顯示就緒隊(duì)列屬性,其中“主存起始位置”均為非負(fù),表示一分配內(nèi)存;PCB信息添加框分列PCB6個(gè)屬性顯示標(biāo)簽和可編輯文本框,和添加按鈕,用于添加PCB;系統(tǒng)日志顯示框附屬時(shí)間片顯示標(biāo)簽和可編輯文本編輯框,可由用戶決定時(shí)間片大小;(8)附屬功能添加完善;最重要的是為程序添加線程,是程序能以停頓一段時(shí)間的頻率自動(dòng)運(yùn)行;后備隊(duì)列、掛起隊(duì)列添加total顯示標(biāo)簽和不可編輯文本顯示框,用于顯示各自數(shù)組中元素?cái)?shù)目,掛起隊(duì)列附屬刪除(remove)按鈕,可刪除掛起隊(duì)列中的元素;后備、掛起、就緒隊(duì)列均添加監(jiān)聽器,用于響應(yīng)用戶單擊操作,可以在PCB信息添加框顯示用戶單擊的那一條PCB的信息;PCB信息添加框附屬reset按鈕,用于一鍵清空信息框中信息,方便輸入;系統(tǒng)日志面板附屬系統(tǒng)暫停(systemPause)和系統(tǒng)重置(systemReset)按鈕,分別用于暫停運(yùn)行(方便用戶觀察當(dāng)前運(yùn)行結(jié)果)和重置系統(tǒng)(方便用戶重復(fù)使用程序,免去關(guān)閉后重啟本程序的麻煩);最終界面如圖:設(shè)計(jì)結(jié)果報(bào)告和分析本程序還存在一些技術(shù)上的問題,使得程序不能盡善盡美;如,PCB信息添加框沒有“隨機(jī)置入就緒隊(duì)列”功能,添加PCB信息仍顯得繁瑣;就緒隊(duì)列的掛起功能在程序自動(dòng)運(yùn)行時(shí),存在反應(yīng)異常(反應(yīng)延遲或直接無反映);可分分區(qū)表只顯示了當(dāng)前可分的內(nèi)存,沒有顯示已分的PCB及其對(duì)應(yīng)內(nèi)存使用情況,且沒有利用圖形和豐富的顏色來更好的展示;時(shí)間片設(shè)計(jì)還需要改進(jìn),使用效率不高;系統(tǒng)重置功能存在響應(yīng)延遲的問題;另外,界面不夠美觀;還需要不斷改進(jìn)。設(shè)計(jì)感想通過這次實(shí)驗(yàn),我們對(duì)操作系統(tǒng)的進(jìn)程調(diào)度和內(nèi)存分配管理有了更加深入的了解,對(duì)操作系統(tǒng)內(nèi)部的工作原理有了進(jìn)一步的認(rèn)識(shí)。通過編程,也鞏固了我們的程序設(shè)計(jì)和代碼編寫的能力,實(shí)驗(yàn)過程中遇到的各種問題以及解決問題的過程與方法,都是我們獲益匪淺。同時(shí),程序的不完善,也將促使我們?cè)谡n程之后,繼續(xù)學(xué)習(xí)、理解課程內(nèi)容,并盡一切努力不斷完善程序,做到盡善盡美。程序代碼:packagesrc;publicclassMemoryItem{ privateintmemoryBase=0; privateintmemoryLimit=0; privateintavailableStatus=0; publicMemoryItem(intinitMemoryBase,intinitMemoryLimit){ this.memoryBase=initMemoryBase; this.memoryLimit=initMemoryLimit; } publicintgetMemoryBase(){ returnthis.memoryBase; }publicintgetMemoryLimit(){ returnthis.memoryLimit; }publicintgetStatus(){ returnthis.availableStatus; }publicStringtoString(){ returnthis.getMemoryBase()+"_"+this.getMemoryLimit()+"\n";}}packagesrc;publicclassProcessPCB{// backupBAK后備ready就緒suspend掛起memory內(nèi)存 privateStringPID; privateintRequiredTime; privateintPriority; privateStringStatus; privateintMwmoryBase=0000; privateintMemoryLimit;// privateStringPCBPointer; publicProcessPCB(StringinitpID,intinitRTime,intinitpriority, Stringstatus,intinitBase,intinitLimit){ this.PID=initpID; this.RequiredTime=initRTime; this.Priority=initpriority; this.Status=status; this.MwmoryBase=initBase; this.MemoryLimit=initLimit; } publicStringgetPID(){ if(this.PID==null) return""; else returnthis.PID; } publicintgetRequiredTime(){ returnthis.RequiredTime; } publicintgetPriority(){ returnthis.Priority; } publicStringgetStatus(){ if(this.Status==null) return""; else returnthis.Status; } publicintgetMemoryBase(){ returnthis.MwmoryBase; } publicintgetMemoryLimit(){ returnthis.MemoryLimit; } publicbooleanequals(ProcessPCBpcb){ if(pcb.getPID()==this.getPID()){ returntrue; } elsereturnfalse; } publicStringtoString(){ returnthis.getPID()+"_"+this.getRequiredTime()+"_"+this.getPriority()+"_" +this.getStatus()+"_"+this.getMemoryBase()+"_"+this.getMemoryLimit()+"\n"; } publicvoidrun(){ this.RequiredTime=this.RequiredTime-1; this.Priority=this.Priority-1; } }packagesrc;importjava.util.ArrayList;importjava.util.Iterator;publicclassPCBRecordsimplementsIterable<ProcessPCB>{ privateArrayList<ProcessPCB>PCBItems; publicArrayList<ProcessPCB>getPCBItems(){ returnthis.PCBItems; }publicPCBRecords(){ this.PCBItems=newArrayList<ProcessPCB>(); }publicvoidaddItem(ProcessPCBPcbItem){ this.PCBItems.add(PcbItem); }publicvoidremoveItem(ProcessPCBPCbItem){ this.PCBItems.remove(PCbItem); }publicProcessPCBgetItem(ProcessPCBprocessPCB){ for(ProcessPCBpCbItem:this.PCBItems){ if(pCbItem.equals(processPCB)){ returnpCbItem; } } returnnull; } publicProcessPCBgetItem(Stringpid){ for(ProcessPCBpcBItem:this.PCBItems){ if(pcBItem.getPID().equals(pid)){ returnpcBItem; } } returnnull; }publicintgetNumberOfItems(){ returnthis.PCBItems.size(); }publicString[]getItemsProperties(){ StringitemsProperties[]=newString[getNumberOfItems()]; inti=0;for(Iteratoriterator1=PCBItems.iterator();iterator1.hasNext();){ ProcessPCBstu_Item=(ProcessPCB)iterator1.next(); itemsProperties[i++]=stu_Item.toString();}returnitemsProperties; } publicIterator<ProcessPCB>iterator(){ returnthis.PCBItems.iterator(); }}packagesrc;importjava.util.ArrayList;importjava.util.Iterator;publicclassMemoryRecordsimplementsIterable<MemoryItem>{ privateArrayList<MemoryItem>memoryItems; publicIterator<MemoryItem>iterator(){ //TODOAuto-generatedmethodstub returnthis.memoryItems.iterator(); }publicArrayList<MemoryItem>getMemoryItems(){ returnthis.memoryItems; }publicMemoryRecords(){ this.memoryItems=newArrayList<MemoryItem>();}publicvoidaddItem(MemoryItemnewMemoryItem){ this.memoryItems.add(newMemoryItem);}publicvoidremoveItem(MemoryItemmomoryItem){ this.memoryItems.remove(momoryItem);}publicMemoryItemgetMomoryItem(MemoryItemitem){ for(MemoryItemmItem:this.memoryItems){ if(mItem.equals(item)){ returnmItem; } } returnnull;}publicMemoryItemgetMemoryItem(intbase){ for(MemoryItemmItem:this.memoryItems){ if(mItem.getMemoryBase()==base){ returnmItem; } } returnnull;}publicintgetNumberOfItems(){ returnthis.memoryItems.size(); }publicString[]getItemsProperties(){ StringitemsProperties[]=newString[getNumberOfItems()]; inti=0; for(Iteratoriterator1=this.memoryItems.iterator();iterator1.hasNext();){ MemoryItemmmItem=(MemoryItem)iterator1.next(); itemsProperties[i++]=mmItem.toString(); } if(itemsProperties==null){ itemsProperties[0]=""; } returnitemsProperties;}}packagesrc;importjava.util.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;importsrc.SingleCPUSchedulingGUI03.AddToBAKListener;importsrc.SingleCPUSchedulingGUI03.AddToReadyListener;importsrc.SingleCPUSchedulingGUI03.DisplayBackupProcessListener;importsrc.SingleCPUSchedulingGUI03.DisplaySuspendedProcessListener;importsrc.SingleCPUSchedulingGUI03.DisplayreadyProcessListener;importsrc.SingleCPUSchedulingGUI03.RemoveListener;importsrc.SingleCPUSchedulingGUI03.ResetListener;importsrc.SingleCPUSchedulingGUI03.ResetSystemListener;importsrc.SingleCPUSchedulingGUI03.StartSystemListener;importsrc.SingleCPUSchedulingGUI03.SuspendListener;importsrc.SingleCPUSchedulingGUI03.UmountListener;//importsrc.myframe.MyWindowMonitor;importjava.io.*;importjava.text.*;publicclassSingleCPUSchedulingGUI03extendsJFrame{ /*Standarerrorstream*/ staticprivatePrintWriterstdErr=newPrintWriter(System.err,true); staticprivateintWIDTH=800,HEIGHT=750;//thesizeoftheFrame主面板 /*各列表對(duì)應(yīng)的面板規(guī)格*//* 對(duì)應(yīng)各名詞釋義backupBAK后備ready就緒suspend掛起memory內(nèi)存*/ staticprivateintBackupBAK_CELL_SIZE=250,BackupBAK_LIST_ROWS=10; //后備隊(duì)列 staticprivateintSuspend_CELL_SIZE=250,Suspend_LIST_ROWS=10; //掛起隊(duì)列 staticprivateintReady_CELL_SIZE=200,Ready_LIST_ROWS=6; //就緒隊(duì)列 staticprivateintMemory_CELL_SIZE=200,Memory_LIST_ROWS=10;//內(nèi)存隊(duì)列 staticprivateintCPU_ROWS=12,CPU_COLS=22;//CPU面板 staticprivateintSTATUS_ROWS=8,STATUS_COLS=30;//系統(tǒng)狀態(tài)面板privateinttimeslice=1;//設(shè)置時(shí)間片大小privateintsystemStatus=0;//設(shè)置系統(tǒng)狀態(tài)0——系統(tǒng)預(yù)備狀態(tài),等待開始,1——系統(tǒng)運(yùn)行狀態(tài),2——系統(tǒng)暫停狀態(tài) staticprivateintTOTAL__TEXTFIELD_SIZE=10; //Sizetotaltextfield記錄各隊(duì)列元素個(gè)數(shù) privateJListbackupList,suspendList,readyList,memoryList;//各隊(duì)列相對(duì)應(yīng)的數(shù)組列表 privateJButtonaddToBAKButton,addToReadyButton,resetButton;// 進(jìn)程添加框中的"添加至后備隊(duì)列","添加至就緒隊(duì)列","重置"Button privateJButtonsuspendButton,umountButton,removeButton;//就緒隊(duì)列框中的"掛起",掛起隊(duì)列框中的"解掛","刪除"Button privateJButtonstartButton,pauseButton,resetSyatemButton;//Status面板中的"啟動(dòng)系統(tǒng)","重置系統(tǒng)"Button //優(yōu)先級(jí)和時(shí)間片單選鈕及時(shí)間片顯示框 privateJRadioButtonpriorityJRB,timesliceJRB; privateJLabeltimesliceSizeLabel; privateJTextFieldtimesliceJtf; privateJPanelbackupBAKPanel,PCBItemPanel,suspendedPanel,memoryPanel;//后備面板、進(jìn)程添加面板、掛起面板、內(nèi)存面板 privateJLabelbackupTotalLabel,suspendTotalLabel;//后備隊(duì)列、掛起隊(duì)列元素總數(shù)標(biāo)簽 privateJLabelPIDLabel,requiredTimeLabel,priorityLabel,statuesLabel,baseLabel,limitLabel;// 進(jìn)程信息標(biāo)簽進(jìn)程編號(hào)PID,所需運(yùn)行時(shí)間requiredTime,優(yōu)先級(jí)priority,當(dāng)前狀態(tài)statues,內(nèi)存中的基址base,所需內(nèi)存大小limit privateJTextFieldbackupTotalTextField,suspendTotalTextField;// 后備隊(duì)列、掛起隊(duì)列元素總數(shù)文本框(不可編輯) privateJTextFieldPIDTextField,requiredTimeTextField,priorityTextField,statusTextField,baseTextField,limitTextField;//CPU狀態(tài)顯示文本域(不可編輯),status信息文本域(用于現(xiàn)實(shí)程序每一步的操作和影響,不可編輯) privateJTextAreaCPUTextArea,statuesTextArea; //后備隊(duì)列PCB數(shù)組,就緒、掛起,——內(nèi)存(可分分區(qū)表) PCBRecordsbackupPCB,readyPCB,suspendedPCB; privateMemoryRecordsmemoryItems; privatebooleanflag=false;//main函數(shù) publicstaticvoidmain(String[]args)throwsIOException{ newSingleCPUSchedulingGUI03().initFrame(); } //初始化Frame publicvoidinitFrame(){ backupList=newJList(); backupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); backupList.setVisibleRowCount(BackupBAK_LIST_ROWS); backupList.setFixedCellWidth(BackupBAK_CELL_SIZE); suspendList=newJList(); suspendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); suspendList.setVisibleRowCount(Suspend_LIST_ROWS); suspendList.setFixedCellWidth(Suspend_CELL_SIZE); readyList=newJList(); readyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); readyList.setVisibleRowCount(Ready_LIST_ROWS); readyList.setFixedCellWidth(Ready_CELL_SIZE); memoryList=newJList(); memoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); memoryList.setVisibleRowCount(Memory_LIST_ROWS); memoryList.setFixedCellWidth(Memory_CELL_SIZE); suspendButton=newJButton("掛起"); addToBAKButton=newJButton("添加至后備隊(duì)列"); addToReadyButton=newJButton("添加至就緒隊(duì)列"); resetButton=newJButton("重置"); umountButton=newJButton("解掛"); removeButton=newJButton("刪除"); startButton=newJButton("開始調(diào)度"); pauseButton=newJButton("暫停"); resetSyatemButton=newJButton("重啟系統(tǒng)"); priorityJRB=newJRadioButton("優(yōu)先權(quán)",true); timesliceJRB=newJRadioButton("時(shí)間片"); backupTotalLabel=newJLabel("總計(jì):"); backupTotalTextField=newJTextField("0",TOTAL__TEXTFIELD_SIZE); backupTotalTextField.setEditable(false); suspendTotalLabel=newJLabel("總計(jì):"); suspendTotalTextField=newJTextField("0",TOTAL__TEXTFIELD_SIZE); suspendTotalTextField.setEditable(false); timesliceSizeLabel=newJLabel("時(shí)間片"); timesliceJtf=newJTextField("3",5); timesliceJtf.setEditable(true); CPUTextArea=newJTextArea(CPU_ROWS,CPU_COLS); CPUTextArea.setEditable(false); statuesTextArea=newJTextArea(STATUS_ROWS,STATUS_COLS); statuesTextArea.setEditable(false); /*northpanel*/ JPanelnorthPanel=newJPanel(newGridLayout(1,3)); //ProcessPCBiteminformationPanel PCBItemPanel=newJPanel(newBorderLayout()); PCBItemPanel.setBorder( BorderFactory.createTitledBorder("PCB信息")); JPanelPCBItemButtonJPanel=newJPanel(newGridLayout(3,1)); PCBItemButtonJPanel.add(addToBAKButton); PCBItemButtonJPanel.add(addToReadyButton); PCBItemButtonJPanel.add(resetButton); PCBItemPanel.add(this.initPCBItemPanel(),BorderLayout.CENTER); PCBItemPanel.add(PCBItemButtonJPanel,BorderLayout.SOUTH); //backupBAKListPanel backupBAKPanel=newJPanel(newBorderLayout()); backupBAKPanel.setBorder(BorderFactory.createTitledBorder("后備列表")); JPanelbackupTotalPAnel=newJPanel();backupTotalPAnel.add(backupTotalLabel);backupTotalPAnel.add(backupTotalTextField);backupBAKPanel.add( newJScrollPane(backupList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),BorderLayout.CENTER);backupBAKPanel.add(backupTotalPAnel,BorderLayout.SOUTH);//SuspendListPanelsuspendedPanel=newJPanel(newBorderLayout());suspendedPanel.setBorder(BorderFactory.createTitledBorder("掛起列表"));JPanelsuspendedTotalPAnel=newJPanel();suspendedTotalPAnel.add(suspendTotalLabel);suspendedTotalPAnel.add(suspendTotalTextField);JPanelsuspendComponentPanel=newJPanel(newGridLayout(1,2));suspendComponentPanel.add(umountButton);suspendComponentPanel.add(removeButton);suspendedPanel.add(suspendedTotalPAnel,BorderLayout.NORTH);suspendedPanel.add( newJScrollPane(suspendList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),BorderLayout.CENTER);suspendedPanel.add(suspendComponentPanel,BorderLayout.SOUTH);northPanel.add(backupBAKPanel);northPanel.add(PCBItemPanel);northPanel.add(suspendedPanel);/*centerPanel*/JPanelcentrelPanel=newJPanel(newGridLayout(1,3));//readyListpanelJPanelreadyListPanel=newJPanel(newBorderLayout());readyListPanel.setBorder(BorderFactory.createTitledBorder("就緒列表"));readyListPanel.add( newJScrollPane(readyList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));readyListPanel.add(suspendButton,BorderLayout.SOUTH);//CPUpanelJPanelCPUPanel=newJPanel();CPUPanel.setBorder(BorderFactory.createTitledBorder("CPU"));CPUPanel.add(newJScrollPane(CPUTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));memoryPanel=newJPanel(newBorderLayout());memoryPanel.setBorder(BorderFactory.createTitledBorder("內(nèi)存列表"));memoryPanel.add( newJScrollPane(memoryList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));centrelPanel.add(readyListPanel);centrelPanel.add(CPUPanel);centrelPanel.add(memoryPanel);/*statuespanel*/JPanelsouthPanel=newJPanel(newBorderLayout());JPanelstatuesPanel=newJPanel();statuesPanel.setBorder(BorderFactory.createTitledBorder("狀態(tài)"));statuesPanel.add(newJScrollPane(statuesTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));JPanelsystemContralButtonPanel=newJPanel(newGridLayout(6,1));systemContralButtonPanel.setBorder(BorderFactory.createTitledBorder("系統(tǒng)控制"));ButtonGroupgroup=newButtonGroup();group.add(priorityJRB);group.add(timesliceJRB);JPanelporityPanel=newJPanel(newGridLayout(1,2));porityPanel.add(timesliceSizeLabel);porityPanel.add(timesliceJtf);systemContralButtonPanel.add(priorityJRB);systemContralButtonPanel.add(timesliceJRB);systemContralButtonPanel.add(porityPanel);systemContralButtonPanel.add(startButton);systemContralButtonPanel.add(pauseButton);systemContralButtonPanel.add(resetSyatemButton);southPanel.add(statuesPanel,BorderLayout.CENTER);southPanel.add(systemContralButtonPanel,BorderLayout.EAST);// arrangepanelsinwindow setLayout(newBorderLayout()); add(northPanel,BorderLayout.NORTH); add(centrelPanel,BorderLayout.CENTER); add(southPanel,BorderLayout.SOUTH);//statuesPanel // startlisteningforlistandbuttonsevents backupList.addListSelectionListener(newDisplayBackupProcessListener()); suspendList.addListSelectionListener(newDisplaySuspendedProcessListener()); readyList.addListSelectionListener(newDisplayreadyProcessListener()); addToBAKButton.addActionListener(newAddToBAKListener()); addToReadyButton.addActionListener(newAddToReadyListener()); resetButton.addActionListener(newResetListener()); suspendButton.addActionListener(newSuspendListener()); umountButton.addActionListener(newUmountListener()); removeButton.addActionListener(newRemoveListener()); startButton.addActionListener(newStartSystemListener()); pauseButton.addActionListener(newSystemPauseListener()); resetSyatemButton.addActionListener(newResetSystemListener()); priorityJRB.addActionListener(newpriotiryListener()); timesliceJRB.addActionListener(newtimeslicListener()); backupPCB=newPCBRecords(); readyPCB=newPCBRecords(); suspendedPCB=newPCBRecords(); memoryItems=newMemoryRecords(); MemoryIteminitMemoryItem=newMemoryItem(0,3000); memoryItems.addItem(initMemoryItem); backupList.setListData(backupPCB.getItemsProperties()); readyList.setListData(readyPCB.getItemsProperties()); suspendList.setListData(suspendedPCB.getItemsProperties()); memoryList.setListData(memoryItems.getItemsProperties()); this.setTitle("CPU調(diào)度"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(WIDTH,HEIGHT); this.setResizable(true); this.setVisible(true); this.setLocation(200,10); this.addWindowListener(newMyWindowMonitor()); } publicJPanelinitPCBItemPanel(){ JPaneliniPCBItemJPanel=newJPanel(newBorderLayout()); JPaneliniNamePanel=newJPanel(newGridLayout(6,1)); JPaneliniValuePanel=newJPanel(newGridLayout(6,1)); PIDLabel=newJLabel("PID:"); requiredTimeLabel=newJLabel("所需時(shí)間:"); priorityLabel=newJLabel("優(yōu)先權(quán):"); statuesLabel=newJLabel("狀態(tài):"); baseLabel=newJLabel("基態(tài):"); limitLabel=newJLabel("限制:"); iniNamePanel.add(PIDLabel); iniNamePanel.add(requiredTimeLabel); iniNamePanel.add(priorityLabel); iniNamePanel.add(statuesLabel); iniNamePanel.add(baseLabel); iniNamePanel.add(limitLabel); PIDTextField=newJTextField("",10); PIDTextField.setEditable(true); requiredTimeTextField=newJTextField("",10); requiredTimeTextField.setEditable(true); priorityTextField=newJTextField("",10); priorityTextField.setEditable(true); statusTextField=newJTextField("",10); statusTextField.setEditable(false); baseTextField=newJTextField("",10); baseTextField.setEditable(false); limitTextField=newJTextField("",10); limitTextField.setEditable(true); iniValuePanel.add(PIDTextField); iniValuePanel.add(requiredTimeTextField); iniValuePanel.add(priorityTextField); iniValuePanel.add(statusTextField); iniValuePanel.add(baseTextField); iniValuePanel.add(limitTextField); iniPCBItemJPanel.add(iniNamePanel,BorderLayout.WEST); iniPCBItemJPanel.add(iniValuePanel,BorderLayout.CENTER); returniniPCBItemJPanel; } classMyWindowMonitorextendsWindowAdapter{ publicvoidwindowClosing(WindowEvente){ System.exit(0); setVisible(false); } } publicvoiddisplayInformation(ProcessPCBpcb){ PIDTextField.setText(pcb.getPID()); requiredTimeTextField.setText(Integer.toString(pcb.getRequiredTime())); priorityTextField.setText(Integer.toString(pcb.getPriority())); statusTextField.setText(pcb.getStatus()); baseTextField.setText(Integer.toString(pcb.getMemoryBase())); limitTextField.setText(Integer.toString(pcb.getMemoryLimit())); } //DisplaystheinformationoftheselectedPCBItem classDisplayBackupProcessListenerimplementsListSelectionListener{ publicvoidvalueChanged(ListSelectionEventevent){ if(!backupList.getValueIsAdjusting()){ Stringid=null,selectedBAKItem=null; selectedBAKItem=(String)backupList.getSelectedValue(); if(selectedBAKItem!=null){ StringTokenizerstringtokenizer=newStringTokenizer(selectedBAKItem,"_"); id=stringtokenizer.nextToken(); ProcessPCBbackupItem=backupPCB.getItem(id); if(backupItem!=null) { displayInformation(backupItem); } statuesTextArea.append(backupItem.toString()+"ProcessItem"+id +"inbackupListhasbeendisplayed!\n"); } } } } classDisplaySuspendedProcessListenerimplementsListSelectionListener{ publicvoidvalueChanged(ListSelectionEventevent){ if(!suspendList.getValueIsAdjusting()){// stdErr.flush(); Stringid=""; Stringstr=(String)suspendList.getSelectedValue(); if(str!=null){ StringTokenizerstrtokenizer=newStringTokenizer(str,"_"); id=strtokenizer.nextToken(); ProcessPCBselectedSuspendedItem=suspendedPCB.getItem(id); if(selectedSuspendedItem!=null) { displayInformation(selectedSuspendedItem); } statuesTextArea.append(selectedSuspendedItem.toString()+"ProcessItem"+id +"insuspendListhasbeendisplayed!\n"); } } } } classDisplayreadyProcessListenerimplementsListSelectionListener{ publicvoidvalueChanged(ListSelectionEventevent){ if(!readyList.getValueIsAdjusting()){ Stringid=null,s=null; s=(String)readyList.getSelectedValue(); if(s!=null){ StringTokenizersttokenizer=newStringTokenizer(s,"_"); id=sttokenizer.nextToken(); ProcessPCBreadyItem=readyPCB.getItem(id); if(readyItem!=null) { displayInformation(readyItem); } statuesTextArea.append(readyItem.toString()+"ProcessItem"+id +"inreadyListhasbeendisplayed!\n"); } } } } //內(nèi)存可分分區(qū)表排序,按可用內(nèi)存大小降序排列 publicvoidsortMemoryList(){ MemoryRecordscurrentListItems=newMemoryRecords(); intnum=memoryItems.getNumberOfItems(); if(num>0){ for(inti=num;i>=1;i--){ IteratormemoryIterator=memoryItems.iterator(); MemoryItemcurrentItem=(MemoryItem)memoryIterator.next(); for(;memoryIterator.hasNext();){ MemoryItemnowItem=(MemoryItem)memoryIterator.next(); if(currentItem.getMemoryLimit()<nowItem.getMemoryLimit()){ currentItem=null; currentItem=nowItem; } } currentListItems.addItem(currentItem); memoryItems.removeItem(currentItem); } memoryItems=null; memoryItems=currentListItems; } memoryList.setListData(memoryItems.getItemsProperties()); } //判斷能否為進(jìn)程分配內(nèi)存 publicbooleanboolMemoryAllocation(intneededLimit){ if(memoryItems.getNumberOfItems()>0){ IteratormemoryListIteartor=memoryItems.iterator(); MemoryItemlagerestItem=(MemoryItem)memoryListIteartor.next(); if(lagerestItem.getMemoryLimit()>=neededLimit) returntrue; else{ returnfalse; } }else{ returnfalse; } } //內(nèi)存分配,按最先適應(yīng)算法原則 publicintMemoryAllocation(intneededLimit){ intcurrentBase=-1,currentLimit=-1; IteratormemoryListIteartor=memoryItems.iterator(); MemoryItemlagerestItem=(MemoryItem)memoryListIteartor.next(); currentBase=lagerestItem.getMemoryBase()+neededLimit; currentLimit=lagerestItem.getMemoryLimit()-neededLimit; memoryItems.removeItem(lagerestItem); if(currentLimit>0){ memoryItems.addItem(newMemoryItem(currentBase,currentLimit)); } sortMemoryList(); returnlagerestItem.getMemoryBase(); } //內(nèi)存回收 publicvoidmemoryRecall(intnowBase,intnowLimit){ MemoryItemrecallItem=null; intrecalledBase=nowBase; intrecalledLimit=nowLimit; if(memoryItems.getNumberOfItems()>0){ IteratormemoryListIteartor=memoryItems.iterator(); for(;memoryListIteartor.hasNext();){ MemoryItemexistedItem=(MemoryItem)memoryListIteartor.next(); if(existedItem.getMemoryBase()==(nowBase+nowLimit)){ recalledLimit=recalledLimit+existedItem.getMemoryLimit(); memoryItems.removeItem(existedItem); memoryListIteartor=memoryItems.iterator(); }elseif(nowBase==(existedItem.getMemoryBase()+existedItem.getMemoryLimit())){ recalledBase=existedItem.getMemoryBase(); recalledLimit=recalledLimit+existedItem.getMemoryLimit(); memoryItems.removeItem(existedItem); memoryListIteartor=memoryItems.iterator(); } //memoryListIteartor=memoryItems.iterator();//注意此處代碼曾引發(fā)異常 } //recallItem=newMemoryItem(recalledBase,recalledLimit);//曾產(chǎn)生異常 } recallItem=newMemoryItem(recalledBase,recalledLimit); memoryItems.addItem(recallItem); sortMemoryList(); } publicintreadInteger(Strings){ intnum=-1; try{ num=Integer.parseInt(s); }catch(NumberFormatExceptionnumberformatexception){ statuesTextArea.append("Pleaseinputapositiveinteger!\n"); num=-999; } returnnum; } publicvoidaddToBackupList(StringnewID,ints1,ints2,ints3,Strings){ ProcessPCBitem=backupPCB.getItem(newID); if(item!=null){ statuesTextArea.append("ThePCB"+newID+"hasexistedinBackupList!\n" +"youneedtomodifythePIDoftheselectedPCB!\n"); while(item!=null){ newID=s+newID; item=backupPCB.getItem(newID);} }ProcessPCBnewPCB=newProcessPCB(newID,s1,s2,"Waiting",-1,s3);backupPCB.addItem(newPCB);backupList.setListData(backupPCB.getItemsProperties());backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems())); } publicvoidaddToReadyList(StringnowID,ints1,i
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 濱州醫(yī)學(xué)院《孟子導(dǎo)讀》2023-2024學(xué)年第一學(xué)期期末試卷
- 濱州科技職業(yè)學(xué)院《國際貨運(yùn)代理實(shí)務(wù)》2023-2024學(xué)年第一學(xué)期期末試卷
- 畢節(jié)職業(yè)技術(shù)學(xué)院《學(xué)前兒童衛(wèi)生與保健》2023-2024學(xué)年第一學(xué)期期末試卷
- 畢節(jié)工業(yè)職業(yè)技術(shù)學(xué)院《當(dāng)代藝術(shù)創(chuàng)意與產(chǎn)品設(shè)計(jì)》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025版綠色建筑節(jié)能改造施工合同6篇
- 解除終止勞動(dòng)合同證明書
- 2025年度智慧物流配送中心運(yùn)營管理服務(wù)合同3篇
- 房地產(chǎn)營銷策劃及銷售代理工作架構(gòu)和獨(dú)家代理合同書
- 租房協(xié)議模板房東租房合同
- 公司與公司借款合同范本
- 小兒肺炎支氣管鏡護(hù)理查房課件
- Part 6 Unit 8 Green Earth 教案-【中職專用】高一英語精研課堂(高教版2021·基礎(chǔ)模塊2)
- 艾森克人格問卷(EPQ)(成人)
- 設(shè)備維護(hù)與故障排除項(xiàng)目風(fēng)險(xiǎn)評(píng)估報(bào)告
- (新版)質(zhì)量知識(shí)競賽參考題庫400題(含各題型)
- 幼兒園小班語言《誰的耳朵》課件
- 院前急救診療技術(shù)操作規(guī)范
- 患者轉(zhuǎn)診記錄單
- 美好生活“油”此而來-暨南大學(xué)中國大學(xué)mooc課后章節(jié)答案期末考試題庫2023年
- 買賣合同糾紛案民事判決書
- 神經(jīng)內(nèi)科應(yīng)急預(yù)案完整版
評(píng)論
0/150
提交評(píng)論