当前位置:文档之家› 操作系统课程实验报告(完整版)

操作系统课程实验报告(完整版)

. .中南大学《操作系统》实验报告姓名:福星专业班级:软件 1006班学号:完成日期: 2011.11.22进程调度与存管理一、实验目的在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。

当就续进程个数大于处理器数时,就必须依照某种策略来决定哪些进程优先占用处理器。

实验模拟实现处理机调度,以加深了解处理机调度的工作,并体会优先级和时间片轮转调度算法的具体实施方法。

帮助了解在不同的存储管理方式下,应怎样实现主存空间的分配和回收。

二、实验要求1、可随机输入若干进程,并按优先权排序;2、从就绪队首选进程运行:优先权-1/要求运行时间-1要求运行时间=0时,撤销该进程3、重新排序,进行下轮调度。

4、可随时增加进程;5、规定道数,设置后备队列和挂起状态。

若存中进程少于规定道数,可自动从后备队列调度一作业进入。

被挂起进程入挂起队列,设置解挂功能用于将指定挂起进程解挂入就绪队列。

6、每次调度后,显示各进程状态。

7、自行假设主存空间大小,预设操作系统所占大小并构造未分分区表;表目容:起址、长度、状态(未分/空表目)8、结合以上实验,PCB增加为:{PID,要求运行时间,优先权,状态,所需主存大小,主存起始位置,PCB指针}9、采用最先适应算法分配主存空间;10、进程完成后,回收主存,并与相邻空闲分区合并。

11、采用图形界面;三、实验容选择一个调度算法,实现处理机调度。

1、设计一个按优先权调度算法实现处理机调度的程序;2、设计按时间片轮转实现处理机调度的程序。

3、主存储器空间的分配和回收。

在可变分区管理方式下,采用最先适应算法实现主存空间的分配和回收。

四、实验原理该模拟系统采用java语言实现,要实现的功能有新建进程、进程调度、挂起进程、解挂进程、删除进程,道数和时间片大小可以由用户自己调整,有两种调度策略:按优先权调度和按时间片轮转调度。

每个进程可能有5种状态:新建(new)、就绪(ready)、运行(running)、阻塞(waiting)、挂起(suspend)。

每个状态都有一个队列用来存放处于该状态的进程,不同的调度策略采用不同的队列实现。

当创建进程时,如果存中的进程数还没达到规定道数,则将新建进程插入就绪队列,如果存中进程数已经达到规定道数,则插到后备队列,后备队列中的进程的状态为new。

CPU每次调度时都从就绪队列中取进程,在进程执行过程中如果下一个操作时IO操作,则将进程插入到waiting队列。

在系统运行过程中可以执行进程挂起操作,但执行的挂起操作时系统自动暂停运行,在弹出窗口选择要挂起的进程后,将选中的进程从原来的队列中删除并插入到挂起队列。

进行解挂操作时将选中的进程从挂起队列中删除并插入该进程原来所处的队列。

➢按优先级调度:当选择按优先权调度时,所有队列都采用优先队列,优先队列采用一个有序链表实现,进程的优先权值越大代表优先级越高,优先队列中的进程按优先权从大到小排列,当新进程插入时根据该进程的优先权插入到队列中的合适位置,插入后保持队列按优先权从大到小排列,如果新进程与队列中某个进程优先权值相等,则该新进程插到那个进程后面,以遵循先来先服务的规则。

当要从队列中取出进程时总是取队列中第一个进程,因为该进程的优先级最高。

➢按时间片轮转调度:当选择按时间片轮转调度时,所有队列都采用先进先出队列,先进先出队列采用一个普通单向链表实现,当新进程插入时插入到队列的末尾,当要取进程时取队首进程,这样就实现了先进先出。

➢存管理该实验基于实验一完成,核心是存的分配和回收,在实验一的基础上增加存管理部分,在新建进程的时候增加一个输入存大小的输入框,在进程进入存时要分配存,在进程销毁时要回收存,如果进入存时存不足,则将进程插入到后备队列等待下次调度。

系统维护一个存表,每个表项代表一个空间,每个空间保存了该空间的起始地址和空间大小以及空间使用状态。

初始时只有一个空间,当CPU启动时要分配存,存分配采用最先适应算法。

回收存时如果有相邻空闲空间,则要进行空闲空间合并。

}}五、源代码及截图: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 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;}}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;private 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 = name;}public String getProcessName(){return processName;}public void setRunTime(int time){this.runTime = time;}public int getRunTime(){return this.runTime;}public void setPrority(int prority) {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.processState == 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(){return this.limit;}}3.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;private 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 JLabel allocLabel;private JTable readyTable;private JTable runningTable;private JTable divTable;private JTable allocTable;private JTextField nameText;private JTextField timeText;private JTextField spaceText;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[] newSort;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.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("释放"); startButton = new JButton("开始"); newButton = new JButton("新建进程");nameLabel = new JLabel("进程名"); prorityLabel = new JLabel("优先级"); timeLabel = new JLabel("运行时间"); jobLabel = new JLabel("后备队列"); readyLabel = new JLabel("就绪队列"); 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.setToolTipText("优先级");readyDtoArray = new PcbDTO[6];newSort = new PcbDTO[6];for (int i = 0; i < 6; i++) {newSort[i] = new PcbDTO();}newDtoArray = new PcbDTO[100]; jobDtoVector = new Vector(); jobVectorName = new Vector(); waitingDtoVector = new Vector(); waitingVectorName = new Vector(); divDtoArray = new divDTO[20];for (int i = 0; i < 20; i++) {divDtoArray[i] = new divDTO();divDtoArray[i].setDivFlag(0);}divDtoArray[0].setDivFlag(1);divDtoArray[0].setDivBase(20);divDtoArray[0].setLength(180);readydata = new Object[6][4];runningdata = new Object[2][3];divdata = new Object[20][3];allocdata = new Object[20][3];String[] col1 = { "进程", "时间", "优先级", "状态" }; String[] col2 = { "进程", "时间", "优先级" };String[] col3 = { "起址", "长度", "状态" };String[] col4 = { "起址", "长度", "占用进程" }; readyTable = new JTable(readydata, col1);// readyTable.setEnabled(false);runningTable = 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.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(readyTable);divSP.getViewport().add(divTable);allocSP.getViewport().add(allocTable);// int []prorityArray = new int[10];for (int i = 0; i < 10; i++) {prorityCom.addItem(i);// prorityArray[i] = i;}jobList = new JList();waitingList = new JList();JScrollPane readySP = new JScrollPane(readyList); JScrollPane jobSP = new JScrollPane(jobList); JScrollPane waitingSP = new JScrollPane(waitingList);newPanel.setSize(450, 100); newPanel.setLocation(0, 0);nameLabel.setSize(80, 20); nameLabel.setLocation(10, 5); nameText.setSize(100, 25); nameText.setLocation(10, 30); prorityLabel.setSize(80, 20); prorityLabel.setLocation(120, 5); prorityCom.setSize(100, 25); prorityCom.setLocation(120, 30); timeLabel.setSize(80, 20); timeLabel.setLocation(230, 5); timeText.setSize(100, 25); timeText.setLocation(230, 30); spaceLabel.setSize(80, 20); spaceLabel.setLocation(340, 5); spaceText.setSize(100, 25); spaceText.setLocation(340, 30); newButton.setSize(100, 20); newButton.setLocation(320, 70);waitingPanel.setSize(190, 410); waitingPanel.setLocation(0, 100);jobLabel.setSize(100, 20); jobLabel.setLocation(10, 2); jobSP.setSize(180, 105);jobSP.setLocation(10, 25); waitingLabel.setSize(100, 20); waitingLabel.setLocation(10, 129); waitingSP.setSize(180, 105); waitingSP.setLocation(10, 150); divLabel.setSize(100, 20); divLabel.setLocation(10, 253); divSP.setSize(180, 113);divSP.setLocation(10, 273); relaxButton.setSize(80, 20); relaxButton.setLocation(110, 388);readyPanel.setSize(260, 410); readyPanel.setLocation(190, 100);readyLabel.setSize(100, 22);readyLabel.setLocation(10, 2); allocLabel.setSize(100, 20); allocLabel.setLocation(10, 232); startButton.setSize(80, 20); startButton.setLocation(177, 388); susButton.setSize(80, 20); susButton.setLocation(95, 388); readySP2.setSize(250, 117); readySP2.setLocation(10, 25); runningLabel.setLocation(10, 142); runningLabel.setSize(100, 20); runningSP.setSize(250, 65); runningSP.setLocation(10, 167); allocSP.setSize(250, 130); allocSP.setLocation(10, 255);c.add(newPanel);newPanel.add(nameLabel); newPanel.add(nameText); newPanel.add(prorityLabel); newPanel.add(prorityCom); newPanel.add(timeText); newPanel.add(timeLabel); newPanel.add(newButton); newPanel.add(spaceLabel); newPanel.add(spaceText);c.add(waitingPanel); waitingPanel.add(jobLabel); waitingPanel.add(jobSP); waitingPanel.add(waitingLabel); waitingPanel.add(waitingSP); waitingPanel.add(divLabel); waitingPanel.add(divSP); waitingPanel.add(relaxButton);c.add(readyPanel);readyPanel.add(readyLabel); readyPanel.add(allocLabel); readyPanel.add(runningLabel); readyPanel.add(startButton); readyPanel.add(susButton); readyPanel.add(allocSP); readyPanel.add(runningSP);readyPanel.add(readySP2);jf.setSize(470, 550);jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);jf.setLocationRelativeTo(null);jf.setVisible(true);startButton.addActionListener(new MyActionListener());newButton.addActionListener(new MyActionListener());susButton.addActionListener(new MyActionListener());relaxButton.addActionListener(new MyActionListener());}public void sus() {try {Thread.sleep(1000);} catch (Exception ex) {}}class MyActionListener implements ActionListener {public void actionPerformed(ActionEvent e) {int count = 0;PcbDTO test = new PcbDTO();JButton jb = (JButton) e.getSource();int max = -1;if (jb == startButton) {// while(true)// {int runAllocFlag = -1;if ((String) runningTable.getValueAt(0, 0) == null|| (String) runningTable.getValueAt(0, 0) == "") { try {Thread.sleep(0);} catch (Exception ex) {}// System.out.println("到3");for (int j = first; j != end;) {if (!readyDtoArray[j].getProcessState().equals("waiting")) {max = j;break;}j = (j + 1) % 6;}for (int j = first; j % 6 != end;) {if (!readyDtoArray[j].getProcessState().equals("waiting")) {if (readyDtoArray[j].getPrority() > readyDtoArray[max].getPrority()) {max = j;}}j = (j + 1) % 6;}if (max >= 0) {a = readyDtoArray[max];readyDtoArray[max] = readyDtoArray[first];readyDtoArray[first] = a;readyTable.setValueAt(readyDtoArray[max].getProcessName(), max, 0);readyTable.setValueAt(readyDtoArray[max].getRunTime(),max, 1);readyTable.setValueAt(readyDtoArray[max].getPrority(),max, 2);readyTable.setValueAt(readyDtoArray[max].getProcessState(), max, 3);readyTable.setValueAt("", first, 0);readyTable.setValueAt("", first, 1);readyTable.setValueAt("", first, 2);readyTable.setValueAt("", first, 3);runningTable.setValueAt(a.getProcessName(), 0, 0);runningTable.setValueAt(a.getRunTime(), 0, 1);runningTable.setValueAt(a.getPrority(), 0, 2);readyDtoArray[first].setRunTime(readyDtoArray[first].getRunTime() - 1);if (0 != readyDtoArray[first].getPrority()) {readyDtoArray[first].setPrority(readyDtoArray[first].getPrority() - 1);}first = (first + 1) % 6;} else {System.out.println("cpu等待中……");}} else {/** try { Thread.sleep(2000); } catch(InterruptedException* e1) { System.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 < point; i++) {if (newSort[i].getBase() >= a.getBase()) {newSort[i] = newSort[i + 1];}}point--;// 设置存分配表的容for (int i = 0; i < point; i++) {allocTable.setValueAt(String.valueOf(newSort[i].getBase()), i, 0);allocTable.setValueAt(String.valueOf(newSort[i].getLimit()), i, 1);allocTable.setValueAt(newSort[i].getProcessName(),i, 2);}allocTable.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 < 20; i++) {if (divDtoArray[i].getDivFalg() == 1) {memoryEnd = divDtoArray[i].getDivBase()+ divDtoArray[i].getLength();if (memoryEnd == a.getBase()) {up = i;}if (divDtoArray[i].getDivBase() == (a.getBase() + a.getLimit())) {down = i;}}}if (up >= 0 && down >= 0) {divDtoArray[up].setLength((divDtoArray[up].getLength()+ a.getLimit() + divDtoArray[down].getLength()));divDtoArray[down].setDivFlag(0);for (int i = (down + 1); i < 20; i++) {if (divDtoArray[i].getDivFalg() == 1) {divDtoArray[i - 1].setDivBase(divDtoArray[i].getDivBase());divDtoArray[i - 1].setDivFlag(1);divDtoArray[i - 1].setLength(divDtoArray[i].getLength());divDtoArray[i].setDivFlag(0);} else {divTable.setValueAt("", i - 1, 0);divTable.setValueAt("", i - 1, 1);divTable.setValueAt("", i - 1, 2);break;}}} else if (up >= 0 && down < 0) {divDtoArray[up].setLength((divDtoArray[up].getLength() + a.getLimit()));} else if (up < 0 && down >= 0) {divDtoArray[down].setLength((divDtoArray[down].getLength() + a.getLimit()));divDtoArray[down].setDivBase(a.getBase());} else if (up < 0 && down < 0) {for (int i = 0; i < 20; i++) {if (divDtoArray[i].getDivBase() > a.getBase()|| divDtoArray[i].getDivFalg() == 0) {location = i;break;}}for (int i = 20; i > location; i--) {if (divDtoArray[i - 1].getDivFalg() == 1) {divDtoArray[i].setDivBase(divDtoArray[i - 1].getDivBase());divDtoArray[i].setDivFlag(1);divDtoArray[i].setLength(divDtoArray[i - 1].getLength());}}divDtoArray[location].setDivBase(a.getBase());divDtoArray[location].setDivFlag(1);divDtoArray[location].setLength(a.getLimit());}// 设置未分分区表的容for (int i = 0; i < 20; i++) {if (divDtoArray[i].getDivFalg() == 1) {divTable.setValueAt(String.valueOf(divDtoArray[i].getDivBase()),i, 0);divTable.setValueAt(String.valueOf(divDtoArray[i].getLength()),i, 1);divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i, 2);}}if (!jobDtoVector.isEmpty()) {int runLength = 0;PcbDTO jobToReady = (PcbDTO) jobDtoVector.elementAt(0);for (int i = 0; i < 20; i++) {if (divDtoArray[i].getDivFalg() == 1) {if (divDtoArray[i].getLength() >= jobToReady.getLimit()) {runAllocFlag = i;break;}}}if (runAllocFlag >= 0) {jobDtoVector.removeElementAt(0);jobVectorName.remove(jobVectorName.indexOf(jobToReady.getProcessName()));jobList.setListData(jobVectorName);jobToReady.setProcessState(PcbDTO.Ready);jobToReady.setBase(divDtoArray[runAllocFlag].getDivBase());runLength = divDtoArray[runAllocFlag].getLength() - jobToReady.getLimit();if (runLength == 0) {int i = runAllocFlag;divDtoArray[i].setDivFlag(0);for (; i < 19; i++) {if (divDtoArray[i + 1].getDivFalg() == 1) {divDtoArray[i] = divDtoArray[i + 1];divDtoArray[i + 1].setDivFlag(0);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivBase()), i, 0);divTable.setValueAt(String.valueOf(divDtoArray[i].getLength()), i, 1);divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()), i, 2);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()), i, 2);} else if (runLength > 0) {int c2 = divDtoArray[runAllocFlag].getDivBase()+ jobToReady.getLimit();divDtoArray[runAllocFlag].setDivBase(c2);divDtoArray[runAllocFlag].setLength(runLength);divTable.setValueAt(String.valueOf(c2),runAllocFlag, 0);divTable.setValueAt(String.valueOf(runLength),runAllocFlag, 1);divTable.setValueAt(String.valueOf(divDtoArray[runAllocFlag].getDivFalg()),runAllocFlag, 2);}readyDtoArray[end] = jobToReady;readyTable.setValueAt(jobToReady.getProcessName(), 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() < newSort[runi].getBase()) {break;}}// 如果不是插入到数组末尾,则把比它大的都向后挪一位并设置JTable中的显示for (int i = point; i > runi; i--) {newSort[i] = newSort[i - 1];allocTable.setValueAt(String.valueOf(newSort[i].getBase()), i,0);allocTable.setValueAt(String.valueOf(newSort[i].getLimit()), i,1);allocTable.setValueAt(newSort[i].getProcessName(), i, 2);}// 插入新生成的对象newSort[runi] = jobToReady;allocTable.setValueAt(String.valueOf(jobToReady.getBase()),runi, 0);allocTable.setValueAt(String.valueOf(jobToReady.getLimit()),runi, 1);allocTable.setValueAt(jobToReady.getProcessName(), runi, 2);point++;}}} else {readyDtoArray[end] = a;readyTable.setValueAt(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) {JOptionPane.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.setLimit(Integer.parseInt(spaceText.getText()));String s = prorityCom.getSelectedItem().toString();test.setPrority(Integer.parseInt(s));test.setProcessName(nameText.getText().trim());newDtoArray[count] = test;jobDtoVector.add(newDtoArray[count]);jobVectorName.add(newDtoArray[count].getProcessName());jobList.setListData(jobVectorName);count++;nameText.setText("");timeText.setText("");spaceText.setText("");PcbDTO b = (PcbDTO) jobDtoVector.elementAt(0);for (int i = 0; i < 20; i++) {if (divDtoArray[i].getDivFalg() == 1) {if (divDtoArray[i].getLength() >= b.getLimit()) {newAllocFlag = i;break;}}}// 在就绪队列未满且存有足够空间时将后备队列jobDtoVetor中的对象添加到就绪队列中if ((end + 2) % 6 != first && newAllocFlag >= 0) {jobDtoVector.removeElementAt(0);b.setProcessState(PcbDTO.Ready);b.setBase(divDtoArray[newAllocFlag].getDivBase());newLength = divDtoArray[newAllocFlag].getLength()- b.getLimit();if (newLength == 0) {int i = newAllocFlag;divDtoArray[i].setDivFlag(0);for (; i < 19; i++) {if (divDtoArray[i + 1].getDivFalg() == 1) {divDtoArray[i] = divDtoArray[i + 1];divDtoArray[i + 1].setDivFlag(0);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivBase()),i, 0);divTable.setValueAt(String.valueOf(divDtoArray[i].getLength()),i, 1);divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i, 2);}divTable.setValueAt(String.valueOf(divDtoArray[i].getDivFalg()),i, 2);} else if (newLength > 0) {int c1 = divDtoArray[newAllocFlag].getDivBase()+ b.getLimit();divDtoArray[newAllocFlag].setDivBase(c1);divDtoArray[newAllocFlag].setLength(newLength);divTable.setValueAt(String.valueOf(c1),newAllocFlag, 0);divTable.setValueAt(String.valueOf(newLength),newAllocFlag, 1);divTable.setValueAt(String.valueOf(divDtoArray[newAllocFlag].getDivFalg()), newAllocFlag, 2);}readyDtoArray[end] = b;jobVectorName.remove(jobVectorName.indexOf(b.getProcessName()));readyTable.setValueAt(b.getProcessName(), end, 0);readyTable.setValueAt(b.getRunTime(), end, 1);readyTable.setValueAt(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() < newSort[newi].getBase()) {break;}}// 如果不是插入到数组末尾,则把比它大的都向后挪一位并设置JTable中的显示for (int i = point; i > newi; i--) {newSort[i] = newSort[i - 1];allocTable.setValueAt(String.valueOf(newSort[i].getBase()), i, 0);allocTable.setValueAt(String.valueOf(newSort[i].getLimit()), i, 1);allocTable.setValueAt(newSort[i].getProcessName(),i, 2);}// 插入新生成的对象newSort[newi] = b;allocTable.setValueAt(String.valueOf(b.getBase()),newi, 0);allocTable.setValueAt(String.valueOf(b.getLimit()),newi, 1);allocTable.setValueAt(b.getProcessName(), newi, 2);point++;}}} else if (jb == susButton) {if (readyDtoArray[readyTable.getSelectedRow()] != null) { if (!readyDtoArray[readyTable.getSelectedRow()].getProcessState().equals("waiting")) {readyDtoArray[readyTable.getSelectedRow()].setProcessState(PcbDTO.Waiting);readyTable.setValueAt("waiting",readyTable.getSelectedRow(), 3);waitingDtoVector.add(readyDtoArray[readyTable.getSelectedRow()]);waitingVectorName.add(readyDtoArray[readyTable.getSelectedRow()].getProcessName());waitingList.setListData(waitingVectorName);} else {System.out.println("已挂起");}} else {JOptionPane.showMessageDialog(null, "请选择要挂起的进程");// System.out.println("请选择要挂起的进程");}} else if (jb == relaxButton) {String s = (String) waitingList.getSelectedValue();if (s != null) {waitingVectorName.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(readyDtoArray[i].getProcessName())) {readyTable.setValueAt("ready", i, 3);break;}}waitingList.setListData(waitingVectorName);} else {JOptionPane.showMessageDialog(null, "请选择要解挂的进程");// System.out.println("没有选择的进程");}}}}public static void main(String args[]) {new MainFrame();}}运行截图:运行后开始界面输入进程后界面建立后挂起挂起后各存分配解挂操作异常时的提示六、实验总结这个程序,我参考了课本,互联网以及相关资料。

相关主题