SystemMain.java
package operationsystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.*;
import javax.swing.Timer;
import java.util.TimerTask;
/**
*
* @author ASUS
*/
public class SystemMain extends javax.swing.JFrame {
static ProcessCtrl ctrl;
/** Creates new form SystemMain */
public SystemMain() {
initComponents();
ctrl=new ProcessCtrl();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() {
String []io={"禁止IO堵塞","允许IO堵塞"};
ifio=new JComboBox(io);
ifio.addItemListener(iolistener);
time=new Timer(5,bb);
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
Run = new JLabel();
jPanel5 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
WaitRun = new JPanel();
jPanel6 = new javax.swing.JPanel();
jLabel9 = new javax.swing.JLabel();
jPanel10 = new javax.swing.JPanel();
FirstDo = new javax.swing.JRadioButton();
ShortWorkDo = new javax.swing.JRadioButton();
HighPriorityDo = new javax.swing.JRadioButton();
DivideTime = new javax.swing.JRadioButton();
DivideAndHighPriority = new javax.swing.JRadioButton();
jPanel7 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
Wait = new javax.swing.JPanel();
jPanel8 = new javax.swing.JPanel();
jLabel7 = new javax.swing.JLabel();
Complete = new javax.swing.JPanel();
jPanel9 = new javax.swing.JPanel();
HandProcess = new javax.swing.JButton();
RandomProcess = new javax.swing.JButton();
DynamicModifyPriority = new javax.swing.JButton();
DynamicModifyPriority.addActionListener(pri);
Start = new javax.swing.JButton("Start");
getContentPane().setLayout(new java.awt.GridLayout(2, 3));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("frame");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jPanel2.setLayout(new java.awt.BorderLayout());
jPanel2.setBorder(new javax.swing.border.EtchedBorder());
jLabel1.setBackground(new java.awt.Color(102, 102, 255));
jLabel1.setText("/u8fdb/u7a0b/u8fd0/u884c");
jLabel1.setAutoscrolls(true);
jLabel1.setOpaque(true);
jPanel2.add(jLabel1, java.awt.BorderLayout.NORTH);
Run.setBackground(new java.awt.Color(255, 255, 255));
Run.setForeground(new java.awt.Color(102, 102, 255));
jPanel2.add(Run, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel2);
jPanel5.setLayout(new java.awt.BorderLayout());
jPanel5.setBorder(new javax.swing.border.EtchedBorder());
jLabel3.setBackground(new java.awt.Color(102, 102, 255));
jLabel3.setText("/u5c31/u7eea/u961f/u5217");
jLabel3.setOpaque(true);
jPanel5.add(jLabel3, java.awt.BorderLayout.NORTH);
WaitRun.setLayout(new javax.swing.BoxLayout(WaitRun, javax.swing.BoxLayout.Y_AXIS));
Wait.setLayout(new javax.swing.BoxLayout(Wait, javax.swing.BoxLayout.Y_AXIS));
Complete.setLayout(new javax.swing.BoxLayout(Complete, javax.swing.BoxLayout.Y_AXIS));
WaitRun.setBackground(new java.awt.Color(255, 255, 255));
jPanel5.add(WaitRun, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel5);
jPanel6.setLayout(new java.awt.BorderLayout());
jLabel9.setBackground(new java.awt.Color(102, 102, 102));
jLabel9.setText("/u8c03/u5ea6/u7b56/u7565");
jLabel9.setOpaque(true);
jPanel6.add(jLabel9, java.awt.BorderLayout.NORTH);
jPanel10.setLayout(new java.awt.GridLayout(5, 1));
buttonGroup1.add(FirstDo);
FirstDo.setText("/u5148/u6765/u5148/u670d/u52a1");
FirstDo.addActionListener(ae);
jPanel10.add(FirstDo);
buttonGroup1.add(ShortWorkDo);
ShortWorkDo.setText("/u77ed/u4f5c/u4e1a/u4f18/u5148");
ShortWorkDo.addActionListener(ae);
jPanel10.add(ShortWorkDo);
buttonGroup1.add(HighPriorityDo);
HighPriorityDo.setText("/u9ad8/u4f18/u5148/u7ea7/u4f18/u5148(/u9759/u6001)");
HighPriorityDo.addActionListener(ae);
jPanel10.add(HighPriorityDo);
buttonGroup1.add(DivideTime);
DivideTime.setText("/u5206/u65f6");
DivideTime.addActionListener(ae);
jPanel10.add(DivideTime);
jPanel10.add(ifio);
jPanel6.add(jPanel10, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel6);
jPanel7.setLayout(new java.awt.BorderLayout());
jPanel7.setBorder(new javax.swing.border.EtchedBorder());
jLabel5.setBackground(new java.awt.Color(102, 102, 255));
jLabel5.setText("/u7b49/u5f85/u961f/u5217");
jLabel5.setOpaque(true);
jPanel7.add(jLabel5, java.awt.BorderLayout.NORTH);
Wait.setBackground(new java.awt.Color(255, 255, 255));
jPanel7.add(Wait, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel7);
jPanel8.setLayout(new java.awt.BorderLayout());
jPanel8.setBorder(new javax.swing.border.EtchedBorder());
jLabel7.setBackground(new java.awt.Color(102, 102, 255));
jLabel7.setText("/u5b8c/u6210/u8fdb/u7a0b");
jLabel7.setOpaque(true);
jPanel8.add(jLabel7, java.awt.BorderLayout.NORTH);
Complete.setBackground(new java.awt.Color(255, 255, 255));
jPanel8.add(Complete, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel8);
jPanel9.setLayout(new java.awt.GridLayout(4, 1));
HandProcess.setText("/u624b/u52a8/u751f/u6210/u4e00/u4e2a/u8fdb/u7a0b");
HandProcess.setAutoscrolls(true);
HandProcess.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HandProcessActionPerformed(evt);
}
});
jPanel9.add(HandProcess);
RandomProcess.setText("/u968f/u673a/u751f/u6210/u4e00/u4e2a/u8fdb/u7a0b");
RandomProcess.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RandomProcessActionPerformed(evt);
}
});
jPanel9.add(RandomProcess);
DynamicModifyPriority.setText("/u52a8/u6001/u4fee/u6539/u8fdb/u7a0b/u4f18/u5148/u7ea7");
DynamicModifyPriority.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DynamicModifyPriorityActionPerformed(evt);
}
});
jPanel9.add(DynamicModifyPriority);
//Start.setText("/u5f00/u59cb/u8fd0/u884c");
Start.addActionListener(bb);
jPanel9.add(Start);
getContentPane().add(jPanel9);
pack();
}
// </editor-fold>
private void HighPriorityDoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
}
/**
* 随机生成一个进程
*
*/
private void RandomProcessActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
Random generator=new Random();
int priority=(generator.nextInt(15))%10+1;
int ConsultTime=(generator.nextInt(30))%10+2;
Start.setText("");
JTable table=ctrl.createAProcess(priority,ConsultTime);
System.out.println(table);
WaitRun.removeAll();
WaitRun.add(new JScrollPane(table));
WaitRun.setVisible(true);
Start.setText("Start");
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
System.exit(0);// TODO 将在此处添加您的处理代码:
}
private void DynamicModifyPriorityActionPerformed(java.awt.event.ActionEvent evt) {
}
void slow(){
long time=new Date().getTime()+1000;
while(true){
if(new Date().getTime()>time) break;
}
}
boolean Io(){//判断IO端口是否有空,若有空将等待队列清空,入就绪队列
if(noio==1) return true;
Random generator=new Random();
int io=(generator.nextInt(4))%10;
if(io>=1) {ctrl.WaitToRun();return true;}
else return false;
}
//ctrl.createAProcess(Integer.parseInt(v.get(0).toString()),Integer.parseInt(v.get(1).toString()),Integer.parseInt(v.get(3).toString()));
void CtrlIo(Vector v){//使阻碍进程入等待队列
ctrl.goTOWaitQueue(v);
}
ActionListener bb=new ActionListener(){//点击运行后开始监听所有动作
public void actionPerformed(ActionEvent e){
if(e.getSource()==Start){
if(e.getActionCommand().equals("Start"))
{
Complete.removeAll();
ctrl.CompleteQueue.clear();
time.start();
Start.setText("Stop");
}
if(e.getActionCommand().equals("Stop"))
{
time.stop();
Start.setText("Start");
}
}
else if(e.getSource()==time){
Vector v=new Vector(20);
String all="";
while(Io()==false);//若IO端口无空 则一直等待,防止所有进程都人了等待队列,导致就绪队列为空,而直接跳出程序
if(ctrl.ifCanSchedule()==false) { //看等待队列是否为空,若为空退出
WaitRun.removeAll();
Wait.removeAll();
Start.setText("Start");
Run.setText("");
JScrollPane b1=new JScrollPane(ctrl.getWaitRun());
WaitRun.add(b1);
JScrollPane b2=new JScrollPane(ctrl.getWait());
Wait.add(b2);Start.setText("Stop");
Start.setText("Start"); time.stop();return;
}
slow();
//FirstDo算法
if(temp.equals(FirstDo)){v=ctrl.schedule(1);if(Io()==false) CtrlIo(v);else ctrl.CompleteQueue.add(v);}//若IO无空入等待队列
//ShortWorkDo算法
if(temp.equals(ShortWorkDo)) {v=ctrl.schedule(2);if(Io()==false) CtrlIo(v);else ctrl.CompleteQueue.add(v);}//完成后入完成队列
//StaticPriority算法
if(temp.equals(HighPriorityDo)) {v=ctrl.schedule(3);if(Io()==false) CtrlIo(v);else ctrl.CompleteQueue.add(v);}
if(temp.equals(DivideTime)) {v=ctrl.schedule(4);if(Io()==false) CtrlIo(v);else if(ctrl.DivideTemp==1) ctrl.CompleteQueue.add(v);}
//if(temp.equals(DivideAndHighPriority)) v=ctrl.schedule(5);
String aa="运行队列ID:"+v.get(0);
String bb="进程优先级:"+v.get(1);
String cc="进程时间片:"+v.get(2);
String dd="进程消耗时间:"+v.get(3);
all="<html>"+aa+"<br>"+bb+"<br>"+cc+"<br>"+dd+"<br>"+"</html>";
WaitRun.removeAll();
Wait.removeAll();
Complete.removeAll();
Start.setText("Start");
Run.setText(all);
JScrollPane b1=new JScrollPane(ctrl.getWaitRun());
WaitRun.add(b1);
JScrollPane b2=new JScrollPane(ctrl.getWait());
Wait.add(b2);
JScrollPane b3=new JScrollPane(ctrl.getComplete());
Complete.add(b3);
Start.setText("Stop");
}
}
};
ItemListener iolistener=new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(((String)e.getItem()).equals("允许IO堵塞"))
noio=0;
else
noio=1;
}
};
ActionListener pri=new ActionListener(){//动态修改优先级别
public void actionPerformed(ActionEvent e){
JFrame hand=new JFrame();
JPanel pane=new JPanel();
final JTextField id=new JTextField();
final JTextField pri=new JTextField();
JButton set=new JButton();
JLabel label1=new JLabel();
label1.setText("进程ID");
JLabel label2=new JLabel();
label2.setText("优先级");
set.setText("修改优先级");
set.addActionListener(new java.awt.event.ActionListener() {//对修改进行监听
public void actionPerformed(java.awt.event.ActionEvent e) {
int ID=Integer.parseInt((String)id.getText());
int PRI=Integer.parseInt((String)pri.getText());
Vector vv=new Vector();
for(int i=0;i<ctrl.WaitRunQueue.size();i++)
{
vv=(Vector)ctrl.WaitRunQueue.get(i);
if(vv.get(0).equals(new Integer(ID)))
{
vv.set(1,new Integer(PRI));
vv.set(2,new Integer(PRI));
ctrl.WaitRunQueue.set(i,vv);
}
}
JTable table=new JTable();
Start.setText("");
table=ctrl.getWaitRun();
WaitRun.removeAll();
WaitRun.add(new JScrollPane(table));
Start.setText("Start");
}
});
pane.setLayout(new java.awt.GridLayout(3,2));
pane.add(label1);
pane.add(id);
pane.add(label2);
pane.add(pri);
pane.add(set);
hand.getContentPane().add(pane);
hand.pack();
hand.setSize(300,300);
hand.setVisible(true);
}
};
JRadioButton temp=new JRadioButton();//处理单选钮
ActionListener ae = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JRadioButton radio = (JRadioButton) ae.getSource();
temp=radio;
}
};
/**
*创建一个新进程
*/
private void HandProcessActionPerformed(java.awt.event.ActionEvent evt) {
JFrame hand=new JFrame();
final JTextField Priority=new JTextField();
final JTextField ConsultTime=new JTextField();
JTextField other=new JTextField();
JPanel pane=new JPanel();
JButton set=new JButton();
JLabel label1=new JLabel();
label1.setText("优先权");
JLabel label2=new JLabel();
label2.setText("程序消耗时间");
JLabel label3=new JLabel();
label3.setText("其他");
set.setText("创建");
set.addActionListener(new java.awt.event.ActionListener() {//对创建进行监听
public void actionPerformed(java.awt.event.ActionEvent e) {
JTable table;
Start.setText("");
table=ctrl.createAProcess(Integer.parseInt(Priority.getText()), Integer.parseInt(ConsultTime.getText()));
WaitRun.removeAll();
WaitRun.add(new JScrollPane(table));
Start.setText("Start");
}
});
hand.setTitle("设置进程初始参数");
pane.setLayout(new java.awt.GridLayout(4,2));
pane.add(Priority);
pane.add(label1);
pane.add(ConsultTime);
pane.add(label2);
pane.add(other);
pane.add(label3);
pane.add(set);
hand.getContentPane().add(pane);
hand.pack();
hand.setSize(300,300);
hand.setVisible(true);
hand.show();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SystemMain().setVisible(true);
}
});
}
// 变量声明 - 不进行修改
private javax.swing.JPanel Complete;
private javax.swing.JRadioButton DivideAndHighPriority;
private javax.swing.JRadioButton DivideTime;
private javax.swing.JButton DynamicModifyPriority;
private javax.swing.JRadioButton FirstDo;
private javax.swing.JButton HandProcess;
private javax.swing.JRadioButton HighPriorityDo;
private javax.swing.JButton RandomProcess;
private javax.swing.JLabel Run;
private javax.swing.JRadioButton ShortWorkDo;
private javax.swing.JButton Start;
private javax.swing.JPanel Wait;
private javax.swing.JPanel WaitRun;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel10;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPanel jPanel7;
private javax.swing.JPanel jPanel8;
private javax.swing.JPanel jPanel9;
private javax.swing.Timer time;
private JComboBox ifio;
private int noio=1;
// 变量声明结束
}
//******************************************************************************************************
ProcessCtrl.java
package operationsystem;
import java.util.*;
import javax.swing.JTable;
/**
*
* @author ASUS
*/
public class ProcessCtrl {
int policy;//进程调度策略 1代表先来先 2代表短作业优先 3代表高优级优先静态 4代表分时 5代表分时+高优先级优先动态
LinkedList WaitQueue=new LinkedList();//等待队列
LinkedList RunQueue=new LinkedList();//运行队列
LinkedList WaitRunQueue=new LinkedList();//就绪队列
LinkedList CompleteQueue=new LinkedList();//完成队列
int ConsultTime;//程序消耗时间
int ProcessID=1000;//进程ID
int priority;//优先权
int counter;//时间片
/** Creates a new instance of ProcessCtrl */
public ProcessCtrl() {
}
/**
*创建一个新的进程
*/
JTable createAProcess(int priority,int ConsultTime){
this.priority=priority;
this.ConsultTime=ConsultTime;
this.counter=priority;
this.ProcessID=createID();//生成ID
Vector PCB=new Vector(4);//进程控制块 0对应进程ID 1对应进程优先级 2对应进程时间片 3对应进程消耗时间
PCB.add(new Integer(ProcessID));
PCB.add(new Integer(priority));
PCB.add(new Integer(counter));
PCB.add(new Integer(ConsultTime));
//入就绪队列
WaitRunQueue.add(PCB);
//Vector v=(Vector)WaitRunQueue.getFirst();
//Integer k=(Integer)v.get(0);
//int m=k.intValue();
//System.out.println(m);
String [] headings=new String[]{"ID","优先级","时间片","消耗时间"};
Object[][]data=new Object[WaitRunQueue.size()][4];
for(int i=0;i<WaitRunQueue.size();i++){
Vector v=(Vector)WaitRunQueue.get(i);
data[i][0]=(Integer)v.get(0);
data[i][1]=(Integer)v.get(1);
data[i][2]=(Integer)v.get(2);
data[i][3]=(Integer)v.get(3);
}
JTable table=new JTable(data,headings);
return table;//返回填充过信息的表
}
JTable createAProcess(int ProcessID,int priority,int ConsultTime){
this.priority=priority;
this.ConsultTime=ConsultTime;
this.counter=priority;
this.ProcessID=ProcessID;//生成ID
Vector PCB=new Vector(4);//进程控制块 0对应进程ID 1对应进程优先级 2对应进程时间片 3对应进程消耗时间
PCB.add(new Integer(ProcessID));
PCB.add(new Integer(priority));
PCB.add(new Integer(counter));
PCB.add(new Integer(ConsultTime));
//入就绪队列
WaitRunQueue.add(PCB);
//Vector v=(Vector)WaitRunQueue.getFirst();
//Integer k=(Integer)v.get(0);
//int m=k.intValue();
//System.out.println(m);
String [] headings=new String[]{"ID","优先级","时间片","消耗时间"};
Object[][]data=new Object[WaitRunQueue.size()][4];
for(int i=0;i<WaitRunQueue.size();i++){
Vector v=(Vector)WaitRunQueue.get(i);
data[i][0]=(Integer)v.get(0);
data[i][1]=(Integer)v.get(1);
data[i][2]=(Integer)v.get(2);
data[i][3]=(Integer)v.get(3);
}
JTable table=new JTable(data,headings);
return table;//返回填充过信息的表
}
static int id=1000;
static int createID(){//生成ID
id=id+5;
return id;
}
void goTOWaitQueue(Vector v){//入等待队列
WaitQueue.add(v);
}
void WaitToRun(){//等待队列全部入就绪队列
for(int i=0;i<WaitQueue.size();i++){
WaitRunQueue.add((Vector)WaitQueue.remove(i));
}
}
JTable getWaitRun(){//得到WaitRun队列视图
String [] headings=new String[]{"ID","优先级","时间片","消耗时间"};
Object[][]data=new Object[WaitRunQueue.size()][4];
for(int i=0;i<WaitRunQueue.size();i++){
Vector vv=(Vector)WaitRunQueue.get(i);
data[i][0]=(Integer)vv.get(0);
data[i][1]=(Integer)vv.get(1);
data[i][2]=(Integer)vv.get(2);
data[i][3]=(Integer)vv.get(3);
}
JTable table=new JTable(data,headings);
return table;//返回填充过信息的表
}
JTable getWait(){//得到Wait队列视图
String [] headings=new String[]{"ID","优先级","时间片","消耗时间"};
Object[][]data=new Object[WaitQueue.size()][4];
for(int i=0;i<WaitQueue.size();i++){
Vector vv=(Vector)WaitQueue.get(i);
data[i][0]=(Integer)vv.get(0);
data[i][1]=(Integer)vv.get(1);
data[i][2]=(Integer)vv.get(2);
data[i][3]=(Integer)vv.get(3);
}
JTable table=new JTable(data,headings);
return table;//返回填充过信息的表
}
JTable getComplete(){//得到Complete队列视图
String [] headings=new String[]{"ID","优先级","时间片","消耗时间"};
Object[][]data=new Object[CompleteQueue.size()][4];
for(int i=0;i<CompleteQueue.size();i++){
Vector vv=(Vector)CompleteQueue.get(i);
data[i][0]=(Integer)vv.get(0);
data[i][1]=(Integer)vv.get(1);
data[i][2]=(Integer)vv.get(2);
data[i][3]=(Integer)vv.get(3);
}
JTable table=new JTable(data,headings);
return table;//返回填充过信息的表
}
Vector schedule(int policy){
this.policy=policy;
Vector v=new Vector();
switch(policy){
case 1: v=FirstDo();break;
case 2:v=ShortWork();break;
case 3:v=StaticPriority();break;
case 4:v=DivideTime();break;
//case 5:DivideAndDynamicPriority();break;
}
return v;
}
boolean can=true;//返回是否还能再运行,队列为空停止运行
boolean ifCanSchedule(){
if(WaitRunQueue.size()>0) can=true;
else can=false;
return can;
}
Vector FirstDo(){
Vector v=(Vector)WaitRunQueue.removeFirst();
return v;
}
Vector ShortWork(){
Integer temp1=new Integer("1000");
int k=0;
for(int i=0;i<WaitRunQueue.size();i++)
{
Vector v=(Vector)WaitRunQueue.get(i);
Integer gg=(Integer)v.get(3);
if(gg.compareTo(temp1)<0) {temp1=gg; k=i;}
}
return (Vector)WaitRunQueue.remove(k);
}
Vector StaticPriority(){
Integer temp1=new Integer("0");
int k=0;
for(int i=0;i<WaitRunQueue.size();i++)
{
Vector v=(Vector)WaitRunQueue.get(i);
Integer gg=(Integer)v.get(1);
if(gg.compareTo(temp1)>0) {temp1=gg; k=i;}
}
return (Vector)WaitRunQueue.remove(k);
}
int DivideTemp=0;
Vector DivideTime(){
Vector v=(Vector)WaitRunQueue.getFirst();
Integer ConsultTime=(Integer)v.get(3);
Integer counter=(Integer)v.get(2);
if(counter.compareTo(ConsultTime)>=0)
{
v.add(2,new Integer(0));
v.add(3,new Integer(0));
DivideTemp=1;
return (Vector)WaitRunQueue.removeFirst();
}
else
{
int k=ConsultTime.intValue();
int x=counter.intValue();
v.add(3,new Integer(k-x));
WaitRunQueue.add(v);
DivideTemp=0;
return (Vector)WaitRunQueue.removeFirst();
}
}
}