Swing实现:模拟优先级CPU调度与内存管理(1)

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;
}
}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java Swing程序,它可以模拟先来先服务(FCFS)进程调度算法,并在图形化界面中显示进程和CPU状态。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class ProcessScheduler extends JFrame implements ActionListener { private DefaultTableModel model; private JTable table; private JButton button; private JLabel label; private JPanel panel; private int time; private int index; private int[] arrivalTime = {0, 1, 2, 3, 4}; private int[] serviceTime = {4, 3, 5, 2, 4}; private int[] remainingTime; private int[] completionTime; private int[] turnaroundTime; private int[] waitingTime; public ProcessScheduler() { setTitle("Process Scheduler"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 300); setLocationRelativeTo(null); setLayout(new BorderLayout()); model = new DefaultTableModel(new String[] {"Process", "Arrival Time", "Service Time", "Completion Time", "Turnaround Time", "Waiting Time"}, 0); table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane, BorderLayout.CENTER); button = new JButton("Start"); button.addActionListener(this); add(button, BorderLayout.SOUTH); label = new JLabel("Time: 0", SwingConstants.CENTER); add(label, BorderLayout.NORTH); panel = new JPanel(new GridLayout(1, 4)); add(panel, BorderLayout.EAST); panel.add(new JLabel("CPU")); panel.add(new JLabel("Time")); panel.add(new JLabel("Process")); panel.add(new JLabel("Remaining Time")); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { button.setEnabled(false); time = 0; index = 0; remainingTime = serviceTime.clone(); completionTime = new int[arrivalTime.length]; turnaroundTime = new int[arrivalTime.length]; waitingTime = new int[arrivalTime.length]; while (true) { boolean done = true; for (int i = 0; i < arrivalTime.length; i++) { if (arrivalTime[i] <= time && remainingTime[i] > 0) { done = false; remainingTime[i]--; label.setText("Time: " + time); table.setValueAt("CPU", index, 0); table.setValueAt(time, index, 1); table.setValueAt(i + 1, index, 2); table.setValueAt(remainingTime[i], index, 3); if (remainingTime[i] == 0) { completionTime[i] = time + 1; turnaroundTime[i] = completionTime[i] - arrivalTime[i]; waitingTime[i] = turnaroundTime[i] - serviceTime[i]; } index++; } } if (done) { break; } time++; } for (int i = 0; i < arrivalTime.length; i++) { model.addRow(new Object[] {i + 1, arrivalTime[i], serviceTime[i], completionTime[i], turnaroundTime[i], waitingTime[i]}); } } } public static void main(String[] args) { new ProcessScheduler(); } } ``` 这个程序的主要功能是在模拟进程调度算法的过程中更新图形化界面的显示。它使用了JTable来显示进程的属性,用JButton来启动模拟进程调度算法,用JLabel来显示当前的时间,用JPanel来显示CPU的状态。在实现逻辑上,它使用了一个while循环来模拟进程的调度和执行,并使用了一个数组来存储进程的属性。在每个进程执行完成后,它会将该进程的属性添加到JTable中,并计算出该进程的完成时间、周转时间和等待时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值