《操作系統(tǒng)》課程設(shè)計報告_第1頁
《操作系統(tǒng)》課程設(shè)計報告_第2頁
《操作系統(tǒng)》課程設(shè)計報告_第3頁
《操作系統(tǒng)》課程設(shè)計報告_第4頁
《操作系統(tǒng)》課程設(shè)計報告_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

《操作系統(tǒng)》課程設(shè)計報告姓名:學(xué)號:級班組實驗室:提交日期成績指導(dǎo)教師實驗題目:基于軟件互斥算法的臨界區(qū)進程互斥的模擬實現(xiàn)實驗要求:(1)互斥算法選擇界面要求對Dekker、Peterson、Lamport、Eisenburg/Mcguire4個軟件解決互斥的算法進行模擬實現(xiàn)。(2)運行結(jié)果(界面)根據(jù)選擇的軟件互斥算法,能動態(tài)顯示2個或多個并發(fā)進程對臨界區(qū)的互斥執(zhí)行信息,包括處于臨界區(qū)內(nèi)的進程和要進入臨界區(qū)的進程。所需數(shù)據(jù)結(jié)構(gòu):數(shù)組算法設(shè)計:根據(jù)Dekker、Peterson算法原理,模擬實現(xiàn)2個線程訪問臨界區(qū)。根據(jù)Lamport、Eisenburg_Mcguire算法原理,模擬實現(xiàn)多個線程訪問臨界區(qū)。流程圖:以Dekker算法為例任務(wù)分工及各分工實現(xiàn)方法:黃兆龍:總體程序設(shè)計,整體界面設(shè)計及編寫以及最終代碼的整合,編寫報告實現(xiàn)方法:利用java編寫GUI及程序框架。楊霞:根據(jù)Dekker,Lamport算法,具體實現(xiàn)多進程訪問臨界區(qū)。實現(xiàn)方法:利用java的線程模擬進程,根據(jù)相應(yīng)算法原理實現(xiàn)進程互斥。李海?。焊鶕?jù)Peterson,Eisenburg算法,具體實現(xiàn)多進程訪問臨界區(qū)。實現(xiàn)方法:利用java的線程模擬進程,根據(jù)相應(yīng)算法原理實現(xiàn)進程互斥。測試結(jié)果:(1.主界面)(2.Dekker算法實現(xiàn))(3.Peterson算法實現(xiàn))(4.Lamport算法實現(xiàn))(5.Eisenburg算法實現(xiàn))(6.異常處理)總結(jié)(對所作程序進行分析、評價運行效果,總結(jié)遇到的問題和解決辦法)分析:程序利用GUI圖形化界面選擇算法,啟動后可根據(jù)相應(yīng)算法及輸入選項完成模擬進程訪問臨界區(qū)。評價:程序已成功執(zhí)行,運行效果還算良好,能夠直觀的體現(xiàn)進程進入臨界區(qū)以及出臨界區(qū)的情形。總結(jié):遇到的問題有很多,百度很多資料查不到,只能翻墻去Google查,不過最終都不完美的解決了。程序代碼:《Main.java》packagemutual_exclusion;importjavax.swing.JFrame;publicclassMain{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Contentcontent=newContent(); content.setSize(400,400); content.setLocation(400,200); content.setVisible(true); content.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}《Content.java》packagemutual_exclusion;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.FocusEvent;importjava.awt.event.FocusListener;importjavax.swing.*;importjavax.swing.event.*;publicclassContentextendsJFrame{ /** *@authorNapster;YangXia;LiHaiwei */ privatestaticfinallongserialVersionUID=1L; privatestaticintTHREADS; privateBorderLayoutbor_layout,button_layout; privateJPanelpanel,panel_button; privateJRadioButtonradiobutton[]; privateButtonGroupbuttongroup; privateJTextFieldtextField; privateJButtonbutton_ok; privateJTextAreatext; privateJScrollPanescroll; privateListener_Buttonlistener_button; privateListener_Oklistener_ok; privateintalg_ID; publicContent(){ super("進程互斥的模擬實現(xiàn)"); Containercontainer=this.getContentPane(); bor_layout=newBorderLayout(); container.setLayout(bor_layout); panel=newJPanel(); panel.setSize(300,100); button_layout=newBorderLayout(); panel.setLayout(button_layout); panel.setVisible(true); panel_button=newJPanel(newGridLayout(5,1)); radiobutton=newJRadioButton[4]; textField=newJTextField(); textField.setEditable(true); textField.addFocusListener(newFocusListener(){ @Override publicvoidfocusLost(FocusEventarg0){} @Override publicvoidfocusGained(FocusEventarg0){ textField.setText(""); } }); radiobutton[0]=newJRadioButton("Dekker"); radiobutton[1]=newJRadioButton("Peterson"); radiobutton[2]=newJRadioButton("Lamport"); radiobutton[3]=newJRadioButton("Eisenburg_Mcguire"); listener_button=newListener_Button(); radiobutton[0].addActionListener(listener_button); radiobutton[1].addActionListener(listener_button); radiobutton[2].addActionListener(listener_button); radiobutton[3].addActionListener(listener_button); panel_button.add(radiobutton[0]); panel_button.add(radiobutton[1]); panel_button.add(radiobutton[2]); panel_button.add(radiobutton[3]); panel_button.add(textField); buttongroup=newButtonGroup(); buttongroup.add(radiobutton[0]); buttongroup.add(radiobutton[1]); buttongroup.add(radiobutton[2]); buttongroup.add(radiobutton[3]); button_ok=newJButton("啟動"); button_ok.setSize(50,50); listener_ok=newListener_Ok(); button_ok.addActionListener(listener_ok); panel.add(panel_button,BorderLayout.NORTH); panel.add(button_ok,BorderLayout.SOUTH); text=newJTextArea(); scroll=newJScrollPane(text); text.setEditable(false); text.setAutoscrolls(true); container.add(panel,BorderLayout.EAST); container.add(scroll,BorderLayout.CENTER); } classListener_ButtonimplementsActionListener{ @Override publicvoidactionPerformed(ActionEventarg0){ //TODOAuto-generatedmethodstub if(arg0.getSource().equals(radiobutton[0])){ alg_ID=0; textField.setEditable(false); } if(arg0.getSource().equals(radiobutton[1])){ alg_ID=1; textField.setEditable(false); } if(arg0.getSource().equals(radiobutton[2])){ alg_ID=2; textField.setEditable(true); textField.setText("請輸入線程數(shù)(大于2)"); } if(arg0.getSource().equals(radiobutton[3])){ alg_ID=3; textField.setEditable(true); textField.setText("請輸入線程數(shù)(大于2)"); } } } classListener_OkimplementsActionListener{ @Override publicvoidactionPerformed(ActionEventarg0){ //TODOAuto-generatedmethodstub try{ if(!textField.getText().toString().equals("")) THREADS=Integer.valueOf(textField.getText().toString()); }catch(NumberFormatExceptione){ JOptionPane.showMessageDialog(null,"錯誤,請正確輸入!"); } switch(alg_ID){ case0: Dekker(); break; case1: Peterson(); break; case2: Lamport(); break; case3: Eisenburg_Mcguire(); break; } } privatevoidDekker(){ //TODOAuto-generatedmethodstub newThread(newDekker(0,text),"Thread-0").start(); newThread(newDekker(1,text),"Thread-1").start(); } privatevoidPeterson(){ //TODOAuto-generatedmethodstub newThread(newPeterson(0,text),"Thread-0").start(); newThread(newPeterson(1,text),"Thread-1").start(); } privatevoidLamport(){ //TODOAuto-generatedmethodstub for(inti=0;i<THREADS;i++){ newThread(newLamport(i,THREADS,text),"Thread-"+i) .start(); } } privatevoidEisenburg_Mcguire(){ //TODOAuto-generatedmethodstub for(inti=0;i<THREADS;i++){ newThread(newEisenburg_Mcguire(i,THREADS,text),"Thread-" +i).start(); } } }}《Dekker.java》packagemutual_exclusion;importjavax.swing.JTextArea;publicclassDekkerimplementsRunnable{ privatestaticboolean[]interested={false,false}; privatestaticvolatileintturn=-1; privatefinalintid; privatefinalJTextAreatext; publicDekker(inti,JTextAreatext1){ id=i; text=text1; text.setText(""); } privateintother(){ returnid==0?1:0; } @Override publicvoidrun(){ //TODO自動生成的方法存根 //announcethatIaminterestedintheresource interested[id]=true; //Ifotherthreadisalsointerestedintheresource,Iwillnotinterestedinit. //thenifit'sturntootherthread,waituntilotherthreadisfinished. //Ifotherthreadhasfinished,theniwillinterestedinitandenterthecriticalsection while(interested[other()]){ if(turn==other()){ interested[id]=false; while(turn==other()){ text.append("["+id+"]--Waiting...\n"); } interested[id]=true; } } /*criticalsection*/ try{ text.append("["+id+"]--Working\n"); Thread.sleep(1000); text.append("["+id+"]--Done!\n"); }catch(InterruptedExceptione){ //TODO自動生成的catch塊 e.printStackTrace(); } /*Leavingthecriticalsection*/ //IhavefinishednowandIt'sturntoothers. interested[id]=false; turn=other(); }}《Peterson.java》packagemutual_exclusion;importjavax.swing.JTextArea;publicclassPetersonimplementsRunnable{ privatestaticboolean[]interested={false,false}; privatestaticvolatileintturn=-1; privatefinalintid; privatefinalJTextAreatext; publicPeterson(inti,JTextAreatext1){ id=i; text=text1; text.setText(""); } privateintother(){ returnid==0?1:0; } @Override publicvoidrun(){ //announcethatIaminterestedintheresource interested[id]=true; //Itmeansthatotherthreadmayhasenteredthecriticalsection turn=other(); //Ifotherthreadisalsointerestedandit'sturntootherthread //waituntilotherthreadisfinished. while(interested[other()]&&turn==other()){ text.append("["+id+"]--Waiting...\n"); } //Ifotherthreadhasfinished,theniwillinterestedinitandenterthecriticalsection /*criticalsection*/ try{ text.append("["+id+"]--Working\n"); Thread.sleep(1000); text.append("["+id+"]--Done!\n"); }catch(InterruptedExceptione){ //TODO自動生成的catch塊 e.printStackTrace(); } /*Leavingthecriticalsection*/ interested[id]=false; }}《Lamport.java》packagemutual_exclusion;importjava.util.concurrent.atomic.AtomicIntegerArray;importjavax.swing.JTextArea;publicclassLamportimplementsRunnable{ privatestaticintthreads; //AtomicInteger:achieveatomizationoftheintegervariableoperations //queueforthreadsinline. privatestaticAtomicIntegerArrayqueue; //needthisfornon-atomicmaxselection privatestaticAtomicIntegerArrayentering; privatefinalintid; privatefinalJTextAreatext; publicLamport(inti,intN,JTextAreatext1){ id=i; text=text1; text.setText(""); Lamport.threads=N; queue=newAtomicIntegerArray(threads); entering=newAtomicIntegerArray(threads); for(intj=0;j<threads;++j){ queue.set(j,0); entering.set(j,0); } } @Override publicvoidrun(){ //TODO自動生成的方法存根 entering.set(id,1);//Wanttoenter queue.set(id,max(queue)+1);//Enterthequeue entering.set(id,0);//Hasentered for(inti=0;i<queue.length();++i){ if(i!=id){ /* *Ifatthistimeotherthreadwantstoenterthequeue,wait *whileotherthreadhasenteredthequeue. */ while(entering.get(i)==1){ text.append("["+id+"]--Waiting...\n"); } /* *Ifatthistimeotherthreadhasthehighpriority,waitfor *theotherthreadisfinished. */ while(queue.get(i)!=0 &&(queue.get(id)>queue.get(i)||(queue.get(id)==queue .get(i)&&id>i))){ text.append("["+id+"]--Waiting...\n"); } } } /*criticalsection*/ try{ text.append("["+id+"]--Working\n"); Thread.sleep(1000); text.append("["+id+"]--Done!\n"); }catch(InterruptedExceptione){ //TODO自動生成的catch塊 e.printStackTrace(); } /*Leavingthecriticalsection*/ queue.set(id,0); } privateintmax(AtomicIntegerArrayqueue2){ //TODO自動生成的方法存根 intmax=0; for(inti=0;i<queue2.length();i++){ if(queue2.get(i)>=max){ max=queue2.get(i); } } returnmax; }}《Eisenburg_Mcguire.java》packagemutual_exclusion;importjavax.swing.JTextArea;publicclassEisenburg_McguireimplementsRunnable{ privatestaticintthreads; privatestaticenumstates{ IDLE,WAITTING,ACTIVE }; privatestaticstatesflags[]; privatestaticvolatileintturn=0; privateintindex; privatefinalintid; privatefinalJTextAreatext; publicEisenburg_Mcguire(inti,intN,JTextAreatext1){ id=i; text=text1; text.setText(""); Eisenburg_Mcguire.threads=N; flags=newstates[threads]; for(index=0;index<threads;++index){ flags[index]=states.IDLE; } } @Override publicvoidrun(){ //TODO自動生成的方法存根 flags[id]=states.WAITTING; /*scanprocessesfromtheonewiththeturnuptoourselves.*/ /*repeatifnecessaryuntilthescanfindsallprocessesidle*/ index=turn; do{ while(ind

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論