《2022年操作系统实验进程调度和内存管理java语言版本》由会员分享,可在线阅读,更多相关《2022年操作系统实验进程调度和内存管理java语言版本(24页珍藏版)》请在金锄头文库上搜索。
1、源代码:第一个类: divDTO public class divDTO private int divBase; private int length; private int divFlag; public divDTO(int divBase,int length,int divFlag) this.divBase = divBase; this.divFlag = divFlag; this.length = length; public divDTO() public void setDivBase(int base) this.divBase = base; public int
2、getDivBase() return this.divBase; public void setLength(int length) this.length = length; public int getLength() return this.length; public void setDivFlag(int flag) this.divFlag = flag; public int getDivFalg() return this.divFlag; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - -
3、- - - - - 第 1 页,共 24 页 - - - - - - - - - 2.第二个类: PcbDTO public class PcbDTO static final int Running = 1; static final int Ready = 2; static final int Waiting = 3; private String processName; private int runTime; private int prority; private int processState; private int base; private int limit; pri
4、vate int pcbFlag; public PcbDTO(String name, int time,int pro,int base,int limit) this.processName = name; this.runTime = time; this.prority = pro; this.processState = 0; this.limit = limit; this.base = base; public PcbDTO() this.pcbFlag = 0; public void setProcessName(String name) this.processName
5、= name; public String getProcessName() return processName; public void setRunTime(int time) this.runTime = time; public int getRunTime() return this.runTime; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 24 页 - - - - - - - - - public void setPrority(int prorit
6、y) this.prority = prority; public int getPrority() return this.prority; public void setProcessState(int state) this.processState = state; public String getProcessState() String s = new String(); if(this.processState = 1) s = running; else if(this.processState = 2) s = ready; else if(this.processStat
7、e = 3) s = waiting; return s; public int getBase() return this.base; public void setBase(int base) this.base = base; public void setLimit(int limit) this.limit = limit; public int getLimit() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 24 页 - - - - - - - - -
8、return this.limit; 第三个类:主类import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; public class MainFrame private JList readyList; private JList waitingList; private JList jobList; private JButton susButton; private JButton relaxButton; privat
9、e JButton startButton; private JButton newButton; private JLabel nameLabel; private JLabel prorityLabel; private JLabel timeLabel; private JLabel jobLabel; private JLabel readyLabel; private JLabel waitingLabel; private JLabel runningLabel; private JLabel spaceLabel; private JLabel divLabel; private
10、 JLabel allocLabel; private JTable readyTable; private JTable runningTable; private JTable divTable; private JTable allocTable; private JTextField nameText; private JTextField timeText; private JTextField spaceText; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,
11、共 24 页 - - - - - - - - - private JComboBox prorityCom; private JPanel newPanel; private JPanel waitingPanel; private JPanel readyPanel; Vector jobVectorName; Vector jobDtoVector; Vector waitingVectorName; Vector waitingDtoVector; PcbDTO readyDtoArray; PcbDTO newDtoArray; divDTO divDtoArray; PcbDTO n
12、ewSort; Object readydata; Object runningdata; Object divdata; Object allocdata; int first; int end; int point; PcbDTO a ; public MainFrame() a = new PcbDTO(); first = 0; end = 0; point = 0; JFrame jf = new JFrame(进程调度 -ws); Container c = jf.getContentPane(); c.setLayout(null); /c.setBackground(Color
13、.pink); newPanel = new JPanel(); newPanel.setLayout(null); waitingPanel = new JPanel(); waitingPanel.setLayout(null); /waitingPanel.setBackground(Color.pink);readyPanel = new JPanel(); readyPanel.setLayout(null); susButton = new JButton( 挂起 ); relaxButton = new JButton( 释放 ); 名师资料总结 - - -精品资料欢迎下载 -
14、- - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 24 页 - - - - - - - - - startButton = new JButton( 开始 ); newButton = new JButton( 新建进程 ); nameLabel = new JLabel(进程名 ); prorityLabel = new JLabel(优先级 ); timeLabel = new JLabel(运行时间 ); jobLabel = new JLabel(后备队列 ); readyLabel = new JLabel(
15、就绪队列 ); waitingLabel = new JLabel(等待队列 ); runningLabel = new JLabel(运行进程 ); spaceLabel = new JLabel(需要空间 ); divLabel = new JLabel(未分分区表 ); allocLabel = new JLabel(内存分配表 ); nameText = new JTextField(); timeText = new JTextField(); spaceText = new JTextField(); prorityCom = new JComboBox(); prorityCom
16、.setToolTipText( 优先级 ); readyDtoArray = new PcbDTO6; newSort = new PcbDTO6; for(int i = 0; i 6;i+) newSorti = new PcbDTO(); newDtoArray = new PcbDTO100; jobDtoVector = new Vector(); jobVectorName = new Vector(); waitingDtoVector = new Vector(); waitingVectorName = new Vector(); divDtoArray = new div
17、DTO20; for(int i = 0; i 20; i+) divDtoArrayi = new divDTO(); divDtoArrayi.setDivFlag(0); divDtoArray0.setDivFlag(1); divDtoArray0.setDivBase(20); divDtoArray0.setLength(180); readydata = new Object64; runningdata = new Object23; divdata = new Object203; allocdata = new Object203; 名师资料总结 - - -精品资料欢迎下
18、载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 24 页 - - - - - - - - - String col1 = 进程 , 时间 , 优先级 ,状态 ; String col2 = 进程 , 时间 , 优先级 ; String col3 = 起址 , 长度 , 状态 ; String col4 = 起址 , 长度 , 占用进程 ; readyTable = new JTable(readydata,col1); /readyTable.setEnabled(false); runningTable =
19、 new JTable(runningdata,col2); runningTable.setRowHeight(22); runningTable.setEnabled(false); allocTable = new JTable(allocdata,col4); allocTable.setEnabled(false); divTable = new JTable(divdata,col3); divTable.setEnabled(false); divTable.setValueAt(String.valueOf(20),0,0);divTable.setValueAt(String
20、.valueOf(180),0,1);divTable.setValueAt(String.valueOf(1),0,2); JScrollPane runningSP = new JScrollPane(); JScrollPane readySP2 = new JScrollPane();JScrollPane divSP = new JScrollPane(); JScrollPane allocSP = new JScrollPane(); runningSP .getViewport().add(runningTable);readySP2.getViewport().add(rea
21、dyTable);divSP .getViewport().add(divTable); allocSP .getViewport().add(allocTable);/int prorityArray = new int10; for(int i = 0;i readyDtoArraymax.getPrority() max = j; j = (j+1)%6; if(max=0) a = readyDtoArraymax; readyDtoArraymax = readyDtoArrayfirst; readyDtoArrayfirst = a; readyTable.setValueAt(
22、readyDtoArraymax.getProcessName(),max,0); readyTable.setValueAt(readyDtoArraymax.getRunTime(),max,1); readyTable.setValueAt(readyDtoArraymax.getPrority(),max,2); readyTable.setValueAt(readyDtoArraymax.getProcessState(),max,3); readyTable.setValueAt(,first,0); readyTable.setValueAt(,first,1); 名师资料总结
23、- - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 24 页 - - - - - - - - - readyTable.setValueAt(,first,2); readyTable.setValueAt(,first,3); runningTable.setValueAt(a.getProcessName(),0,0); runningTable.setValueAt(a.getRunTime(),0,1); runningTable.setValueAt(a.getPrority(
24、),0,2); readyDtoArrayfirst.setRunTime(readyDtoArrayfirst.getRunTime()-1); if(0 != readyDtoArrayfirst.getPrority() readyDtoArrayfirst.setPrority(readyDtoArrayfirst.getPrority()-1); first = (first+1)%6; else System.out.println(cpu等待中 ); else /*try Thread.sleep(2000); catch(InterruptedException e1) Sys
25、tem.out.println(e1); */ /System.out.println(到 1); runningTable.setValueAt(,0,0); runningTable.setValueAt(,0,1); runningTable.setValueAt(,0,2); / 如果运行时间为0 则撤销进程, 否则将进程重新添加到就绪队列中if( a.getRunTime()=0) / 收回内存空间for(int i = 0;i =a.getBase() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 -
26、 - - - - - - 第 12 页,共 24 页 - - - - - - - - - newSorti = newSorti+1; point-; / 设置内存分配表的内容for(int i = 0; i point;i+) allocTable.setValueAt(String.valueOf(newSorti.getBase(),i,0); allocTable.setValueAt(String.valueOf(newSorti.getLimit(),i,1); allocTable.setValueAt(newSorti.getProcessName(),i,2); allocT
27、able.setValueAt(,point,0); allocTable.setValueAt(,point,1); allocTable.setValueAt(,point,2); / 把收回的内存加入到记录未分分区的数组int memoryEnd = 0; int location = 0; int up = -1;/ int down = -1; for(int i = 0;i= 0&down = 0) divDtoArrayup.setLength(divDtoArrayup.getLength()+a.getLimit()+divDtoArraydown.getLength();
28、名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 24 页 - - - - - - - - - divDtoArraydown.setDivFlag(0); for(int i = (down+1); i = 0&down 0) divDtoArrayup.setLength(divDtoArrayup.getLength()+a.getLimit(); else if(up = 0) divDtoArraydown.setLength(divDtoArraydown.g
29、etLength()+a.getLimit(); divDtoArraydown.setDivBase(a.getBase(); else if(up 0&down 0) for(int i = 0; i a.getBase()|divDtoArrayi.getDivFalg()=0) location = i; break; for(int i = 20; i location;i-) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 24 页 - - - - - -
30、- - - if(divDtoArrayi-1.getDivFalg()=1) divDtoArrayi.setDivBase(divDtoArrayi-1.getDivBase(); divDtoArrayi.setDivFlag(1); divDtoArrayi.setLength(divDtoArrayi-1.getLength(); divDtoArraylocation.setDivBase(a.getBase(); divDtoArraylocation.setDivFlag(1); divDtoArraylocation.setLength(a.getLimit(); / 设置未
31、分分区表的内容for(int i = 0; i 20;i+) if(divDtoArrayi.getDivFalg()=1) divTable.setValueAt(String.valueOf(divDtoArrayi.getDivBase(),i,0); divTable.setValueAt(String.valueOf(divDtoArrayi.getLength(),i,1); divTable.setValueAt(String.valueOf(divDtoArrayi.getDivFalg(),i,2); if(!jobDtoVector.isEmpty() int runLen
32、gth = 0; PcbDTO jobToReady = (PcbDTO)jobDtoVector.elementAt(0); for(int i = 0; i = jobToReady.getLimit() runAllocFlag = i; break; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 24 页 - - - - - - - - - if(runAllocFlag = 0) jobDtoVector.removeElementAt(0); jobVec
33、torName.remove(jobVectorName.indexOf(jobToReady.getProcessName(); jobList.setListData(jobVectorName);jobToReady.setProcessState(PcbDTO.Ready); jobToReady.setBase(divDtoArrayrunAllocFlag.getDivBase(); runLength = divDtoArrayrunAllocFlag.getLength()- jobToReady.getLimit(); if(runLength = 0) int i = ru
34、nAllocFlag; divDtoArrayi.setDivFlag(0); for(; i 0) int c2 = divDtoArrayrunAllocFlag.getDivBase()+ jobToReady.getLimit(); divDtoArrayrunAllocFlag.setDivBase(c2);divDtoArrayrunAllocFlag.setLength(runLength);divTable.setValueAt(String.valueOf(c2),runAllocFlag,0); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - -
35、- - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 24 页 - - - - - - - - - divTable.setValueAt(String.valueOf(runLength),runAllocFlag,1); divTable.setValueAt(String.valueOf(divDtoArrayrunAllocFlag.getDivFalg(),runAllocFlag,2); readyDtoArrayend = jobToReady; readyTable.setValueAt(jobToReady.getProcessNam
36、e(),end,0); readyTable.setValueAt(jobToReady.getRunTime(),end,1);readyTable.setValueAt(jobToReady.getPrority(),end,2); readyTable.setValueAt(jobToReady.getProcessState(),end,3); end = (end+1)%6; int runi = 0;/ 用于记录当前新生成的PcbDTO对象应该插入到newSort 中的位置for(; runi point; runi+) if(jobToReady.getBase() runi;
37、i-) newSorti = newSorti-1; allocTable.setValueAt(String.valueOf(newSorti.getBase(),i,0); allocTable.setValueAt(String.valueOf(newSorti.getLimit(),i,1); allocTable.setValueAt(newSorti.getProcessName(),i,2); / 插入新生成的对象newSortruni = jobToReady; allocTable.setValueAt(String.valueOf(jobToReady.getBase(),
38、runi,0); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 24 页 - - - - - - - - - allocTable.setValueAt(String.valueOf(jobToReady.getLimit(),runi,1); allocTable.setValueAt(jobToReady.getProcessName(),runi,2);point+; else readyDtoArrayend = a; readyTable.setValueA
39、t(a.getProcessName(),end,0);readyTable.setValueAt(a.getRunTime(),end,1); readyTable.setValueAt(a.getPrority(),end,2); readyTable.setValueAt(a.getProcessState(),end,3); end = (end+1)%6; / else if(jb = newButton) int newAllocFlag = -1; int newLength = 0; if(nameText.getText().trim().length() = 0) JOpt
40、ionPane.showMessageDialog(null, 进程名不能为空!); else if(timeText.getText().trim().length() = 0) JOptionPane.showMessageDialog(null, 运行时间不能为空); else if(spaceText.getText().trim().length() = 0) JOptionPane.showMessageDialog(null, 空间不能为空); else test.setRunTime(Integer.parseInt(timeText.getText(); test.setLi
41、mit(Integer .parseInt(spaceText.getText(); String s = prorityCom.getSelectedItem().toString(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 24 页 - - - - - - - - - test.setPrority(Integer.parseInt(s); test.setProcessName(nameText.getText().trim(); newDtoArray
42、count = test; jobDtoVector.add(newDtoArraycount); jobVectorName.add(newDtoArraycount.getProcessName();jobList.setListData(jobVectorName);count+; nameText.setText(); timeText.setText(); spaceText.setText(); PcbDTO b = (PcbDTO)jobDtoVector.elementAt(0); for(int i = 0; i = b.getLimit() newAllocFlag = i
43、; break; / 在就绪队列未满且内存有足够空间时将后备队列jobDtoVetor 中的对象添加到就绪队列中if(end + 2)%6 != first&newAllocFlag = 0) jobDtoVector.removeElementAt(0); b.setProcessState(PcbDTO.Ready); b.setBase(divDtoArraynewAllocFlag.getDivBase(); newLength = divDtoArraynewAllocFlag.getLength()- b.getLimit(); if(newLength = 0) int i =
44、newAllocFlag; divDtoArrayi.setDivFlag(0); for(; i 0) int c1 = divDtoArraynewAllocFlag.getDivBase()+ b.getLimit(); divDtoArraynewAllocFlag.setDivBase(c1); divDtoArraynewAllocFlag.setLength(newLength); divTable.setValueAt(String.valueOf(c1),newAllocFlag,0);divTable.setValueAt(String.valueOf(newLength)
45、,newAllocFlag,1); divTable.setValueAt(String.valueOf(divDtoArraynewAllocFlag.getDivFalg(),newAllocFlag,2); readyDtoArrayend = b; jobVectorName.remove(jobVectorName.indexOf(b.getProcessName(); readyTable.setValueAt(b.getProcessName(),end,0); readyTable.setValueAt(b.getRunTime(),end,1);readyTable.setV
46、alueAt(b.getPrority(),end,2); readyTable.setValueAt(ready,end,3); end =(end+1)%6; int newi = 0;/ 用于记录当前新生成的PcbDTO 对象应该插入到newSort 中的位置for(; newi point; newi+) if(b.getBase() newi; i-) newSorti = newSorti-1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 24 页 -
47、- - - - - - - - allocTable.setValueAt(String.valueOf(newSorti.getBase(),i,0); allocTable.setValueAt(String.valueOf(newSorti.getLimit(),i,1); allocTable.setValueAt(newSorti.getProcessName(),i,2); / 插入新生成的对象newSortnewi = b; allocTable.setValueAt(String.valueOf(b.getBase(),newi,0); allocTable.setValueA
48、t(String.valueOf(b.getLimit(),newi,1); allocTable.setValueAt(b.getProcessName(),newi,2); point+; else if(jb = susButton) if(readyDtoArrayreadyTable.getSelectedRow() != null) if(!readyDtoArrayreadyTable.getSelectedRow().getProcessState().equals(waiting) readyDtoArrayreadyTable.getSelectedRow().setPro
49、cessState(PcbDTO.Waiting); readyTable.setValueAt(waiting,readyTable.getSelectedRow(),3); waitingDtoVector .add(readyDtoArrayreadyTable.getSelectedRow(); waitingVectorName.add(readyDtoArrayreadyTable.getSelectedRow().getProcessName();waitingList.setListData(waitingVectorName); else System.out.println
50、( 已挂起 ); else JOptionPane.showMessageDialog(null, 请选择要挂起的进程); /System.out.println(请选择要挂起的进程); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 21 页,共 24 页 - - - - - - - - - else if(jb = relaxButton) String s = (String)waitingList.getSelectedValue(); if(s != null) waiti
51、ngVectorName.remove(s); PcbDTO p = new PcbDTO(); for(int i = 0; i waitingDtoVector.size(); i+) p = (PcbDTO)waitingDtoVector.elementAt(i); if(s.equals(p.getProcessName() p.setProcessState(PcbDTO.Ready); waitingDtoVector .remove(p); break; for(int i = 0;i 6;i+) if(s.equals(readyDtoArrayi.getProcessNam
52、e() readyTable.setValueAt(ready,i,3); break; waitingList.setListData(waitingVectorName); else JOptionPane.showMessageDialog(null, 请选择要解挂的进程); /System.out.println(没有选择的进程); public static void main(String args) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 22 页,共 24 页
53、 - - - - - - - - - new MainFrame(); 相关截图如下:运行后开始界面输入进程后界面建立后挂起挂起后各内存分配名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 23 页,共 24 页 - - - - - - - - - 解挂操作异常时的提示名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 24 页,共 24 页 - - - - - - - - -