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

本文介绍了一个使用Java Swing实现的简单图形用户界面(GUI)应用,用于模拟进程管理。该应用允许用户添加新进程,输入进程名称、运行时间和所需内存等参数,并通过按钮触发进程的创建和加入到后备队列中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值