版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
南京工程學(xué)院操作系統(tǒng)課程設(shè)計(jì)說明書題目操作系統(tǒng)—銀行家算法和頁面置換算法班級(jí)軟件101學(xué)生姓名劉洋學(xué)號(hào)202100320課設(shè)截圖:源代碼:3個(gè)類:MainFrame.java:importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JRadioButton;importjavax.swing.UIManager;importjavax.swing.UIManager.LookAndFeelInfo;publicclassMainFrameextendsJFrameimplementsActionListener{/** * */privatestaticfinallongserialVersionUID=1L;privateJPanelpanel1,panel2,panel3;privateJLabellabel1,label2;privateJButtonbutton1;privateJRadioButtonb1,b2;publicMainFrame(){ super("操作系統(tǒng)課程設(shè)計(jì)"); this.setBounds(300,150,800,500); this.setBackground(java.awt.Color.LIGHT_GRAY); this.setLayout(newjava.awt.GridLayout(3,1)); panel1=newJPanel(); panel2=newJPanel(); panel3=newJPanel(); Fontf=newFont("楷體",1,60); Fontf1=newFont("宋體",3,30); Fontf2=newFont("幼圓",1,15); label1=newJLabel("操作系統(tǒng)課程設(shè)計(jì)"); label1.setFont(f); label2=newJLabel("算法模擬題目:"); label2.setFont(f1); panel1.add(label1); panel2.add(label2); ButtonGroupbgroup=newButtonGroup(); b1=newJRadioButton("銀行家算法",true); b1.setFont(f1); bgroup.add(b1); b2=newJRadioButton("頁面置換算法"); b2.setFont(f1); bgroup.add(b2); panel2.add(b1); panel2.add(b2); button1=newJButton("開始演示"); button1.setFont(f2); button1.addActionListener(this); panel3.add(button1); this.add(panel1); this.add(panel2); this.add(panel3); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);} publicvoidactionPerformed(ActionEvente) { if(e.getSource()==button1) { if(b1.isSelected()) { newBanker(); } if(b2.isSelected()) { newYemian(); } } } publicstaticvoidmain(String[]args){ try{ for(LookAndFeelInfoinfo:UIManager.getInstalledLookAndFeels()){ if("Nimbus".equals(info.getName())){ UIManager.setLookAndFeel(info.getClassName()); break; } } } catch(Exceptione1) {}newMainFrame();}}Banker.java:importjava.awt.*;importjava.awt.event.*;importjava.util.ArrayList;importjavax.swing.*;importjavax.swing.table.DefaultTableCellRenderer;importjavax.swing.table.DefaultTableModel;publicclassBankerextendsJFrameimplementsActionListener{ /** * */ privatestaticfinallongserialVersionUID=1L; privateJTabletable1,table2;privateJPanelp0,p1,p11,p12,p13,p14,p2,p3,p31,p32,p33,p34,p4,p5;privateJLabelt1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;privateJButtonb1,b2,b3,b4,b5,b6;privateTextFieldtext01,text02,text03,text04,text05,text06;//編輯框privateJTextField[]text1,text2,text3,text4,text5;//進(jìn)程組的數(shù)據(jù)DefaultTableModeltablemodel,tablemodel1;ArrayList<Integer>list;int[][]claim,allocation,need;int[]available; int[]request,temavail,release;boolean[]possible;intm,n,index=0;publicBanker(){ super("銀行家算法模擬"); p0=newJPanel(); p1=newJPanel(); p11=newJPanel(); p12=newJPanel(); p13=newJPanel(); p14=newJPanel(); p2=newJPanel(); p3=newJPanel(); p31=newJPanel(); p32=newJPanel(); p33=newJPanel(); p34=newJPanel(); p4=newJPanel(); p5=newJPanel(); p0.setLayout(newGridLayout(5,1)); p1.setLayout(newGridLayout(4,1)); p3.setLayout(newGridLayout(4,1)); p1.add(p11); p1.add(p12); p1.add(p13); p1.add(p14); p3.add(p31); p3.add(p32); p3.add(p33); p3.add(p34); p0.add(p1); p0.add(p2); p0.add(p3); p0.add(p4); p0.add(p5); t1=newJLabel("進(jìn)程數(shù)"); t2=newJLabel("資源數(shù)"); t3=newJLabel("進(jìn)程號(hào)"); t4=newJLabel("已分配資資源:"); t5=newJLabel("資源最大需求:"); t6=newJLabel("可用資源:"); t7=newJLabel("請(qǐng)求資源進(jìn)程號(hào)"); t8=newJLabel("請(qǐng)求資源為"); t9=newJLabel("釋放資源"); t10=newJLabel("平安序列"); t11=newJLabel("釋放資源進(jìn)程號(hào)"); b1=newJButton("確定"); b2=newJButton("添加"); b3=newJButton("確定"); b4=newJButton("請(qǐng)求"); b5=newJButton("開始檢測(cè)"); b6=newJButton("釋放"); text1=newJTextField[6];text2=newJTextField[6];text3=newJTextField[6];text4=newJTextField[6];text5=newJTextField[6]; for(inti=0;i<5;i++) { text1[i]=newJTextField(4); text2[i]=newJTextField(4); text3[i]=newJTextField(4); text4[i]=newJTextField(4); text5[i]=newJTextField(4); } text01=newTextField(4); text02=newTextField(4); text03=newTextField(4); text04=newTextField(4); text05=newTextField(4); text06=newTextField(20); String[]columnNames1={"進(jìn)程號(hào)","allocation","claim","need","available"}; tablemodel=newDefaultTableModel(columnNames1,0); table1=newJTable(tablemodel); table1.setPreferredScrollableViewportSize(newDimension(700,200)); table1.setRowHeight(20); table1.doLayout(); DefaultTableCellRendererr=newDefaultTableCellRenderer(); r.setHorizontalAlignment(JLabel.CENTER); table1.setDefaultRenderer(Object.class,r); JScrollPanepane1=newJScrollPane(table1); p11.add(t1); p11.add(text01); p11.add(t2); p11.add(text02); p11.add(b1); p12.add(t3); p12.add(text03); p12.add(b2); p13.add(t4); for(inti=0;i<5;i++) p13.add(text1[i]); p14.add(t5); for(inti=0;i<5;i++) p14.add(text2[i]); p2.add(pane1); p31.add(t6); for(inti=0;i<5;i++) p31.add(text3[i]); p31.add(b3); p32.add(t7); p32.add(text04); p32.add(t8); for(inti=0;i<5;i++) p32.add(text4[i]); p32.add(b4); p33.add(t11); p33.add(text05); p33.add(t9); for(inti=0;i<5;i++) p33.add(text5[i]); p33.add(b6); p34.add(b5); String[]columnNames2={"進(jìn)程號(hào)","currentavail","need","allocation","curr...+allo...","possible"}; tablemodel1=newDefaultTableModel(columnNames2,0); table2=newJTable(tablemodel1); table2.setPreferredScrollableViewportSize(newDimension(700,200)); table2.setRowHeight(20); table2.doLayout(); DefaultTableCellRendererr1=newDefaultTableCellRenderer(); r1.setHorizontalAlignment(JLabel.CENTER); table2.setDefaultRenderer(Object.class,r1); JScrollPanepane2=newJScrollPane(table2); p4.add(pane2); p5.add(t10); p5.add(text06); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); p0.setBackground(Color.lightGray); list=newArrayList<Integer>(); this.setContentPane(p0); this.setVisible(true); this.pack(); this.setLocation(300,10); this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);} publicvoidactionPerformed(ActionEvente) { if(e.getSource()==b1) { try{ m=Integer.parseInt(text01.getText()); }catch(NumberFormatExceptione1) { JOptionPane.showMessageDialog(this,"進(jìn)程數(shù)不能為空"); return; } try{ n=Integer.parseInt(text02.getText()); }catch(NumberFormatExceptione1) { JOptionPane.showMessageDialog(this,"資源數(shù)不能為空"); return; } claim=newint[m][n]; need=newint[m][n]; allocation=newint[m][n]; available=newint[n]; request=newint[n]; release=newint[n]; for(inti=0;i<5-n;i++) p13.remove(text1[4-i]); p13.updateUI(); for(inti=0;i<5-n;i++) p14.remove(text2[4-i]); p14.updateUI(); for(inti=0;i<5-n;i++) p31.remove(text3[4-i]); p31.updateUI(); for(inti=0;i<5-n;i++) p32.remove(text4[4-i]); p32.updateUI(); for(inti=0;i<5-n;i++) p33.remove(text5[4-i]); p33.updateUI(); temavail=newint[n]; Stringstr[]={"","","","","",""}; for(inti=0;i<m;i++) tablemodel1.addRow(str); } if(e.getSource()==b2) { Stringname[]=newString[m]; if(index>=m) { JOptionPane.showMessageDialog(this,"進(jìn)程個(gè)數(shù)已滿"); return; } try{ for(intj=0;j<n;j++) { allocation[index][j]=Integer.parseInt(text1[j].getText()); claim[index][j]=Integer.parseInt(text2[j].getText()); need[index][j]=claim[index][j]-allocation[index][j]; } }catch(Exceptionf){} name[index]="P"+index; Stringstrd[]={"","","","",""}; strd[0]=name[index]; for(intj=0;j<n;j++) { strd[1]+=allocation[index][j]+""; } for(intj=0;j<n;j++) { strd[2]+=claim[index][j]+""; } for(intj=0;j<n;j++) { strd[3]+=need[index][j]+""; } strd[4]=""; tablemodel.addRow(strd); index++; } if(e.getSource()==b3) { try{ for(inti=0;i<n;i++) { available[i]=Integer.parseInt(text3[i].getText()); temavail[i]=available[i]; } }catch(NumberFormatExceptionf) { JOptionPane.showMessageDialog(this,"可用資源不能為空,請(qǐng)重新輸入"); return; } Stringstr=""; for(inti=0;i<n;i++) str+=available[i]+""; tablemodel.setValueAt(str,0,4); } if(e.getSource()==b5) { list.clear(); safe(); for(inti=0;i<table1.getRowCount();i++) tablemodel1.removeRow(0); update_table2(); } if(e.getSource()==b4) { for(inti=0;i<n;i++) { try{ request[i]=Integer.parseInt(text4[i].getText()); }catch(NumberFormatExceptionf1) { JOptionPane.showMessageDialog(this,"請(qǐng)求資源不能為空,請(qǐng)重新輸入"); return; } } requestresource(); } if(e.getSource()==b6) { for(inti=0;i<n;i++) { try{ release[i]=Integer.parseInt(text5[i].getText()); }catch(NumberFormatExceptionf1) { JOptionPane.showMessageDialog(this,"釋放資源不能為空,請(qǐng)重新輸入"); return; } } releaseresource(); } } privatebooleansafe() { booleanresult=false; for(inti=0;i<m;i++) { intj=0; while(j<m) { booleancan=true; for(intk=0;k<n;k++) { if(need[j][k]>available[k]) can=false; } if(can&&!list.contains((Object)j)) { list.add(j); for(intk=0;k<n;k++) available[k]+=allocation[j][k];} j++; if(j>=m) break; } if(i==m-1) { break; } } if(list.size()==m) { for(intk=0;k<n;k++) available[k]=temavail[k]; result=true; } returnresult; } publicvoidupdate_table1(){ intindex1=Integer.parseInt(text04.getText()); Stringstr[]={"","",""}; for(intk=0;k<n;k++) str[0]+=allocation[index1][k]+""; tablemodel.setValueAt(str[0],index1,1); for(intk=0;k<n;k++) str[1]+=need[index1][k]+""; tablemodel.setValueAt(str[1],index1,3); for(intk=0;k<n;k++) str[2]+=available[k]+""; tablemodel.setValueAt(str[2],0,4);}publicvoidupdate_table2(){ if(safe()) { Stringtemp=""; for(Integerx:list) { temp+=("P"+x+","); Stringstr[]={"","","","","",""}; str[0]="P"+x; for(intk=0;k<n;k++) str[1]+=available[k]+""; for(intk=0;k<n;k++) str[2]+=need[x][k]+""; for(intk=0;k<n;k++) str[3]+=allocation[x][k]+""; for(intk=0;k<n;k++) available[k]+=allocation[x][k]; for(intk=0;k<n;k++) str[4]+=available[k]+""; str[5]="True"; tablemodel1.addRow(str); } for(intk=0;k<n;k++) available[k]=temavail[k]; text06.setText(temp); } else { text06.setText("不存在平安序列"); JOptionPane.showMessageDialog(this,"系統(tǒng)處于不平安狀態(tài),不能分配資源!!"); return; } }publicvoidrequestresource(){ intindex1=Integer.parseInt(text04.getText()); for(inti=0;i<n;i++) { if(request[i]<=need[index1][i]&&request[i]<=available[i]) { temavail[i]-=request[i]; allocation[index1][i]+=request[i]; need[index1][i]-=request[i]; if(safe()) { update_table1(); } else { temavail[i]+=request[i]; } } else { JOptionPane.showMessageDialog(this,"系統(tǒng)資源缺乏,申請(qǐng)被拒絕!"); return; } }}publicvoidreleaseresource(){ intindex2=Integer.parseInt(text05.getText()); for(inti=0;i<n;i++) { available[i]+=release[i]; temavail[i]+=release[i]; allocation[index2][i]-=release[i]; need[index2][i]+=release[i]; } update_table1();}}Yemian.java:importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.ArrayList;importjava.util.LinkedList;importjavax.swing.*;publicclassYemianextendsJFrameimplementsActionListener{ /** * */ privatestaticfinallongserialVersionUID=1L; privateintmSIZE=20;//頁框最大個(gè)數(shù) privatestaticintpSIZE=20;//頁面最大個(gè)數(shù) privatestaticintprocess[]=newint[pSIZE];;//隨機(jī)產(chǎn)生的頁面走向 staticStringmemString=""; privateJButtondemoFIFO; privateJButtondemoLRU; privateJComboBoxproduce;//隨機(jī)產(chǎn)生或輸入數(shù)據(jù) privateJTextAreafifoArea; privateJTextArealruArea; privateJButtonmakeProcess;//頁面 privateJButtonmakememery;//頁框 privateJLabelprocessLabel; privateJLabelmakePLabel; privateJLabelmakeMLabel; privateJTextFieldprocessField; privateJTextFieldmemeryField; privateJPanelproducePanel; privateJPanelinputmemery; privateJPanelinputprocess; privateJPanellruPanel; privateJPanelfifoPanel; privateJScrollPanelruScrollPane; privateJScrollPanefifoScrollPane; publicYemian() { super("頁面置換算法FIFO、LRU"); demoFIFO=newJButton("演示FIFO"); demoLRU=newJButton("演示LRU"); Stringxz[]={"隨機(jī)頁面序列","輸入頁面序列"}; produce=newJComboBox(xz); processLabel=newJLabel("頁號(hào)序列"); makememery=newJButton("確定頁框個(gè)數(shù)"); makeMLabel=newJLabel("請(qǐng)輸入頁框個(gè)數(shù):"); makeProcess=newJButton("確定頁面?zhèn)€數(shù)"); makePLabel=newJLabel("請(qǐng)輸入頁面?zhèn)€數(shù):"); producePanel=newJPanel(newGridLayout(2,1)); producePanel.setBackground(Color.YELLOW); inputmemery=newJPanel(); inputmemery.setLayout(null); inputmemery.setBackground(Color.GREEN); inputprocess=newJPanel(); inputprocess.setLayout(null); inputprocess.setBackground(Color.MAGENTA); lruPanel=newJPanel(); lruPanel.setLayout(null); lruPanel.setBackground(Color.PINK); fifoPanel=newJPanel(); fifoPanel.setLayout(null); fifoPanel.setBackground(Color.ORANGE); processField=newJTextField(); memeryField=newJTextField(); fifoArea=newJTextArea(); lruArea=newJTextArea(); fifoScrollPane=newJScrollPane(fifoArea); lruScrollPane=newJScrollPane(lruArea); this.setLayout(null); this.setBackground(Color.BLUE); producePanel.setBounds(5,5,706,90); inputmemery.setBounds(5,95,200,220); inputprocess.setBounds(5,320,200,220); fifoPanel.setBounds(210,95,210,450); lruPanel.setBounds(500,95,210,450); this.add(producePanel); this.add(inputmemery); this.add(inputprocess); this.add(fifoPanel); this.add(lruPanel); makeMLabel.setBounds(30,15,170,45); memeryField.setBounds(30,85,150,40); makememery.setBounds(30,140,150,45); inputmemery.add(makememery); inputmemery.add(memeryField); inputmemery.add(makeMLabel); makePLabel.setBounds(30,15,170,45); processField.setBounds(30,85,150,40); makeProcess.setBounds(30,140,150,45); inputprocess.add(makePLabel); inputprocess.add(processField); inputprocess.add(makeProcess); produce.setBounds(150,25,150,45); demoFIFO.setBounds(60,385,90,45); demoLRU.setBounds(60,385,90,45); processLabel.setBounds(320,10,500,60); fifoScrollPane.setBounds(15,30,180,320); fifoPanel.add(fifoScrollPane); fifoPanel.add(demoFIFO); lruScrollPane.setBounds(15,30,180,320); lruPanel.add(lruScrollPane); lruPanel.add(demoLRU); producePanel.add(produce); producePanel.add(processLabel); produce.addActionListener(this); demoFIFO.addActionListener(this); demoLRU.addActionListener(this); makememery.addActionListener(this); makeProcess.addActionListener(this); this.setBounds(300,50,750,600); this.setVisible(true); this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } publicvoidactionPerformed(ActionEvente) { if(e.getSource()==produce) { if(produce.getSelectedItem().toString()=="隨機(jī)頁面序列") { build(); fifoArea.setText(""); lruArea.setText(""); inputprocess.add(makePLabel); inputprocess.add(processField); inputprocess.add(makeProcess); inputprocess.updateUI(); } if(produce.getSelectedItem().toString()=="輸入頁面序列") { inputprocess.remove(makeProcess); inputprocess.remove(makePLabel); inputprocess.remove(processField); inputprocess.updateUI(); Stringstr= JOptionPane.showInputDialog(this,"請(qǐng)輸入頁面序列(格式為:1,2,3,...)"); if(str==null) { JOptionPane.showMessageDialog(this,"無效值"); return; } //System.out.println(str); Strings[]=str.split(","); ArrayList<Integer>list=newArrayList<Integer>(); try { for(inti=0;i<s.length;i++) list.add(Integer.parseInt(s[i])); }catch(NumberFormatExceptionf) { JOptionPane.showMessageDialog(this,"輸入數(shù)字格式不正確"); } process=newint[list.size()]; Stringstring=""; for(inti=0;i<process.length;i++) { string=string+list.get(i)+""; process[i]=list.get(i); } for(inti=0;i<process.length;i++) //System.out.println(""+process[i]); pSIZE =process.length; processLabel.setText(string); } } if(e.getSource()==demoFIFO) FIFO(); if(e.getSource()==demoLRU) LRU(); if(e.getSource()==makememery) { Strings1=memeryField.getText(); if(s1.trim().equals(""))
溫馨提示
- 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. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 口腔清潔劑制造工道德水平考核試卷含答案
- 測(cè)量與控制系統(tǒng)(單元)裝調(diào)工安全操作競(jìng)賽考核試卷含答案
- 中藥酒(酊)劑工保密評(píng)優(yōu)考核試卷含答案
- 石腦油吸附分離裝置操作工崗前誠(chéng)信考核試卷含答案
- 蔭罩制板工班組評(píng)比能力考核試卷含答案
- 煤層氣修井工班組建設(shè)能力考核試卷含答案
- 中藥酒(酊)劑工安全意識(shí)強(qiáng)化模擬考核試卷含答案
- 數(shù)據(jù)安全管理員QC管理能力考核試卷含答案
- 中式面點(diǎn)師崗前基礎(chǔ)實(shí)戰(zhàn)考核試卷含答案
- 機(jī)械手表裝配工道德知識(shí)考核試卷含答案
- 消毒供應(yīng)中心護(hù)士長(zhǎng)述職報(bào)告
- (12)普通高中技術(shù)與工程課程標(biāo)準(zhǔn)日常修訂版(2017年版2025年修訂)
- 標(biāo)準(zhǔn)廠房租賃合同范本模板
- QC知識(shí)測(cè)評(píng)考試試題(含答案)
- 大象版三年級(jí)上冊(cè)科學(xué)全冊(cè)教案(完整版)教學(xué)設(shè)計(jì)
- 香薰蠟燭基礎(chǔ)知識(shí)培訓(xùn)
- 網(wǎng)卡課件教學(xué)課件
- 制造可行性分析表
- 中國(guó)職業(yè)技術(shù)教育發(fā)展歷程
- 學(xué)前班兒童數(shù)學(xué)能力測(cè)評(píng)試題集
- 快餐配送外賣配送配送員招聘方案
評(píng)論
0/150
提交評(píng)論