addProcess.java
_______________________
import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
public class addProcess extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public JLabel label1,label2,label3;
public JTextField t1,t2,t3;
public JButton b1,b2;
mainFrame mf;
public addProcess(mainFrame f){
super("添加进程");
mf = f;
Container contentPane = getContentPane();
contentPane.setLayout(null);
label1 = new JLabel("进程名");
label2 = new JLabel("运行时间");
label3 = new JLabel("所需内存");
label1.setBounds(50, 20, 100, 50);
label2.setBounds(50, 100, 100, 50);
label3.setBounds(50, 180, 100, 50);
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t1.setBounds(150, 30, 150, 30);
t2.setBounds(150, 110, 150, 30);
t3.setBounds(150, 190, 150, 30);
b1 = new JButton("确定");
b2 = new JButton("取消");
b1.setBounds(80, 320, 100, 30);
b2.setBounds(220, 320, 100, 30);
b1.addActionListener(this);
b2.addActionListener(this);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(t1);
contentPane.add(t2);
contentPane.add(t3);
contentPane.add(b1);
contentPane.add(b2);
this.setBounds(450, 130, 400, 480);
this.setResizable(false);
//this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == b1){
//为新进程分配pid
int pid = mf.reserveQueue.allocatePid;
int needMemory = Integer.parseInt(t3.getText());
mf.reserveQueue.addNewProcess(new PCB(t1.getText(),pid,Double.parseDouble(t2.getText()),needMemory),false);
this.setVisible(false);
mf.event.append(t1.getText() + "进程已加入后备队列\n");
}
if(e.getSource() == b2){
this.setVisible(false);
}
}
}
mainClass.java
____________________________
public class mainClass {
/**
* @param args
*/
public static void main(String[] args) {
mainFrame f = new mainFrame();
Thread t = new Thread(f);
t.start();
}
}
PCB.java
___________________________________________
public class PCB {
public String processName;
public int pid;
public double time;
public double runningTime;
public double priority;
public String state;
public int needMemory;
public int beginMemory;
public int queuePosition = 0;
public PCB(String processName,int pid,double time,double priority,int needMemory){
this.processName = processName;
this.pid = pid;
this.time = time;
this.priority = priority;
this.state = "ready";
this.needMemory = needMemory;
this.beginMemory = -1;
}
public PCB(String processName,int pid,double time,int needMemory){
this.processName = processName;
this.pid = pid;
this.time = time;
this.priority = 1 / time;
this.state = "ready";
this.needMemory = needMemory;
this.beginMemory = -1;
}
public void aging(){
if(priority >= 0.1){
priority = priority - 0.1;
}
}
}