![實(shí)驗四-模擬操作系統(tǒng)內(nèi)存管理_第1頁](http://file4.renrendoc.com/view10/M03/31/1B/wKhkGWWcivWAHZygAAHYK4jeeu4075.jpg)
![實(shí)驗四-模擬操作系統(tǒng)內(nèi)存管理_第2頁](http://file4.renrendoc.com/view10/M03/31/1B/wKhkGWWcivWAHZygAAHYK4jeeu40752.jpg)
![實(shí)驗四-模擬操作系統(tǒng)內(nèi)存管理_第3頁](http://file4.renrendoc.com/view10/M03/31/1B/wKhkGWWcivWAHZygAAHYK4jeeu40753.jpg)
![實(shí)驗四-模擬操作系統(tǒng)內(nèi)存管理_第4頁](http://file4.renrendoc.com/view10/M03/31/1B/wKhkGWWcivWAHZygAAHYK4jeeu40754.jpg)
![實(shí)驗四-模擬操作系統(tǒng)內(nèi)存管理_第5頁](http://file4.renrendoc.com/view10/M03/31/1B/wKhkGWWcivWAHZygAAHYK4jeeu40755.jpg)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
實(shí)驗四模擬操作系統(tǒng)內(nèi)存管理本程序可分配,撤消進(jìn)程,動態(tài)顯示內(nèi)存占用情況.運(yùn)行如圖示:代碼如下:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;/*主程序類*/publicclassMemoryextendsJFrameimplementsActionListener{//實(shí)現(xiàn)接口JFrameset_frame,fr;JButtonjbutton1,jbutton2,jbutton3,jbutton4,jbutton5,jbutton6;//聲明進(jìn)程JLabeljLabel1,jLabel2,jLabel3,jLabel4,thread_label1,thread_label2,lab1;//聲明內(nèi)存使用情況JTextPanejTextPane1,jTextPane2,thread_text;staticJTablejTable;JProgressBarjProgressBar;JPanelpel;finalString[]table_title={"分區(qū)號","大小〔K〕","起址〔K〕","狀態(tài)"};staticObject[][]table_data=newObject[15][4];staticintfree_size=1000,use_size=0,num=0,thread_num;//初始化內(nèi)存空間大小為1000,已用空間大小為0staticbooleanfind_flag=true;staticthreads[]thread_arry=newthreads[15];staticmemory_class[]memory_arry=newmemory_class[15];publicMemory(){super("實(shí)驗五:可變分區(qū)方式的存儲器管理");setSize(620,450);setLayout(null);setLocation(240,180);setResizable(false);jLabel1=newJLabel("內(nèi)存的使用情況列表:");add(jLabel1);jLabel1.setBounds(20,20,200,25);//創(chuàng)立橫坐標(biāo)為20,縱坐標(biāo)為20,長為200,寬為25的矩形。jTable=newJTable(table_data,table_title);jTable.setRowHeight(20);intv=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;inth=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;JScrollPanescrollPane=newJScrollPane(jTable,v,h);JPanelcp=newJPanel();cp.setLayout(newBorderLayout());add(cp);cp.add(scrollPane,BorderLayout.CENTER);cp.setBounds(20,50,370,220);Stringstr1,str2;str1=0+"K";str2=1000+"K";jLabel2=newJLabel("已用的內(nèi)存空間:");add(jLabel2);//將jLabel2加到集合中jLabel2.setBounds(440,20,200,25);//創(chuàng)立橫坐標(biāo)為440,縱坐標(biāo)為20,長為200,寬為25的矩形。jTextPane1=newJTextPane();add(jTextPane1);jTextPane1.setBounds(440,55,100,30);jTextPane1.setEditable(false);jTextPane1.setText(str1);jLabel3=newJLabel("可用的內(nèi)存空間:");add(jLabel3);jLabel3.setBounds(440,115,200,25);jTextPane2=newJTextPane();add(jTextPane2);jTextPane2.setBounds(440,150,100,30);jTextPane2.setEditable(false);jTextPane2.setText(str2);jLabel4=newJLabel("內(nèi)存總的使用情況:");add(jLabel4);jLabel4.setBounds(440,210,200,25);jProgressBar=newJProgressBar();add(jProgressBar);jProgressBar.setBackground(Color.white);//設(shè)置背景圖片,為白色jProgressBar.setMaximum(1000);//設(shè)置組件大小為1000jProgressBar.setStringPainted(true);//設(shè)置進(jìn)度條,進(jìn)度默認(rèn)用百分比表示。jProgressBar.setBounds(440,245,150,20);jbutton1=newJButton("創(chuàng)建進(jìn)程");add(jbutton1);jbutton1.addActionListener(this);//給jButton1添加一個事件偵聽器,當(dāng)jButton被按下的時候,所需要發(fā)生的事情定義在newPlusCaculator_btnCaculate_actionAdapter(this)里。jbutton1.setBounds(50,310,120,30);jbutton1.setCursor(newCursor(12));jbutton2=newJButton("撤消進(jìn)程");add(jbutton2);jbutton2.addActionListener(this);jbutton2.setBounds(250,310,120,30);jbutton2.setCursor(newCursor(12));jbutton3=newJButton("退出程序");add(jbutton3);jbutton3.addActionListener(this);jbutton3.setBounds(440,310,120,30);jbutton3.setCursor(newCursor(12));setVisible(true);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidactionPerformed(ActionEvente){if(e.getSource()==jbutton1){if(free_size>0){add_thread();if(thread_arry[num-1].thread_size<=free_size){memory_class.find_free_memory();if(find_flag==true){set_table();jTable.repaint();free_size=0;use_size=0;set_text();jProgressBar.setValue(use_size);}elseif(find_flag==false){notice("find");}}else{num--;notice("free");//去掉多余}}else{notice("mem");}}elseif(e.getSource()==jbutton2){destroy_thread_frame();}elseif(e.getSource()==jbutton3){System.exit(0);}elseif(e.getSource()==jbutton4){thread_num=Integer.parseInt(thread_text.getText());destroy_thread();set_frame.setVisible(false);}elseif(e.getSource()==jbutton5){set_frame.setVisible(false);}elseif(e.getSource()==jbutton6){fr.setVisible(false);}}/*用于添加進(jìn)程的方法*/publicvoidadd_thread(){thread_arry[num]=newthreads();memory_arry[num+1]=newmemory_class();num++;}/*設(shè)置文本框的值*/publicvoidset_text(){Stringstr1,str2;for(intj=0;j<num;j++){if(memory_arry[j].use_flag==true){use_size+=memory_arry[j].size;}}for(inti=0;i<=num;i++){if(memory_arry[i].use_flag==false){free_size+=memory_arry[i].size;}}str1=use_size+"K";str2=free_size+"K";jTextPane1.setText(str1);jTextPane2.setText(str2);}/*設(shè)置表格*/publicstaticvoidset_table(){for(intj=1;j<=num;j++){ints=0;for(intn=0;n<j;n++){s+=memory_arry[n].size;}memory_arry[j].start_address=s+1;}for(inti=0;i<=num;i++){table_data[i][0]=memory_arry[i].mem_num;table_data[i][1]=memory_arry[i].size;table_data[i][2]=memory_arry[i].start_address;if(Memory.memory_arry[i].use_flag==true){table_data[i][3]=memory_arry[i].thread_use+"號進(jìn)程占用";}elseif(memory_arry[i].use_flag==false){table_data[i][3]="可用";}jTable.repaint();}}/*撤消進(jìn)程的窗口*/publicvoiddestroy_thread_frame(){set_frame=newJFrame("撤消進(jìn)程");set_frame.setSize(300,150);set_frame.setLayout(null);set_frame.setLocation(370,330);set_frame.setResizable(false);thread_label1=newJLabel("請輸入想要撤消的進(jìn)程編號:");set_frame.add(thread_label1);thread_label1.setBounds(20,20,180,25);thread_text=newJTextPane();set_frame.add(thread_text);thread_text.setBounds(200,20,30,20);thread_label2=newJLabel("號進(jìn)程");set_frame.add(thread_label2);thread_label2.setBounds(230,20,150,25);jbutton4=newJButton("確定");set_frame.add(jbutton4);jbutton4.addActionListener(this);jbutton4.setBounds(50,70,80,30);jbutton4.setCursor(newCursor(12));jbutton5=newJButton("取消");set_frame.add(jbutton5);jbutton5.addActionListener(this);jbutton5.setBounds(160,70,80,30);jbutton5.setCursor(newCursor(12));set_frame.setVisible(true);}/*撤消進(jìn)程的方法*/publicvoiddestroy_thread(){for(intn=0;n<=num;n++){if(memory_arry[n].thread_use==thread_num){if(memory_arry[n-1].use_flag==false){/*如果前面分區(qū)為空,與前面的分區(qū)合并*/memory_arry[n-1].size+=memory_arry[n].size;intna=n;for(intx=n;x<num;x++){memory_arry[x]=memory_arry[x+1];memory_arry[x].mem_num=na;na++;}table_data[num][0]="";jTable.repaint();}elseif(memory_arry[n+1].use_flag==false){/*如果后面分區(qū)為空,與后面的分區(qū)合并*/memory_arry[n].size+=memory_arry[n+1].size;intna=n+1;for(intx=n+1;x<num;x++){memory_arry[x]=memory_arry[x+1];memory_arry[x].mem_num=na;na++;}}elseif(memory_arry[n+1].use_flag==false&&memory_arry[n-1].use_flag==false){/*如果前后的分區(qū)都為空,與前后的分區(qū)合并*/memory_arry[n-1].size=memory_arry[n-1].size+memory_arry[n].size+memory_arry[n+1].size;intna=n;for(intx=n;x<num;x++){memory_arry[x]=memory_arry[x+2];memory_arry[x].mem_num=na;na++;}}else{memory_arry[n].use_flag=false;}}}Memory.set_table();}/*錯誤提示信息窗口*/publicvoidnotice(Stringsuccess){load_imageimage_notice;fr=newJFrame("注意");fr.setSize(530,150);fr.setLayout(null);fr.setLocation(260,280);fr.setResizable(false);if(success=="mem"){lab1=newJLabel("系統(tǒng)內(nèi)存的容量已用完,請撒消某些進(jìn)程!");fr.add(lab1);lab1.setBounds(130,35,400,25);}elseif(success=="free"){lab1=newJLabel("系統(tǒng)內(nèi)存的容量缺乏,請撒消某些進(jìn)程!");fr.add(lab1);lab1.setBounds(130,35,400,25);}elseif(success=="find"){lab1=newJLabel("找不到適宜的空閑分區(qū),請撒消某些進(jìn)程!");fr.add(lab1);lab1.setBounds(130,35,400,25);}image_notice=newload_image("false");pel=newJPanel();pel.setLayout(newBorderLayout());pel.add(image_notice,BorderLayout.CENTER);fr.add(pel);pel.setBounds(15,10,100,100);jbutton6=newJButton("確定");fr.add(jbutton6);jbutton6.setBounds(410,35,90,30);jbutton6.addActionListener(this);jbutton3.setCursor(newCursor(12));fr.setVisible(true);}publicstaticvoidmain(Stringargs[]){Memorymain_obj=newMemory();memory_arry[0]=newmemory_class();memory_arry[0].set_start();table_data[0][0]=""+0;table_data[0][1]=""+1000;table_data[0][2]=""+0;table_data[0][3]=""+"可用";}}/*------------------------------------------------------------------------------------*//*進(jìn)程類*/classthreads{intthread_number,thread_size;booleanstate=false,run_flag=false;/*線程類的構(gòu)造函數(shù)*/publicthreads(){this.thread_number=Memory.num;this.thread_size=(int)(Math.random()*400);}}/*用于加載圖片的類*/classload_imageextendsCanvas{Toolkittool;Imagemyimage;publicload_image(Stringpicture){setSize(200,200);tool=getToolkit();myimage=tool.getImage("false.jpg");}publicvoidpaint(Graphicsg){g.drawImage(myimage,10,10,myimage.getWidth(this),myimage.getHeight(this),this);}}/*內(nèi)存類*/classmemory_class{intmem_num;intsize,start_address,thread_use;publicbooleanuse_flag=false;memory_class(){this.mem_num=Memory.num;}publicvoidset_start(){this.size=1000;this.start_address=0;this.mem_num=0;}/*查找適合某進(jìn)程的空閑分區(qū)*/publicstaticvoidfind_free_memory(){memory_classchange_obj=newmemory_class();for(inti=0;i<Memory.num;i++){if(Memory.memory_arry[i].size<Memory.thread_arry[Memory.num-1].thread_size&&Memory.memory_arry[i].use_flag==false){Memory.find_flag=false;}elseif(Memory.memory_arry[i].size==Memory.thread_arry[Memory.num-1].thread_size&&Memory.memory_arry[i].use_flag==false){Memory.memory_arry[i].use_flag=true;Memory.memory_arry[i].thread_use=Memory.thread_arry[Memory.num-1].thread_number;
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《Dreamweaver CS5網(wǎng)頁設(shè)計與制作實(shí)例教程》課件-第1章 概述
- 2025年全球及中國應(yīng)急響應(yīng)無人機(jī)行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國用于光學(xué)應(yīng)用的超透鏡行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國單相柵極驅(qū)動器IC行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國臺式激光二極管驅(qū)動儀行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球高山輸送機(jī)行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國柵網(wǎng)型離子源行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球大麻含量分析儀行業(yè)調(diào)研及趨勢分析報告
- 2025-2030全球藥品和食品防偽技術(shù)行業(yè)調(diào)研及趨勢分析報告
- 2025-2030全球立式高溫反應(yīng)釜行業(yè)調(diào)研及趨勢分析報告
- 江蘇省蘇州市2024-2025學(xué)年高三上學(xué)期1月期末生物試題(有答案)
- 銷售與銷售目標(biāo)管理制度
- 特殊教育學(xué)校2024-2025學(xué)年度第二學(xué)期教學(xué)工作計劃
- 2025年第一次工地開工會議主要議程開工大吉模板
- 第16課抗日戰(zhàn)爭課件-人教版高中歷史必修一
- 對口升學(xué)語文模擬試卷(9)-江西省(解析版)
- 糖尿病高滲昏迷指南
- 【公開課】同一直線上二力的合成+課件+2024-2025學(xué)年+人教版(2024)初中物理八年級下冊+
- 南京信息工程大學(xué)《教師領(lǐng)導(dǎo)力》2021-2022學(xué)年第一學(xué)期期末試卷
- 信息科技大單元教學(xué)設(shè)計之七年級第三單元便捷的互聯(lián)網(wǎng)服務(wù)
- (正式版)HGT 22820-2024 化工安全儀表系統(tǒng)工程設(shè)計規(guī)范
評論
0/150
提交評論