java操作系统优先级和分时调度算法和内存管理
好了最新的改版出炉,更加的符合用户的胃口,也是本人修改的最终版本。
更新的地方:1.添加了进度条标示了内存的占用情况;2.实现了每一个按钮的快捷键;3.将光标置于PCB的ID处,方便用户直接开始输入数据;4.加上了鼠标直接对队列的操作,比如对就绪队列的进程双击,即可实现挂起功能,更多的按钮解释都在相应的按钮上加了提示;5.关键的系统操作按钮追加了颜色,并且在最后添加了帮助功能;6.优化了界面的大小和边框,使就绪队列的数据居中显示;
package CPU;
public class ProcessPCB {
// backupBAK 后备 ready 就绪 suspend 挂起 memory内存
private String PID;
private int RequiredTime;
private int Priority;
private String Status;
private int MwmoryBase = 0000;
private int MemoryLimit;
// private String PCBPointer;
public ProcessPCB(String initpID, int initRTime, int initpriority,
String status, int initBase, int initLimit) {
this.PID = initpID;
this.RequiredTime = initRTime;
this.Priority = initpriority;
this.Status = status;
this.MwmoryBase = initBase;
this.MemoryLimit = initLimit;
}
public String getPID() {
if(this.PID == null)
return " ";
else
return this.PID;
}
public int getRequiredTime() {
return this.RequiredTime;
}
public int getPriority() {
return this.Priority;
}
public String getStatus() {
if(this.Status == null)
return " ";
else
return this.Status;
}
public int getMemoryBase() {
return this.MwmoryBase;
}
public int getMemoryLimit() {
return this.MemoryLimit;
}
public boolean equals(ProcessPCB pcb) {
if(pcb.getPID() == this.getPID()) {
return true;
}
else return false;
}
public String toString() {
return this.getPID() + "_" + this.getRequiredTime() + "_" + this.getPriority() + "_"
+ this.getStatus() + "_" + this.getMemoryBase() + "_" + this.getMemoryLimit() + "\n";
}
public void run() {
this.RequiredTime = this.RequiredTime-1;
this.Priority = this.Priority-1;
}
}
package CPU;
import java.util.ArrayList;
import java.util.Iterator;
public class PCBRecords implements Iterable<ProcessPCB> {
private ArrayList<ProcessPCB> PCBItems;
public ArrayList<ProcessPCB> getPCBItems() {
return this.PCBItems;
}
public PCBRecords() {
this.PCBItems = new ArrayList<ProcessPCB>();
}
public void addItem(ProcessPCB PcbItem) {
this.PCBItems.add(PcbItem);
}
public void removeItem(ProcessPCB PCbItem) {
this.PCBItems.remove(PCbItem);
}
public ProcessPCB getItem(ProcessPCB processPCB) {
for (ProcessPCB pCbItem : this.PCBItems) {
if (pCbItem.equals(processPCB)) {
return pCbItem;
}
}
return null;
}
public ProcessPCB getItem(String pid) {
for (ProcessPCB pcBItem : this.PCBItems) {
if (pcBItem.getPID().equals(pid)) {
return pcBItem;
}
}
return null;
}
public int getNumberOfItems() {
return this.PCBItems.size();
}
public String[] getItemsProperties() {
String itemsProperties[] = new String[getNumberOfItems()];
int i = 0;
for(Iterator iterator1 = PCBItems.iterator(); iterator1.hasNext();)
{
ProcessPCB stu_Item = (ProcessPCB)iterator1.next();
itemsProperties[i++] = stu_Item.toString();
}
return itemsProperties;
}
public Iterator<ProcessPCB> iterator() {
return this.PCBItems.iterator();
}
}
package CPU;
public class MemoryItem {
private int memoryBase=0;
private int memoryLimit=0;
private int availableStatus=0;
public MemoryItem(int initMemoryBase, int initMemoryLimit) {
this.memoryBase = initMemoryBase;
this.memoryLimit = initMemoryLimit;
}
public int getMemoryBase() {
return this.memoryBase;
}
public int getMemoryLimit() {
return this.memoryLimit;
}
public int getStatus() {
return this.availableStatus;
}
public String toString() {
return this.getMemoryBase() + "_" + this.getMemoryLimit() + "\n";
}
}
package Final;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
public class CPUScheduling extends JFrame {
/**
* 定义变量
*/
private static final long serialVersionUID = -6748846647033499999L;
//放歌对象
private AudioClip clip;
//内存占用情况
private static int rate=0;
private static JProgressBar jpr = new JProgressBar(0,1000);
static private PrintWriter stdErr = new PrintWriter(System.err, true);
private boolean bflag=false;
static private int WIDTH = 1000, HEIGHT = 730; // the size of the Frame 主面板
/* 各列表对应的面板规格*/
/* 对应各名词释义 backupBAK 后备 ready 就绪 suspend 挂起 memory内存 */
static private int BackupBAK_CELL_SIZE = 250, BackupBAK_LIST_ROWS = 10; //后备队列
static private int Suspend_CELL_SIZE = 250, Suspend_LIST_ROWS = 10; //挂起队列
static private int Ready_CELL_SIZE = 200, Ready_LIST_ROWS = 6; //就绪队列
static private int Memory_CELL_SIZE = 200, Memory_LIST_ROWS = 4; //内存队列
static private int CPU_ROWS =3, CPU_COLS = 2; //CPU面板
static private int STATUS_ROWS = 7, STATUS_COLS = 45; //系统状态面板
private int timeslice = 1; //设置时间片大小
private int systemStatus=0; //设置系统状态 0——系统预备状态,等待开始,1——系统运行状态,2——系统暂停状态
static private int TOTAL__TEXTFIELD_SIZE = 10; // Size total text field 记录各队列元素个数
private JList backupList, suspendList, readyList, memoryList; //各队列相对应的数组列表
// 进程添加框中的"添加至后备队列","添加至就绪队列","重置"Button
private JButton addToBAKButton, addToReadyButton, resetButton;
//就绪队列框中的"挂起",挂起队列框中的"解挂","删除"Button
private JButton suspendButton, umountButton, removeButton;
//Status面板中的"启动系统","重置系统"Button,帮助按钮
private JButton startButton, pauseButton, resetSyatemButton,helpButton;
//优先级和时间片单选钮及时间片显示框
private JRadioButton priorityJRB, timesliceJRB;
private JLabel timesliceSizeLabel;
private JTextField timesliceJtf;
//后备面板、进程添加面板、挂起面板、内存面板
private JPanel backupBAKPanel, PCBItemPanel, suspendedPanel, memoryPanel;
//后备队列、挂起队列元素总数标签
private JLabel backupTotalLabel, suspendTotalLabel;
//进程信息标签 进程编号PID,所需运行时间requiredTime,优先级priority,当前状态statues,内存中的基址base,所需内存大小limit
private JLabel PIDLabel, requiredTimeLabel, priorityLabel, statuesLabel, baseLabel, limitLabel;
//后备队列、挂起队列元素总数文本框(不可编辑)
private JTextField backupTotalTextField, suspendTotalTextField;
//进程信息文本框 PID(可编辑),requiredTime(可编辑),priority(可编辑),status(不可编辑),base(不可编辑),limit(可编辑)
private JTextField PIDTextField, requiredTimeTextField, priorityTextField, statusTextField, baseTextField, limitTextField;
//CPU状态显示文本域(不可编辑),status信息文本域(用于现实程序每一步的操作和影响,不可编辑)
private JTextArea CPUTextArea, statuesTextArea;
//后备队列PCB数组,就绪、挂起,——内存(可分分区表)
PCBRecords backupPCB, readyPCB, suspendedPCB;
private MemoryRecords memoryItems;
private boolean flag = false;
/**
* 主函数
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new CPUScheduling().initFrame();
}
//初始化Frame
public void initFrame() {
backupList = new JList();
backupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
backupList.setVisibleRowCount(BackupBAK_LIST_ROWS);
backupList.setFixedCellWidth(BackupBAK_CELL_SIZE);
suspendList = new JList();
suspendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
suspendList.setVisibleRowCount(Suspend_LIST_ROWS);
suspendList.setFixedCellWidth(Suspend_CELL_SIZE);
readyList = new JList();
readyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
readyList.setVisibleRowCount(Ready_LIST_ROWS);
readyList.setFixedCellWidth(Ready_CELL_SIZE);
memoryList = new JList();
memoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
memoryList.setVisibleRowCount(Memory_LIST_ROWS);
memoryList.setFixedCellWidth(Memory_CELL_SIZE);
suspendButton = new JButton("挂起(3)");
addToBAKButton = new JButton("加入后备(1)");
addToReadyButton = new JButton("加入就绪(2)");
resetButton = new JButton("重置(0)");
umountButton = new JButton("解挂(4)");
removeButton = new JButton("移除(5)");
startButton = new JButton("调度开始(Enter)");
pauseButton = new JButton("暂停(Pause)");
resetSyatemButton = new JButton("重置系统(Backspace)");
helpButton = new JButton("帮助(H)");
priorityJRB = new JRadioButton("优先级(Page Up)", true);
timesliceJRB = new JRadioButton("时间片(Page Down)");
suspendButton.setToolTipText("双击进程可以直接挂起!");
umountButton.setToolTipText("双击解挂按钮的正上方的进程,可以直接解挂!");
removeButton.setToolTipText("双击移除按钮的正上方的进程,可以直接移除!");
startButton.setBackground(Color.GREEN);
pauseButton.setBackground(new Color(0,150,255));
resetSyatemButton.setBackground(Color.RED);
backupTotalLabel = new JLabel("总计:");
backupTotalTextField = new JTextField("0", TOTAL__TEXTFIELD_SIZE);
backupTotalTextField.setEditable(false);
suspendTotalLabel = new JLabel("总计:");
suspendTotalTextField = new JTextField("0", TOTAL__TEXTFIELD_SIZE);
suspendTotalTextField.setEditable(false);
timesliceSizeLabel = new JLabel("时间片:");
timesliceJtf = new JTextField("3", 5);
timesliceJtf.setEditable(true);
CPUTextArea = new JTextArea(CPU_ROWS, CPU_COLS);
CPUTextArea.setEditable(false);
statuesTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS);
statuesTextArea.setEditable(false);
/* north panel*/
JPanel northPanel = new JPanel(new GridLayout(1, 3));
PCBItemPanel = new JPanel(new FlowLayout());
PCBItemPanel.setBorder(
BorderFactory.createTitledBorder("PCB项目信息"));
JPanel PCBItemButtonJPanel = new JPanel(new GridLayout(1, 3));
PCBItemButtonJPanel.add(addToBAKButton);
PCBItemButtonJPanel.add(addToReadyButton);
PCBItemButtonJPanel.add(resetButton);
PCBItemPanel.add(this.initPCBItemPanel());
PCBItemPanel.add(PCBItemButtonJPanel);
//backupBAKList Panel
backupBAKPanel = new JPanel(new BorderLayout());
JTable jtb = new JTable(1, 6);
jtb.setRowHeight(30);
jtb.setEnabled(false);
jtb.setGridColor(Color.BLUE);
//设置jtable中的数据居中显示
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
jtb.setDefaultRenderer(Object.class,r);
jtb.setFont(new Font("",Font.BOLD,14));
String[] str ={"name","time","priority","status","base","limit"};
for(int i = 0;i<6;i++){
jtb.setValueAt(str[i], 0, i);
}
backupBAKPanel.setBorder(BorderFactory.createTitledBorder("后备队列"));
backupList.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
JPanel backupTotalPAnel = new JPanel();
backupTotalPAnel.add(backupTotalLabel);
backupTotalPAnel.add(backupTotalTextField);
backupBAKPanel.add(jtb,BorderLayout.NORTH);
backupList.setFont(new Font(null, Font.BOLD,18));
backupBAKPanel.add (
new JScrollPane(backupList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
backupBAKPanel.add(backupTotalPAnel, BorderLayout.SOUTH);
// SuspendList Panel
suspendedPanel = new JPanel(new BorderLayout());
suspendedPanel.setBorder(BorderFactory.createTitledBorder("挂起队列"));
JPanel suspendedTotalPAnel = new JPanel();
suspendedTotalPAnel.add(suspendTotalLabel);
suspendedTotalPAnel.add(suspendTotalTextField);
JPanel suspendComponentPanel = new JPanel(new GridLayout(1, 2));
suspendComponentPanel.add(umountButton);
suspendComponentPanel.add(removeButton);
suspendedPanel.add (suspendedTotalPAnel, BorderLayout.NORTH);
suspendList.setFont(new Font(null, Font.BOLD,18));
suspendList.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
suspendedPanel.add (
new JScrollPane(suspendList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
suspendedPanel.add(suspendComponentPanel, BorderLayout.SOUTH);
/* center Panel*/
JPanel centrelPanel = new JPanel(new GridLayout(1, 3));
// readyList panel
JPanel readyListPanel = new JPanel(new BorderLayout());
readyListPanel.setBorder(BorderFactory.createTitledBorder("就绪队列"));
readyList.setFont(new Font(null, Font.BOLD,18));
readyList.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
readyListPanel.add (
new JScrollPane(readyList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
readyListPanel.add (suspendButton, BorderLayout.SOUTH);
// CPU panel
JPanel CPUPanel = new JPanel(new BorderLayout());
CPUPanel.setBorder(BorderFactory.createTitledBorder("CPU-运行"));
JLabel jl=new JLabel(new ImageIcon("2.gif"));
CPUPanel.add(jl,BorderLayout.CENTER);
CPUTextArea.setFont(new Font(null, Font.BOLD,18));
CPUPanel.add (new JScrollPane(CPUTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),BorderLayout.NORTH);
memoryPanel = new JPanel(new BorderLayout());
memoryPanel.setBorder(BorderFactory.createTitledBorder("内存列表(正被占用内存——剩余内存)"));
memoryList.setFont(new Font(null, Font.BOLD,18));
memoryPanel.add (
new JScrollPane(memoryList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),BorderLayout.NORTH);
//加入内存占用的比例条
jpr.setBackground(Color.WHITE);
jpr.setForeground(Color.BLUE);
jpr.setStringPainted(true);
memoryPanel.add(jpr);
northPanel.add(backupBAKPanel);
northPanel.add(CPUPanel);
northPanel.add(memoryPanel);
centrelPanel.add(readyListPanel);
centrelPanel.add(suspendedPanel);
centrelPanel.add(PCBItemPanel);
/*statues panel*/
JPanel southPanel = new JPanel(new BorderLayout());
JPanel statuesPanel = new JPanel();
statuesPanel.setBorder(BorderFactory.createTitledBorder("状态"));
statuesTextArea.setBackground(Color.YELLOW);
statuesTextArea.setFont(new Font(null, Font.BOLD,18));
statuesPanel.add (new JScrollPane(statuesTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
JPanel systemContralButtonPanel = new JPanel(new GridLayout(7, 1));
systemContralButtonPanel.setBorder(BorderFactory.createTitledBorder("系统控制区"));
ButtonGroup group = new ButtonGroup();
group.add(priorityJRB);
group.add(timesliceJRB);
JPanel porityPanel = new JPanel(new GridLayout(1, 2));
porityPanel.add(timesliceSizeLabel);
porityPanel.add(timesliceJtf);
systemContralButtonPanel.add(priorityJRB);
systemContralButtonPanel.add(timesliceJRB);
systemContralButtonPanel.add(porityPanel);
systemContralButtonPanel.add(startButton);
systemContralButtonPanel.add(pauseButton);
systemContralButtonPanel.add(resetSyatemButton);
systemContralButtonPanel.add(helpButton);
southPanel.add(statuesPanel, BorderLayout.CENTER);
southPanel.add(systemContralButtonPanel, BorderLayout.EAST);
// 布置界面
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(centrelPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int result=JOptionPane.showConfirmDialog(null, "你确定要退出?", "Information", JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.YES_NO_OPTION)
//这里用这个比较合适,因为这样是直接退出程序,而dispose()只关闭窗体,而程序还没结束。
System.exit(0);
}
});
//statuesPanel
// 开始监听按钮
backupList.addListSelectionListener(new DisplayBackupProcessListener());
suspendList.addListSelectionListener(new DisplaySuspendedProcessListener());
readyList.addListSelectionListener(new DisplayreadyProcessListener());
addToBAKButton.addActionListener(new AddToBAKListener());
addToReadyButton.addActionListener(new AddToReadyListener());
resetButton.addActionListener(new ResetListener());
suspendButton.addActionListener(new SuspendListener());
umountButton.addActionListener(new UmountListener());
removeButton.addActionListener(new RemoveListener());
startButton.addActionListener(new StartSystemListener());
pauseButton.addActionListener(new SystemPauseListener());
resetSyatemButton.addActionListener(new ResetSystemListener());
priorityJRB.addActionListener(new priotiryListener());
timesliceJRB.addActionListener(new timeslicListener());
//实现快捷键
addToBAKButton.setMnemonic(KeyEvent.VK_1);
addToReadyButton.setMnemonic(KeyEvent.VK_2);
resetButton.setMnemonic(KeyEvent.VK_0);
suspendButton.setMnemonic(KeyEvent.VK_3);
umountButton.setMnemonic(KeyEvent.VK_4);
removeButton.setMnemonic(KeyEvent.VK_5);
startButton.setMnemonic(KeyEvent.VK_ENTER);
pauseButton.setMnemonic(KeyEvent.VK_PAUSE);
resetSyatemButton.setMnemonic(KeyEvent.VK_BACK_SPACE);
helpButton.setMnemonic(KeyEvent.VK_H);
priorityJRB.setMnemonic(KeyEvent.VK_PAGE_UP);
timesliceJRB.setMnemonic(KeyEvent.VK_PAGE_DOWN);
backupPCB = new PCBRecords();
readyPCB = new PCBRecords();
suspendedPCB = new PCBRecords();
memoryItems = new MemoryRecords();
MemoryItem initMemoryItem = new MemoryItem(0,1000);
memoryItems.addItem(initMemoryItem);
backupList.setListData(backupPCB.getItemsProperties());
readyList.setListData(readyPCB.getItemsProperties());
suspendList.setListData(suspendedPCB.getItemsProperties());
memoryList.setListData(memoryItems.getItemsProperties());
setTitle("CPU调度");
setLayout(new GridLayout(3,3));
setIconImage(new ImageIcon("321.jpg").getImage());//添加方框的图标
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setSize(WIDTH, HEIGHT);
//设置界面的外观
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
initHelp();
}
//帮助界面
private void initHelp() {
JFrame jf = new JFrame();
jf.setSize(100, 100);
jf.setResizable(false);
jf.setLocationRelativeTo(null);
JButton shiy =new JButton("使用说明");
JButton guany =new JButton("关于作者");
jf.setLayout(new GridLayout(2,1));
jf.add(shiy);
jf.add(guany);
jf.setVisible(true);
shiy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "使用ALT+相应的按键,可以实现键盘操作!");
}
});
guany.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Ctong from the class 1307");
}
});
}
});
//实现双击挂起
readyList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getClickCount()==2){
String selectedReadyItem = null;
String pid = "";
if(readyPCB.getNumberOfItems() == 0)
{
statuesTextArea.append("就绪队列为空!\n");
} else {
try {
selectedReadyItem = (String) readyList.getSelectedValue();
if(selectedReadyItem == null) {
statuesTextArea.append("请选择一个进程放入就绪队列!\n");
} else
{
StringTokenizer stringtokenizer = new StringTokenizer(selectedReadyItem, "_");
pid = stringtokenizer.nextToken();
ProcessPCB selectedItem = readyPCB.getItem(pid);
if(selectedItem == null) {
statuesTextArea.append("进程 " + pid + " 不存在!\n");
} else {
ProcessPCB boolItem = suspendedPCB.getItem(pid);
if(boolItem != null) {
statuesTextArea.append("PCB " + pid + " 已经存在于挂起队列!\n"
+ "你需要去修改所选择的PCB的PID!\n");
while(boolItem != null) {
pid = "S" + pid;
boolItem = suspendedPCB.getItem(pid);
}
}
//注意下一步存在问题!
ProcessPCB newPcb = new ProcessPCB(pid, selectedItem.getRequiredTime(),
selectedItem.getPriority(),"Suspended", -1, selectedItem.getMemoryLimit());
rate-=selectedItem.getMemoryLimit();
jpr.setValue(rate);
memoryRecall(selectedItem.getMemoryBase(), selectedItem.getMemoryLimit());
suspendedPCB.addItem(newPcb);
readyPCB.removeItem(selectedItem);
sortReadyPCB();
suspendList.setListData(suspendedPCB.getItemsProperties());
statuesTextArea.append("进程已被挂起!\n");
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems()));
}
}
} catch (RuntimeException e1) {
// TODO Auto-generated catch block
}
}
}
}
});
//挂起队列双击解挂
suspendList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2&&e.getX()<=150){
String selectedSuspendedItem = null;
String id = "";
if(suspendedPCB.getNumberOfItems() == 0)
{
statuesTextArea.append("挂起队列为空!\n");
} else {
try {
selectedSuspendedItem = (String) suspendList.getSelectedValue();
if(selectedSuspendedItem == null) {
statuesTextArea.append("请从挂起队列里面选择一个进程!\n");
} else
{
StringTokenizer stringtokenizer = new StringTokenizer(selectedSuspendedItem, "_");
id = stringtokenizer.nextToken();
ProcessPCB selectedItem = suspendedPCB.getItem(id);
if(selectedItem == null) {
statuesTextArea.append("进程 " + id + " 不存在!\n");
} else {
int s3 = selectedItem.getMemoryLimit();
if(readyPCB.getNumberOfItems() < 6) {
if(boolMemoryAllocation(s3)) {
addToReadyList(id, selectedItem.getRequiredTime(),
selectedItem.getRequiredTime(), s3, "r");
statuesTextArea.append("进程已被解挂进入了就绪队列!\n");
} else {
statuesTextArea.append("内存空间不足! PCB将被解挂进入后备队列!\n");
addToBackupList(id, selectedItem.getRequiredTime(),
selectedItem.getRequiredTime(), s3, "II");
statuesTextArea.append("进程已被解挂入后备队列!\n");
}
} else {
statuesTextArea.append("就绪队列已满! 新的进程将被解挂进入后备队列!\n");
addToBackupList(id, selectedItem.getRequiredTime(),
selectedItem.getRequiredTime(), s3, "II");
statuesTextArea.append("进程已被解挂入后备队列!\n");
}
suspendedPCB.removeItem(selectedItem);
suspendList.setListData(suspendedPCB.getItemsProperties());
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems()));
}
}
} catch (RuntimeException e1) {
// TODO Auto-generated catch block
}
}
}
else if(e.getClickCount()==2&&e.getX()>150){
String selectedRemovsItem = null;
String id = "";
if(suspendedPCB.getNumberOfItems() == 0)
{
statuesTextArea.append("记录为空!\n");
} else
{
selectedRemovsItem = (String) suspendList.getSelectedValue();
if(selectedRemovsItem == null) {
statuesTextArea.append("ID不能为空!\n");
} else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedRemovsItem, "_");
id = stringtokenizer.nextToken();
ProcessPCB suspendedItem = suspendedPCB.getItem(id);
if(suspendedItem == null) {
statuesTextArea.append("进程" + id + " 不存在!\n");
} else {
suspendedPCB.removeItem(suspendedItem);
suspendList.setListData(suspendedPCB.getItemsProperties());
statuesTextArea.append("进程已被移除!\n");
}
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems()));
}
}
}
}
});
}
/**
* PCB项目信息的面板
* @return
*/
public JPanel initPCBItemPanel() {
JPanel iniPCBItemJPanel = new JPanel(new GridLayout(1,2));
JPanel iniNamePanel = new JPanel(new GridLayout(6, 1));
JPanel iniValuePanel = new JPanel(new GridLayout(6, 1));
PIDLabel = new JLabel("PID:",JLabel.CENTER);
requiredTimeLabel = new JLabel("所需时间:",JLabel.CENTER);
priorityLabel = new JLabel("优先级:",JLabel.CENTER);
statuesLabel = new JLabel("状态:",JLabel.CENTER);
baseLabel = new JLabel("基址:",JLabel.CENTER);
limitLabel = new JLabel("分配内存:",JLabel.CENTER);
iniNamePanel.add(PIDLabel);
iniNamePanel.add(requiredTimeLabel);
iniNamePanel.add(priorityLabel);
iniNamePanel.add(statuesLabel);
iniNamePanel.add(baseLabel);
iniNamePanel.add(limitLabel);
PIDTextField = new JTextField("", 10);
//获取焦点
this.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
PIDTextField.requestFocusInWindow();
}
});
PIDTextField.setEditable(true);
requiredTimeTextField = new JTextField("", 10);
requiredTimeTextField.setEditable(true);
priorityTextField = new JTextField("", 10);
priorityTextField.setEditable(true);
statusTextField = new JTextField("", 10);
statusTextField.setEditable(false);
baseTextField = new JTextField("", 10);
baseTextField.setEditable(false);
limitTextField = new JTextField("", 10);
limitTextField.setEditable(true);
limitTextField.setToolTipText("内存不可大于或等于1000");
iniValuePanel.add(PIDTextField);
iniValuePanel.add(requiredTimeTextField);
iniValuePanel.add(priorityTextField);
iniValuePanel.add(statusTextField);
iniValuePanel.add(baseTextField);
iniValuePanel.add(limitTextField);
iniPCBItemJPanel.add(iniNamePanel);
iniPCBItemJPanel.add(iniValuePanel);
return iniPCBItemJPanel;
}
/**
* 在PCB项目信息中显示鼠标选中的进程的信息
* @param pcb
*/
public void displayInformation(ProcessPCB pcb) {
PIDTextField.setText(pcb.getPID());
requiredTimeTextField.setText(Integer.toString(pcb.getRequiredTime()));
priorityTextField.setText(Integer.toString(pcb.getPriority()));
statusTextField.setText(pcb.getStatus());
baseTextField.setText(Integer.toString(pcb.getMemoryBase()));
limitTextField.setText(Integer.toString(pcb.getMemoryLimit()));
}
/**
* 显示后备队列进程类
* @author ctong
*
*/
class DisplayBackupProcessListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (! backupList.getValueIsAdjusting()) {
String id = null, selectedBAKItem = null;
selectedBAKItem = (String) backupList.getSelectedValue();
if(selectedBAKItem != null) {
StringTokenizer stringtokenizer = new StringTokenizer(selectedBAKItem, "_");
id = stringtokenizer.nextToken();
ProcessPCB backupItem = backupPCB.getItem(id);
if(backupItem != null)
{
displayInformation(backupItem);
}
statuesTextArea.append(backupItem.toString() + "进程 " + id
+ "加入后备队列了!\n");
}
}
}
}
/**
* 显示挂起队列进程类
* @author ctong
*
*/
class DisplaySuspendedProcessListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (! suspendList.getValueIsAdjusting()) {
String id = "";
String str = (String) suspendList.getSelectedValue();
if(str != null) {
StringTokenizer strtokenizer = new StringTokenizer(str, "_");
id = strtokenizer.nextToken();
ProcessPCB selectedSuspendedItem = suspendedPCB.getItem(id);
if(selectedSuspendedItem != null)
{
displayInformation(selectedSuspendedItem);
}
statuesTextArea.append(selectedSuspendedItem.toString() + "进程 " + id
+ "加入挂起队列了!\n");
}
}
}
}
/**
* 显示就绪队列进程类
* @author ctong
*
*/
class DisplayreadyProcessListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (! readyList.getValueIsAdjusting()) {
String id = null, s = null;
s = (String) readyList.getSelectedValue();
if(s != null) {
StringTokenizer sttokenizer = new StringTokenizer(s, "_");
id = sttokenizer.nextToken();
ProcessPCB readyItem = readyPCB.getItem(id);
if(readyItem != null)
{
displayInformation(readyItem);
}
statuesTextArea.append(readyItem.toString() + "进程 " + id
+ "加入就绪队列了!\n");
}
}
}
}
/**
* 内存可分分区表排序,按可用内存大小降序排列
*/
public void sortMemoryList() {
MemoryRecords currentListItems = new MemoryRecords();
int num = memoryItems.getNumberOfItems();
if(num>0) {
for(int i=num; i>=1; i--) {
Iterator memoryIterator = memoryItems.iterator();
MemoryItem currentItem = (MemoryItem) memoryIterator.next();
for( ; memoryIterator.hasNext(); ) {
MemoryItem nowItem = (MemoryItem) memoryIterator.next();
if(currentItem.getMemoryLimit() < nowItem.getMemoryLimit()) {
currentItem = null;
currentItem = nowItem;
}
}
currentListItems.addItem(currentItem);
memoryItems.removeItem(currentItem);
}
memoryItems = null;
memoryItems = currentListItems;
}
memoryList.setListData(memoryItems.getItemsProperties());
}
/**
* 判断能否为进程分配内存
* @param neededLimit
* @return
*/
public boolean boolMemoryAllocation(int neededLimit) {
if(memoryItems.getNumberOfItems()>0) {
Iterator memoryListIteartor = memoryItems.iterator();
MemoryItem lagerestItem = (MemoryItem) memoryListIteartor.next();
if(lagerestItem.getMemoryLimit()>= neededLimit)
return true;
else {
return false;
}
} else {
return false;
}
}
/**
* 内存分配,按最先适应算法原则
* @param neededLimit
* @return
*/
public int MemoryAllocation(int neededLimit) {
int currentBase=-1, currentLimit=-1;
Iterator memoryListIteartor = memoryItems.iterator();
MemoryItem lagerestItem = (MemoryItem) memoryListIteartor.next();
currentBase = lagerestItem.getMemoryBase()+neededLimit;
currentLimit = lagerestItem.getMemoryLimit()-neededLimit;
memoryItems.removeItem(lagerestItem);
if(currentLimit>0) {
memoryItems.addItem(new MemoryItem(currentBase, currentLimit));
}
sortMemoryList();
return lagerestItem.getMemoryBase();
}
/**
* 内存回收
* @param nowBase
* @param nowLimit
*/
public void memoryRecall(int nowBase, int nowLimit) {
MemoryItem recallItem = null;
int recalledBase = nowBase;
int recalledLimit = nowLimit;
if(memoryItems.getNumberOfItems()>0) {
Iterator memoryListIteartor = memoryItems.iterator();
for( ;memoryListIteartor.hasNext(); ) {
MemoryItem existedItem = (MemoryItem) memoryListIteartor.next();
if(existedItem.getMemoryBase() == (nowBase+nowLimit)) {
recalledLimit = recalledLimit+existedItem.getMemoryLimit();
memoryItems.removeItem(existedItem);
memoryListIteartor = memoryItems.iterator();
} else if(nowBase == (existedItem.getMemoryBase()+existedItem.getMemoryLimit())) {
recalledBase = existedItem.getMemoryBase();
recalledLimit = recalledLimit+existedItem.getMemoryLimit();
memoryItems.removeItem(existedItem);
memoryListIteartor = memoryItems.iterator();
}
}
}
recallItem = new MemoryItem(recalledBase, recalledLimit);
memoryItems.addItem(recallItem);
sortMemoryList();
}
/**
* 把字符串读成整数
* @param s
* @return
*/
public int readInteger(String s) {
int num = -1;
try {
num = Integer.parseInt(s);
} catch(NumberFormatException numberformatexception) {
statuesTextArea.append("请输入一个正整数!\n");
num = -999;
}
return num;
}
/**
* 加入后备队列函数
* @author ctong
*
*/
public void addToBackupList(String newID, int s1, int s2, int s3, String s) {
ProcessPCB item = backupPCB.getItem(newID);
if(item != null) {
statuesTextArea.append("PCB " + newID + " 已经存在于后备队列了!\n"
+ "你需要去改变所选PCB的PID!\n");
while(item != null) {
newID = s + newID;
item = backupPCB.getItem(newID);
}
}
ProcessPCB newPCB = new ProcessPCB(newID, s1, s2, "Waiting", -1, s3);
backupPCB.addItem(newPCB);
backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems()));
}
/**
* 加入就绪队列函数
* @author ctong
*
*/
public void addToReadyList(String nowID, int s1, int s2, int s3, String s) {
ProcessPCB item = readyPCB.getItem(nowID);
if(item != null) {
statuesTextArea.append("PCB " + nowID + " 已经存在于就绪队列了!\n"
+ "你需要去改变所选PCB的PID!\n");
while(item != null) {
nowID = s + nowID;
item = backupPCB.getItem(nowID);
}
}
int s4=MemoryAllocation(s3);
rate+=s3;
jpr.setValue(rate);
sortMemoryList();
ProcessPCB newPCB = new ProcessPCB(nowID, s1, s2, "Ready", s4, s3);
readyPCB.addItem(newPCB);
sortReadyPCB();
}
/**
* 监听加入后备队列按钮
* @author ctong
*
*/
class AddToBAKListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String newID = PIDTextField.getText();
String newTime = requiredTimeTextField.getText();
String newPriority = priorityTextField.getText();
String newLimit = limitTextField.getText();
int s1 = 0, s2 = 0, s3 = 0, tag1=-1, tag2=-1, tag3=-1;
if(newTime != null)
{
s1 = readInteger(newTime);
if(s1 > 0.0) tag1 = 1;
else statuesTextArea.append("所需时间必须是个正整数.\n");
}
if(newPriority != null)
{
s2 = readInteger(newPriority);
if(s1 != -999) tag2 = 1;
else statuesTextArea.append("优先级必须是个正整数.\n");
}
if(newLimit != null)
{
s3 = readInteger(newLimit);
if(s3 > 0.0&&s3 < 1000) tag3 = 1;
else if(s3<=0) statuesTextArea.append("所需内存大小必须是个正整数.\n");
else {statuesTextArea.append("进程长度不能大于内存最大值.\n");
JOptionPane.showMessageDialog(null, "进程长度不能大于内存最大值.", "错误", 2);
}
}
if(tag1 ==1 && tag2 == 1 && tag3 == 1) {
if(newID == null) {
statuesTextArea.append("ID不能为空!\n");
} else {
addToBackupList(newID, s1, s2, s3, "B");
statuesTextArea.append("PCB的信息加入后备队列里!\n");
reset();
}
}
}
}
/**
* 监听加入就绪队列按钮
* @author ctong
*
*/
class AddToReadyListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String nowID = PIDTextField.getText();
String time = requiredTimeTextField.getText();
String priority = priorityTextField.getText();
String limit = limitTextField.getText();
int s1 = 0, s2 = 0, s3 =0;
int tag1 = -1, tag2 = -1, tag3 = -1;
if(time != null)
{
s1 = readInteger(time);
if(s1 > 0.0) tag1=1;
else statuesTextArea.append("所需时间必须是个正整数.\n");
}
if(priority != null)
{
s2 = readInteger(priority);
if(s2 != -999) tag2=1;
else statuesTextArea.append("优先级必须是个正整数.\n");
}
if(limit != null)
{
s3 = readInteger(limit);
if(s3 > 0.0&&s3 < 1000) tag3 = 1;
else if(s3<=0) statuesTextArea.append("所需内存大小必须是个正整数.\n");
else {statuesTextArea.append("进程长度不能大于内存最大值.\n");
JOptionPane.showMessageDialog(null, "进程长度不能大于内存最大值.", "错误", 2);
}
}
if(tag1 ==1 && tag2 == 1 && tag3 ==1) {
if(nowID == null) {
statuesTextArea.append("ID不能为空!\n");
} else {
if(readyPCB.getNumberOfItems() < 6) {
if(boolMemoryAllocation(s3)) {
addToReadyList(nowID, s1, s2, s3, "R");
statuesTextArea.append("进程加入就绪队列!\n");
} else {
statuesTextArea.append("内存空间不足! PCB将被加载入后备队列!\n");
addToBackupList(nowID, s1, s2, s3, "b");
statuesTextArea.append("进程加入后备队列!\n");
}
} else {
statuesTextArea.append("就绪队列已满! 新的进程将被加载入后备队列!\n");
addToBackupList(nowID, s1, s2, s3, "b");
statuesTextArea.append("进程加入后备队列!\n");
}
reset();
}
}
}
}
/**
* 重置函数
*/
public void reset() {
PIDTextField.setText("");
requiredTimeTextField.setText("");
if(!bflag)
priorityTextField.setText("");
statusTextField.setText("");
baseTextField.setText("");
limitTextField.setText("");
}
/**
* 监听重置按钮
* @author ctong
*
*/
class ResetListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
reset();
}
}
/**
* 监听挂起按钮
* @author ctong
*
*/
//注:在挂起时,不会触发进程调度,而是在点击"startSyatemJButton"时才会出发进程调度
class SuspendListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String selectedReadyItem = null;
String pid = "";
if(readyPCB.getNumberOfItems() == 0)
{
statuesTextArea.append("就绪队列为空!\n");
} else {
try {
selectedReadyItem = (String) readyList.getSelectedValue();
if(selectedReadyItem == null) {
statuesTextArea.append("请选择一个进程放入就绪队列!\n");
} else
{
StringTokenizer stringtokenizer = new StringTokenizer(selectedReadyItem, "_");
pid = stringtokenizer.nextToken();
ProcessPCB selectedItem = readyPCB.getItem(pid);
if(selectedItem == null) {
statuesTextArea.append("进程 " + pid + " 不存在!\n");
} else {
ProcessPCB boolItem = suspendedPCB.getItem(pid);
if(boolItem != null) {
statuesTextArea.append("PCB " + pid + " 已经存在于挂起队列!\n"
+ "你需要去修改所选择的PCB的PID!\n");
while(boolItem != null) {
pid = "S" + pid;
boolItem = suspendedPCB.getItem(pid);
}
}
//注意下一步存在问题!
ProcessPCB newPcb = new ProcessPCB(pid, selectedItem.getRequiredTime(),
selectedItem.getPriority(),"Suspended", -1, selectedItem.getMemoryLimit());
rate-=selectedItem.getMemoryLimit();
jpr.setValue(rate);
memoryRecall(selectedItem.getMemoryBase(), selectedItem.getMemoryLimit());
suspendedPCB.addItem(newPcb);
readyPCB.removeItem(selectedItem);
sortReadyPCB();
suspendList.setListData(suspendedPCB.getItemsProperties());
statuesTextArea.append("进程已被挂起!\n");
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems()));
}
}
} catch (RuntimeException e1) {
// TODO Auto-generated catch block
}
}
}
}
/**
* 对Ready队列进行优先级排序
*/
public void sortReadyPCB() {
PCBRecords currentReadyPCB = new PCBRecords();
int num = readyPCB.getNumberOfItems();
if(num > 0) {
for(int i=num; i>=1; i--) {
Iterator readyIterator = readyPCB.iterator();
ProcessPCB currentItem = (ProcessPCB) readyIterator.next();
for( ; readyIterator.hasNext(); ) {
ProcessPCB nowItem = (ProcessPCB) readyIterator.next();
if(currentItem.getPriority() < nowItem.getPriority()) {
currentItem = null;
currentItem = nowItem;
}
}
currentReadyPCB.addItem(currentItem);
readyPCB.removeItem(currentItem);
}
readyPCB = null;
readyPCB = currentReadyPCB;
}
readyList.setListData(readyPCB.getItemsProperties());
}
/**
* 实现解挂按钮的监听
*/
class UmountListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String selectedSuspendedItem = null;
String id = "";
if(suspendedPCB.getNumberOfItems() == 0)
{
statuesTextArea.append("挂起队列为空!\n");
} else {
try {
selectedSuspendedItem = (String) suspendList.getSelectedValue();
if(selectedSuspendedItem == null) {
statuesTextArea.append("请从挂起队列里面选择一个进程!\n");
} else
{
StringTokenizer stringtokenizer = new StringTokenizer(selectedSuspendedItem, "_");
id = stringtokenizer.nextToken();
ProcessPCB selectedItem = suspendedPCB.getItem(id);
if(selectedItem == null) {
statuesTextArea.append("进程 " + id + " 不存在!\n");
} else {
int s3 = selectedItem.getMemoryLimit();
if(readyPCB.getNumberOfItems() < 6) {
if(boolMemoryAllocation(s3)) {
addToReadyList(id, selectedItem.getRequiredTime(),
selectedItem.getRequiredTime(), s3, "r");
statuesTextArea.append("进程已被解挂进入了就绪队列!\n");
} else {
statuesTextArea.append("内存空间不足! PCB将被解挂进入后备队列!\n");
addToBackupList(id, selectedItem.getRequiredTime(),
selectedItem.getRequiredTime(), s3, "II");
statuesTextArea.append("进程已被解挂入后备队列!\n");
}
} else {
statuesTextArea.append("就绪队列已满! 新的进程将被解挂进入后备队列!\n");
addToBackupList(id, selectedItem.getRequiredTime(),
selectedItem.getRequiredTime(), s3, "II");
statuesTextArea.append("进程已被解挂入后备队列!\n");
}
suspendedPCB.removeItem(selectedItem);
suspendList.setListData(suspendedPCB.getItemsProperties());
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems()));
}
}
} catch (RuntimeException e1) {
// TODO Auto-generated catch block
}
}
}
}
/**
* 获取时间片
* @return
*/
public int getTimeslice() {
return timeslice;
}
/**
* 实现移除按钮的监听
*/
class RemoveListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String selectedRemovsItem = null;
String id = "";
if(suspendedPCB.getNumberOfItems() == 0)
{
statuesTextArea.append("记录为空!\n");
} else
{
selectedRemovsItem = (String) suspendList.getSelectedValue();
if(selectedRemovsItem == null) {
statuesTextArea.append("ID不能为空!\n");
} else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedRemovsItem, "_");
id = stringtokenizer.nextToken();
ProcessPCB suspendedItem = suspendedPCB.getItem(id);
if(suspendedItem == null) {
statuesTextArea.append("进程" + id + " 不存在!\n");
} else {
suspendedPCB.removeItem(suspendedItem);
suspendList.setListData(suspendedPCB.getItemsProperties());
statuesTextArea.append("进程已被移除!\n");
}
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems()));
}
}
}
}
/**
* 内存进程调度
*/
public void PCBSchudling() {
while(readyPCB.getNumberOfItems() < 6) {
if(backupPCB.getNumberOfItems() > 0) {
Iterator bwackupPCBIterator = backupPCB.iterator();
ProcessPCB newItem = (ProcessPCB) bwackupPCBIterator.next();
readyPCB.addItem(newItem);
backupPCB.removeItem(newItem);
sortReadyPCB();
readyList.setListData(readyPCB.getItemsProperties());
backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems()));
} else break;
}
}
/**
*
* @author ctong
* 调度线程
*/
class RunThread extends Thread {
int t, TimeSlice;
boolean Flag = true;
public void run(){
while(Flag) {
if(getSystemStatus() == 0) {
Flag = false;
break;
} else if(getSystemStatus() == 2) {
while(getSystemStatus() == 2) {
try {
Thread.sleep(1000);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
if(readyPCB.getNumberOfItems() < 6) {
if(backupPCB.getNumberOfItems() > 0) {
while(readyPCB.getNumberOfItems() < 6) {
if(backupPCB.getNumberOfItems() > 0) {
Iterator bwackupPCBIterator = backupPCB.iterator();
ProcessPCB newItem = (ProcessPCB) bwackupPCBIterator.next();
while(bwackupPCBIterator.hasNext() ) {
int s = newItem.getMemoryLimit();
if (boolMemoryAllocation(s)) {
break;
} else
newItem = (ProcessPCB) bwackupPCBIterator.next();
if(newItem == null) break;
}
if(newItem != null) {
int bbase=0;
int llimit = newItem.getMemoryLimit();
rate+=llimit;
jpr.setValue(rate);
bbase = MemoryAllocation(llimit);
ProcessPCB nowItem = new ProcessPCB(newItem.getPID(), newItem.getRequiredTime(), newItem.getPriority(),
"Ready", bbase, llimit);
readyPCB.addItem(nowItem);
backupPCB.removeItem(newItem);
backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems()));
} else {
statuesTextArea.append("内存空间不足!\n");
break;
}
} else break;
}
}
}
sortReadyPCB();
backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems()));
Iterator readyIterator = readyPCB.iterator();
ProcessPCB runningItem = null;
if(readyIterator.hasNext()) {
runningItem = (ProcessPCB) readyIterator.next();
}
if(runningItem != null) {
t = runningItem.getRequiredTime();
TimeSlice = getTimeslice()-1;
for(int i=TimeSlice; i>=0; i--) {
if(t >= 0) {
runningItem.run();
t--;
sortReadyPCB();
if(getFlag() == true)
CPUTextArea.setText("PCB " + runningItem.getPID() + " is running!\n");
try {
Thread.sleep(1000);
} catch (Exception ee) {
ee.printStackTrace();
}
}
} CPUTextArea.setText("");
if(t < 0) {
Flag = false;
statuesTextArea.append("该进程执行完成并且相应的PCB将从内存中移除!\n");
rate-=runningItem.getMemoryLimit();
jpr.setValue(rate);
memoryRecall(runningItem.getMemoryBase(), runningItem.getMemoryLimit());
readyPCB.removeItem(runningItem);
runningItem = null;
sortReadyPCB();
}
if(readyPCB.getNumberOfItems() > 0)
Flag = getFlag();
else Flag = false;
} else Flag = false;
if(getSystemStatus() == 0) {
Flag = false;
break;
} else {
if(getSystemStatus() == 2) {
while(getSystemStatus() == 2) {
try {
Thread.sleep(1000);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
if(getSystemStatus() == 1) Flag = true;
}
}
}
}
/**
* 获取标识符
* @return
*/
public boolean getFlag() {
return this.flag;
}
/**
* 获取系统状态
* @return
*/
public int getSystemStatus() {
return systemStatus;
}
/**
* 监听开始调度按钮
* @author ctong
*
*/
class StartSystemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(readyPCB.getNumberOfItems() > 0) {
if(systemStatus == 0) {
Thread runThread = new RunThread();
flag = true;
runThread.start();
systemStatus = 1;
try {
File f =new File("2.wav");
clip=Applet.newAudioClip(f.toURL());
clip.loop();
}catch (Exception e) {
e.printStackTrace();
}
statuesTextArea.append("调度开始!\n");
} else {
statuesTextArea.append("重置完成!\n");
}
} else statuesTextArea.append("就绪队列为空,请加入进程\n");
}
}
/**
* 系统暂停按钮的监听
* @author ctong
*
*/
class SystemPauseListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(systemStatus == 1) {
flag = false;
systemStatus = 2;
pauseButton.setText("继续(Pause)");
clip.stop();
statuesTextArea.append("暂停!\n");
} else if(systemStatus == 2) {
flag = true;
systemStatus = 1;
pauseButton.setText("暂停(Pause)");
clip.loop();
statuesTextArea.append("系统继续!\n");
} else {
statuesTextArea.append("错误操作!\n");
}
}
}
/**
* 系统重置按钮的监听
* @author ctong
*
*/
class ResetSystemListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(systemStatus != 0) {
clip.stop();
rate=0;
jpr.setValue(0);
flag = false;
systemStatus = 0;
backupPCB = null;
readyPCB = null;
suspendedPCB = null;
memoryItems = null;
backupPCB = new PCBRecords();
readyPCB = new PCBRecords();
suspendedPCB = new PCBRecords();
backupList.setListData(backupPCB.getItemsProperties());
readyList.setListData(readyPCB.getItemsProperties());
suspendList.setListData(suspendedPCB.getItemsProperties());
backupTotalTextField.setText("0");
suspendTotalTextField.setText("0");
CPUTextArea.setText("");
statuesTextArea.setText("");
priorityJRB.doClick();
timeslice = 1;
timesliceJtf.setText("3");
memoryItems = new MemoryRecords();
memoryList.removeAll();
MemoryItem newMemoryItem = new MemoryItem(0,1000);
memoryItems.addItem(newMemoryItem);
memoryList.setListData(memoryItems.getItemsProperties());
statuesTextArea.append("系统重置!\n");
} else {
statuesTextArea.append("错误操作!\n");
}
}
}
/**
* 监听优先级按钮
* @author ctong
*
*/
class priotiryListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
priorityTextField.setEditable(true);
bflag=false;
timeslice = 1;
}
}
/**
* 监听时间片按钮
* @author ctong
*
*/
class timeslicListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
timeslice = getTimesliceText();
bflag=true;
priorityTextField.setText("0");
priorityTextField.setEditable(false);
if(timeslice <= 0) {
statuesTextArea.append("时间片必须是个正整数!\n");
timesliceJtf.setText("3");
timeslice = 1;
}
}
}
/**
* 获取时间片中的数据
* @return
*/
public int getTimesliceText() {
int n;
try {
n = Integer.parseInt(timesliceJtf.getText());
} catch(NumberFormatException numberformatexception) {
statuesTextArea.append("时间片必须是个正整数!\n");
n = -999;
}
return n;
}
}
package CPU;
import java.util.ArrayList;
import java.util.Iterator;
public class MemoryRecords implements Iterable<MemoryItem> {
private ArrayList<MemoryItem> memoryItems;
public Iterator<MemoryItem> iterator() {
// TODO Auto-generated method stub
return this.memoryItems.iterator();
}
public ArrayList<MemoryItem> getMemoryItems() {
return this.memoryItems;
}
public MemoryRecords() {
this.memoryItems = new ArrayList<MemoryItem>();
}
public void addItem(MemoryItem newMemoryItem) {
this.memoryItems.add(newMemoryItem);
}
public void removeItem(MemoryItem momoryItem) {
this.memoryItems.remove(momoryItem);
}
public MemoryItem getMomoryItem(MemoryItem item) {
for(MemoryItem mItem : this.memoryItems) {
if(mItem.equals(item)){
return mItem;
}
}
return null;
}
public MemoryItem getMemoryItem(int base) {
for(MemoryItem mItem : this.memoryItems) {
if(mItem.getMemoryBase() == base) {
return mItem;
}
}
return null;
}
public int getNumberOfItems() {
return this.memoryItems.size();
}
public String[] getItemsProperties() {
String itemsProperties[] = new String[getNumberOfItems()];
int i=0;
for(Iterator iterator1 = this.memoryItems.iterator(); iterator1.hasNext(); ) {
MemoryItem mmItem = (MemoryItem) iterator1.next();
itemsProperties[i++] = mmItem.toString();
}
if(itemsProperties == null) {
itemsProperties[0] = " ";
}
return itemsProperties;
}
}
欢迎留言!!!