實(shí)驗(yàn)一-進(jìn)程調(diào)度_第1頁
實(shí)驗(yàn)一-進(jìn)程調(diào)度_第2頁
實(shí)驗(yàn)一-進(jìn)程調(diào)度_第3頁
實(shí)驗(yàn)一-進(jìn)程調(diào)度_第4頁
實(shí)驗(yàn)一-進(jìn)程調(diào)度_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

實(shí)驗(yàn)一進(jìn)程調(diào)度一、實(shí)驗(yàn)?zāi)康亩嗟莱绦蛟O(shè)計(jì)中,經(jīng)常是假設(shè)干個(gè)進(jìn)程同時(shí)處于就緒狀態(tài),必須依照某種策略來決定那個(gè)進(jìn)程優(yōu)先占有處理機(jī)。因而引起進(jìn)程調(diào)度。本實(shí)驗(yàn)?zāi)M在單處理機(jī)情況下的處理機(jī)調(diào)度問題,加深對(duì)進(jìn)程調(diào)度的理解。二、實(shí)驗(yàn)內(nèi)容優(yōu)先權(quán)法、輪轉(zhuǎn)法簡(jiǎn)化假設(shè)進(jìn)程為計(jì)算型的〔無I/O〕進(jìn)程狀態(tài):ready、running、finish進(jìn)程需要的CPU時(shí)間以時(shí)間片為單位確定算法描述優(yōu)先權(quán)法——?jiǎng)討B(tài)優(yōu)先權(quán)當(dāng)前運(yùn)行進(jìn)程用完時(shí)間片后,其優(yōu)先權(quán)減去一個(gè)常數(shù)。輪轉(zhuǎn)法三、流程圖開始開始鍵盤輸入進(jìn)程數(shù)n,和調(diào)度方法的選擇優(yōu)先權(quán)法?輪轉(zhuǎn)法產(chǎn)生n個(gè)進(jìn)程,對(duì)每個(gè)進(jìn)程產(chǎn)生一個(gè)PCB,并用隨機(jī)數(shù)產(chǎn)生進(jìn)程的優(yōu)先權(quán)及進(jìn)程所需的CPU時(shí)間按優(yōu)先權(quán)大小,把n個(gè)進(jìn)程拉成一個(gè)就緒隊(duì)列初始化其他數(shù)據(jù)結(jié)構(gòu)區(qū)鏈?zhǔn)走M(jìn)程投入運(yùn)行時(shí)間片到,進(jìn)程所需的CPU時(shí)間減1,優(yōu)先權(quán)減3,輸出個(gè)進(jìn)程的運(yùn)行情況所需的CPU時(shí)間=0?撤銷進(jìn)程就緒隊(duì)列為空?結(jié)束將進(jìn)程插入就緒隊(duì)列NYNYYBN產(chǎn)生n個(gè)進(jìn)程,對(duì)每個(gè)進(jìn)程用隨機(jī)數(shù)產(chǎn)生進(jìn)程的輪轉(zhuǎn)時(shí)間片數(shù)及進(jìn)程所需的時(shí)間片數(shù),已占用CPU的時(shí)間片數(shù)置為0按進(jìn)程產(chǎn)生的先后次序拉成就緒隊(duì)列鏈產(chǎn)生n個(gè)進(jìn)程,對(duì)每個(gè)進(jìn)程用隨機(jī)數(shù)產(chǎn)生進(jìn)程的輪轉(zhuǎn)時(shí)間片數(shù)及進(jìn)程所需的時(shí)間片數(shù),已占用CPU的時(shí)間片數(shù)置為0按進(jìn)程產(chǎn)生的先后次序拉成就緒隊(duì)列鏈鏈?zhǔn)走M(jìn)程投入運(yùn)行時(shí)間片到,進(jìn)程所需時(shí)間片數(shù)減1,已占用CPU時(shí)間片數(shù)加1輸出各進(jìn)程的運(yùn)行情況進(jìn)程所需時(shí)間片數(shù)=0?撤銷該進(jìn)程就緒隊(duì)列為空嗎?占用CPU的時(shí)間片數(shù)=輪轉(zhuǎn)時(shí)間片數(shù)?占用CPU的時(shí)間片數(shù)置為0把該進(jìn)程插入就緒隊(duì)列尾BNYNYY結(jié)束N開始鍵盤輸入進(jìn)程數(shù)n,和調(diào)度方法的選擇優(yōu)先權(quán)法?輪轉(zhuǎn)法開始鍵盤輸入進(jìn)程數(shù)n,和調(diào)度方法的選擇優(yōu)先權(quán)法?輪轉(zhuǎn)法產(chǎn)生n個(gè)進(jìn)程,對(duì)每個(gè)進(jìn)程產(chǎn)生一個(gè)PCB,并用隨機(jī)數(shù)產(chǎn)生進(jìn)程的優(yōu)先權(quán)及進(jìn)程所需的CPU時(shí)間按優(yōu)先權(quán)大小,把n個(gè)進(jìn)程拉成一個(gè)就緒隊(duì)列初始化其他數(shù)據(jù)結(jié)構(gòu)區(qū)鏈?zhǔn)走M(jìn)程投入運(yùn)行時(shí)間片到,進(jìn)程所需的CPU時(shí)間減1,優(yōu)先權(quán)減3,輸出個(gè)進(jìn)程的運(yùn)行情況所需的CPU時(shí)間=0?撤銷進(jìn)程就緒隊(duì)列為空?結(jié)束將進(jìn)程插入就緒隊(duì)列NYNYYBN進(jìn)程數(shù)n不要太大通常取4~8個(gè)使用動(dòng)態(tài)數(shù)據(jù)結(jié)構(gòu)獨(dú)立編程至少三種調(diào)度算法假設(shè)有可能請(qǐng)?jiān)趫D形方式下,將PCB的調(diào)度用圖形成動(dòng)畫顯示。五、源程序代碼1.PCB類packageProcessScheduing;publicclassPCB{privateintID;//進(jìn)程ID號(hào)privateintPriority;//進(jìn)程優(yōu)先權(quán)privateintCPUtime;//進(jìn)程執(zhí)行CPU時(shí)間privateStringState;//進(jìn)程狀態(tài)privateintNeedtime;//進(jìn)程需要的時(shí)間片數(shù)privatePCBnextPCB;//下一跳進(jìn)程publicintgetID(){returnID; }publicvoidsetID(intID){this.ID=ID; }publicintgetPriority(){returnPriority; }publicvoidsetPriority(intPriority){this.Priority=Priority; }publicintgetCPUtime(){returnCPUtime; }publicvoidsetCPUtime(intCPUtime){this.CPUtime=CPUtime; }publicStringgetState(){returnState; }publicvoidsetState(StringState){this.State=State; }publicStringtoString(){return"ID:"+ID+"Priority:"+Priority+"CPUtime:"+CPUtime+"State:"+State+"\n"; }publicStringtoTimeChangeString(){return"ID:"+ID+"NeedTime:"+Needtime+"State:"+State+"\n"; }publicvoidrunGO(){CPUtime=CPUtime-1;Priority=Priority-3; }publicPCBgetNextPCB(){returnnextPCB; }publicvoidsetNextPCB(PCBnextPCB){this.nextPCB=nextPCB; }publicintgetNeedtime(){returnNeedtime; }publicvoidsetNeedtime(intneedtime){Needtime=needtime; }}2.PCB操作類packageProcessScheduing;importjava.util.ArrayList;importjava.util.Random;classQueue{ PCBfront; PCBrear;}publicclassPCB_Operation{privateArrayList<PCB>mPCBs=newArrayList<PCB>();//進(jìn)程順序表privateQueuereadyQueue=newQueue();//就緒隊(duì)列privateRandomrandom=newRandom();//隨機(jī)數(shù)privateinthaveCPUtime;//占用CPU的時(shí)間片數(shù)privateinteveryPCBruntime;//進(jìn)程時(shí)間片數(shù)privateStringstring=newString();publicQueuegetReadyQueue(){returnreadyQueue; }publicvoidsetReadyQueue(QueuereadyQueue){this.readyQueue=readyQueue; }//產(chǎn)生N個(gè)PCB放于mPCBs中,方便后面調(diào)用publicvoidinitPCBs(intN){for(inti=0;i<N;i++){ PCBmPCB=newPCB(); mPCB.setID(i); mPCB.setPriority(random.nextInt(20)); mPCB.setCPUtime(random.nextInt(19)+1); mPCB.setNeedtime(random.nextInt(19)+1); mPCB.setState("就緒"); mPCB.setNextPCB(null);mPCBs.add(i,mPCB); } }//初始化就緒隊(duì)列publicQueueinitReadyQueue(){ getReadyQueue().front=getReadyQueue().rear=newPCB();for(inti=0;i<mPCBs.size();i++){ insertReadyQueue(getReadyQueue(),mPCBs.get(i)); }returngetReadyQueue(); }//按優(yōu)先權(quán)大小向就緒隊(duì)列插入PCBpublicvoidinsertReadyQueue(QueuereadyQueue,PCBinPCB){ PCBtp=newPCB(),mp=newPCB(); inPCB.setNextPCB(null);if(readyQueue.front==readyQueue.rear){ readyQueue.rear.setNextPCB(inPCB); readyQueue.rear=inPCB; } mp=readyQueue.front; tp=readyQueue.front.getNextPCB(); while(tp!=null){if(inPCB.getPriority()>=tp.getPriority()){ mp.setNextPCB(inPCB); inPCB.setNextPCB(tp);break; } mp=tp; tp=tp.getNextPCB();if(mp==readyQueue.rear){ readyQueue.rear.setNextPCB(inPCB); readyQueue.rear=inPCB;break; } } }//向隊(duì)列尾插入進(jìn)程publicvoidinsertQueueRear(PCBinPCB){ getReadyQueue().rear.setNextPCB(inPCB); getReadyQueue().rear=inPCB; }//展示就緒隊(duì)列中所有進(jìn)程publicvoiddisplayAll(QueuereadyQueue){ PCBfrontPcb=readyQueue.front.getNextPCB();while(frontPcb!=null){ setString(getString()+frontPcb.toString()); System.out.println(frontPcb.toString()); frontPcb=frontPcb.getNextPCB(); } }//優(yōu)先權(quán)法單步執(zhí)行publicQueueSingleRun(QueuereadyQueue){ PCBrunPCB=readyQueue.front.getNextPCB();if(runPCB.getNextPCB()==null){ runPCB.runGO();if(runPCB.getCPUtime()==0){ Strings4="ID為"+runPCB.getID()+"的PCB執(zhí)行結(jié)束"; setString(getString()+(s4+="\n")); readyQueue.front.setNextPCB(null); }returnreadyQueue; } readyQueue.front.setNextPCB(runPCB.getNextPCB()); runPCB.runGO();if(runPCB.getCPUtime()>0){ insertReadyQueue(readyQueue,runPCB); }else{ Strings1="ID為"+runPCB.getID()+"的PCB執(zhí)行結(jié)束"; setString(getString()+(s1+="\n")); System.out.println(s1); }returnreadyQueue; }//動(dòng)態(tài)優(yōu)先權(quán)法publicvoidPriorityALG(){ QueuereadyQueue=initReadyQueue(); Strings2="初始化就緒隊(duì)列"; System.out.println(s2); setString(getString()+(s2+="\n")); displayAll(readyQueue);intN=1; PCBfrontPcb=readyQueue.front.getNextPCB();while(frontPcb!=null){ readyQueue=SingleRun(readyQueue); Strings3="執(zhí)行"+N+++"次后的就緒隊(duì)列"; System.out.println(s3); setString(getString()+(s3+="\n")); displayAll(readyQueue); frontPcb=readyQueue.front.getNextPCB(); } }//時(shí)間片輪轉(zhuǎn)隊(duì)列publicQueueinitTimeChangeQueue(){ getReadyQueue().front=getReadyQueue().rear=newPCB();for(inti=0;i<mPCBs.size();i++){ getReadyQueue().rear.setNextPCB(mPCBs.get(i)); getReadyQueue().rear=mPCBs.get(i); } returngetReadyQueue(); }//時(shí)間片輪轉(zhuǎn)法初始化publicvoidinitTimeChange(){everyPCBruntime=random.nextInt(10)+1;haveCPUtime=0; }//展示時(shí)間片輪轉(zhuǎn)中就緒隊(duì)列中所有進(jìn)程publicvoiddisplayTimeChangeAll(QueuereadyQueue){ PCBfrontPcb=readyQueue.front.getNextPCB();while(frontPcb!=null){ Strings0=frontPcb.toTimeChangeString();string+=s0; System.out.println(s0); frontPcb=frontPcb.getNextPCB(); } }//時(shí)間片輪轉(zhuǎn)法publicvoidTimeChange(){ initTimeChange(); setReadyQueue(initTimeChangeQueue()); Strings1="初始化就緒隊(duì)列:"+"進(jìn)程時(shí)間片"+everyPCBruntime; System.out.println(s1);string+=s1+="\n"; displayTimeChangeAll(getReadyQueue()); PCBpointer=newPCB();intN=1;while(getReadyQueue().front!=getReadyQueue().rear){ if(getReadyQueue().front.getNextPCB()==null &&getReadyQueue().front.getNeedtime()==0){break; } getReadyQueue().front.getNextPCB().setNeedtime(getReadyQueue().front.getNextPCB().getNeedtime()-1);haveCPUtime=haveCPUtime+1; Strings2="執(zhí)行"+N+++"次后的就緒隊(duì)列:"+"CPU時(shí)間片:"+haveCPUtime; System.out.println(s2);string+=s2+="\n"; displayTimeChangeAll(getReadyQueue());if(getReadyQueue().front.getNextPCB().getNeedtime()>0){if(haveCPUtime==everyPCBruntime){haveCPUtime=0; pointer=getReadyQueue().front.getNextPCB();if(pointer.getNextPCB()==null){continue; }else{ getReadyQueue().front.setNextPCB(pointer.getNextPCB()); pointer.setNextPCB(null); insertQueueRear(pointer);continue; } }else{continue; } }else{ Strings3="ID:"+getReadyQueue().front.getNextPCB().getID()+"進(jìn)程執(zhí)行結(jié)束";string+=s3+="\n";if(haveCPUtime==everyPCBruntime)haveCPUtime=0;if(getReadyQueue().front.getNextPCB().getNeedtime()==0&& getReadyQueue().front==getReadyQueue().rear){break; } getReadyQueue().front.setNextPCB(getReadyQueue().front.getNextPCB().getNextPCB()); } } }publicStringgetString(){returnstring; }publicvoidsetString(Stringstring){this.string=string; }}3.界面類packageProcessScheduing;importorg.eclipse.swt.widgets.Display;publicclassProcessSWT{protectedShellshell;privateTexttext;privateTexttext_1;privateintN;privatePCB_OperationmOperation;/** *Launchtheapplication. *@paramargs */publicstaticvoidmain(String[]args){try{ ProcessSWTwindow=newProcessSWT(); 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.setSize(450,300);shell.setText("進(jìn)程調(diào)度");shell.setLayout(null); ScrolledCompositescrolledComposite=newScrolledComposite(shell,SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL); scrolledComposite.setBounds(160,10,251,242); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setExpandVertical(true);text=newText(scrolledComposite,SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL|SWT.CANCEL);text.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));text.setEditable(false); scrolledComposite.setContent(text); scrolledComposite.setMinSize(textputeSize(SWT.DEFAULT,SWT.DEFAULT)); Labellabel=newLabel(shell,SWT.NONE); label.setBounds(29,23,87,17); label.setText("\u8BF7\u8F93\u5165\u8FDB\u7A0B\u6570\uFF1A");text_1=newText(shell,SWT.BORDER);text_1.setBounds(29,56,73,23); Buttonbutton=newButton(shell,SWT.NONE); button.setBounds(29,95,80,27); button.addSelectionListener(newSelectionAdapter(){@OverridepublicvoidwidgetSelected(SelectionEvente){text.setText("");mOperation=newPCB_Operation();N=Integer.parseInt(text_1.getText());mOperation.initPCBs(N);mOperation.PriorityALG(); Stringstring=mOperation.getString();text.setText(string); } }); button.setTex

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論