⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpudd.java

📁 java实现的CPU调度算法。。算法比较通俗
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;

class Pcb{//PCB管理类;
private String name;
private int time;
private int priority;
private String state;
private int mem_begin;
private int mem_size;
public Pcb next;
public Pcb(){

}
public Pcb(String name,int time,int priority,String state,int mem_size){
this.name = name;
this.time = time;
this.priority = priority;
this.state = state;
this.mem_size = mem_size;
}
//过去式,今后会被替换;
public Pcb(String name,int time,int priority,String state){
this.name = name;
this.time = time;
this.priority = priority;
this.state = state; 
}

public Pcb(String name,int time,int priority,int mem_size){
this.name = name;
this.time = time;
this.priority = priority;
this.mem_size = mem_size;
}
//will be replace in future;
public Pcb(String name,int time,int priority){
this.name = name;
this.time = time;
this.priority = priority; 
}
public void SetName(String name){
this.name = name;
}
public void SetTime(int time){
this.time = time;
}
public void SetPriority(int priority){
this.priority = priority;
}
public void SetState(String state){
this.state = state;
}
public void Setmem_beg(int begin){
this.mem_begin = begin; 
}
public void Setmem_size(int size){
this.mem_size = size;
}
public String GetName(){
return name;
}
public int GetTime(){
return time;
}
public int GetPriority(){
return priority;
}
public String GetState(){
return state;
}
public int Getmem_beg(){
return mem_begin;
}
public int Getmem_size(){
return mem_size;
}
public void SubTime(){
time--;
}
public void SubPriority(){
priority--;
}

}

class memory{//内存类;
private int mem_begin;
private int mem_size;
private String pcd_name;
private int state;
public memory next;
// public memory pre;
public memory(){
}
public memory(String name,int size){
this.pcd_name = name;
this.mem_size = size;
this.state = 0;
//this.pre = null;
this.next = null;
}
public void Setmem_beg(int begin){
this.mem_begin = begin; 
}
public void Setmem_size(int size){
this.mem_size = size;
}
public void SetPcd_name(String name){
this.pcd_name = name;
}
public void Setmem_state(int state){
this.state = state; 
}
public int Getmem_beg(){
return mem_begin;
}
public int Getmem_size(){
return mem_size;
}
public String GetPcd_name(){
return pcd_name;
}
public int Getmem_state(){
return state;
}

}
class Windows extends WindowAdapter {
//声明窗口容器;
Frame Cpuwin;
List Lready , Lwaiting , Lsuspend ,L_cpus_info,L_memory_table,L_memory_gra;
Button BCpuscheduler,Bgetp_r , BgetP_s ,Bgetp_s_r;
TextField tp_name,tp_time,tp_priority,tp_memory_size,tcpuinfo;
Label p_name , p_time , p_priority ,p_memory_size, 
r_queue , w_queue , s_queue, memory_gra , memory_table,
cpus_info;
Panel p1,p2,p3,p4;
//初试化窗口函数;
public void display(){ 
Cpuwin = new Frame("Cpuscheduler by liuxin");//面板样式;
Cpuwin.setSize(700,400);
Cpuwin.setLocation(300, 140);
Cpuwin.setLayout(new GridLayout(4,1,0,10));
Cpuwin.addWindowListener(this);
Cpuwin.setBackground(Color.gray); 
p_name = new Label("进程名");//第一行,输入进程信息面板;
tp_name = new TextField("NAME", 10);
p_time = new Label("运行时间");
tp_time = new TextField("TIME",2);
p_priority = new Label("优先权");
tp_priority = new TextField("PRT", 3);
p_memory_size = new Label("所需主存大小");
tp_memory_size = new TextField("以K为单位",15);
Bgetp_r = new Button("置入就绪队列");
Bgetp_r.addActionListener(new CpuScheduler());
p1 = new Panel(new FlowLayout(FlowLayout.LEFT));
p1.add(p_name); p1.add(tp_name);
p1.add(p_time); p1.add(tp_time);
p1.add(p_priority); p1.add(tp_priority);
p1.add(p_memory_size); p1.add(tp_memory_size);
p1.add(Bgetp_r);

r_queue = new Label("就绪队列");//第二行,队列名面板;
w_queue = new Label("后备队列");
s_queue = new Label("挂起队列");
memory_gra = new Label("内存状态图");
memory_table = new Label("未分分区表");
p2 = new Panel(new FlowLayout(FlowLayout.LEFT, 50, 0));
p2.add(r_queue);
p2.add(w_queue);
p2.add(s_queue);
p2.add(memory_gra);
p2.add(memory_table);

Lready = new List(6);
Lready.addActionListener(new CpuScheduler());//第三行,队列状态面板;
// Lready.setSize(200, 40);
Lwaiting = new List(6);
// Lwaiting.setSize(200, 40);
Lwaiting.addActionListener(new CpuScheduler());
Lsuspend = new List(6);
Lsuspend.addActionListener(new CpuScheduler());
// Lsuspend.setSize(200, 40);
L_memory_gra = new List(6);
L_memory_gra.addActionListener(new CpuScheduler());
L_memory_table = new List(6);
L_memory_table.addActionListener(new CpuScheduler());

p3 = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
p3.add(Lready);p3.add(Lwaiting);p3.add(Lsuspend);
p3.add(L_memory_gra);p3.add(L_memory_table);

BCpuscheduler = new Button("进行调度");//第四行,调度面板;
BCpuscheduler.addActionListener(new CpuScheduler());
BgetP_s = new Button("挂起");
BgetP_s.addActionListener(new CpuScheduler());
Bgetp_s_r = new Button("解挂");
Bgetp_s_r.addActionListener(new CpuScheduler());
tcpuinfo = new TextField("info", 30);
p4 = new Panel(new FlowLayout(FlowLayout.LEFT, 30, 0));
p4.add(BCpuscheduler);p4.add(BgetP_s);p4.add(Bgetp_s_r);
p4.add(tcpuinfo); 

Cpuwin.add(p1);//窗口面板;
Cpuwin.add(p2);
Cpuwin.add(p3);
Cpuwin.add(p4);
Cpuwin.setVisible(true);

}
public void windowClosing(WindowEvent e)
{System.exit(0);}

} 



public class CpuScheduler implements ActionListener{
static Windows c=new Windows();
static public Pcb ReadyHead;
static public Pcb WaitHead;
static public Pcb SuspendHead;
static public Pcb SuspendEnd;
static public memory MemTable;
static boolean beg = false;
static int ReadyNum = 0;
static int WaitNum = 0;
static int n = 0;
static int m = 0;
static int k = 0;
static int mem = 0; 
static int nowsize = 0;

//建立READY队列;
public void BuildReadyQueue(String name,int time,int priority, String state, int size){
c.tcpuinfo.setText("已经将"+name+"放入就绪队列"); 
Pcb Temp = new Pcb(name, time, priority, state, size);
Pcb Compare_f;
Pcb Compare_b;
Compare_f = ReadyHead;
Compare_b = ReadyHead;
if(ReadyHead == null){
ReadyHead = Temp;
// InitMemory(Temp);
}
else
if(Temp.GetPriority() > ReadyHead.GetPriority())
{
Temp.next = ReadyHead;
ReadyHead = Temp;
// InitMemory(Temp); 
}
else{
while(Compare_b != null && Temp.GetPriority() <= Compare_b.GetPriority()){
Compare_f = Compare_b;
Compare_b = Compare_b.next; 
} 
Compare_f.next = Temp;
Temp.next = Compare_b;
// InitMemory(Temp); 
}
n++; 
}
//建立waiting队列
public void BuildWaitingQueue(String name,int time,int priority){ 
Pcb Temp = new Pcb(name, time, priority);
Pcb Min,R_Temp; 
Pcb W_Compare_f;
Pcb W_Compare_b;
W_Compare_f = WaitHead;
W_Compare_b = WaitHead;
Min = ReadyHead;
R_Temp = ReadyHead;
while(R_Temp != null){
if(Min.GetPriority() > R_Temp.GetPriority())
Min=R_Temp; 
R_Temp=R_Temp.next;
}
if(Min.GetPriority() >= priority){
Temp.SetState("后备");
if(WaitHead == null){
WaitHead=new Pcb(name,time,priority);
WaitHead.SetState("后备");
}
else{
if(Temp.GetPriority() > WaitHead.GetPriority()){
Temp.next = WaitHead;
WaitHead=Temp;
}
else{
while(W_Compare_b != null && W_Compare_b.GetPriority()>=Temp.GetPriority()){
W_Compare_f=W_Compare_b;
W_Compare_b=W_Compare_b.next;
}
W_Compare_f.next=Temp;
Temp.next=W_Compare_b;
}
}
c.tcpuinfo.setText("就绪队列已满,名字为"+name+"的新进程进入后备队列!");
m++;
}
else{
Temp.SetState("就绪");
Insert(Temp);
c.tcpuinfo.setText("就绪队列已满,但由于名为"+name+"的进程优先权高,放入就绪队列");
}
}


public void BuilidSuspendQueue(String name){
Pcb Compare_f;
Pcb Compare_b;
Pcb Temp;
Compare_b=ReadyHead;
Compare_f=ReadyHead;
if(ReadyHead.GetName().equals(name)){
ReleaseMem(ReadyHead);// release the memory;
ReadyHead.SetState("后备");
if(k==0){
SuspendHead = new Pcb(name,ReadyHead.GetTime(),ReadyHead.GetPriority());
SuspendEnd = SuspendHead;
k++;
}
else{
Temp = new Pcb(name,ReadyHead.GetTime(),ReadyHead.GetPriority());
SuspendEnd.next = Temp;
SuspendEnd = SuspendEnd.next;
k++;
}
c.tcpuinfo.setText("已经将名称为"+name+"的进程放入挂起队列中!");
ReadyHead = ReadyHead.next;
n--;
if(WaitHead!=null){
Insert();
n++;
}
}
else{
while(Compare_b != null && (!Compare_b.GetName().equals(name))){
Compare_f = Compare_b;
Compare_b = Compare_b.next;
}
if(Compare_b == null){
c.tcpuinfo.setText("就绪队列中没有找到你想要挂起的名字为"+name+"的进程!");
}
else{
c.tcpuinfo.setText("已经将名称为"+name+"的进程放入挂起队列中!");
ReleaseMem(Compare_b);
ReadyHead.SetState("后备");
if(k==0){
SuspendHead = new Pcb(name,Compare_b.GetTime(),Compare_b.GetPriority());
SuspendEnd =SuspendHead;
k++;
}
else{
Temp = new Pcb(name,Compare_b.GetTime(),Compare_b.GetPriority());
SuspendEnd.next = Temp;
SuspendEnd = SuspendEnd.next;
k++;
}
Compare_f.next = Compare_b.next;
n--;
if(WaitHead!=null){
Insert();
n++;
}
}

} 
}
public void GetSuspendToReady(String name){
Pcb Compare_f;
Pcb Compare_b;
Pcb Temp;
Compare_b = SuspendHead;
Compare_f = SuspendHead;
if(SuspendHead.GetName().equals(name)){
Temp = new Pcb(name,SuspendHead.GetTime(),SuspendHead.GetPriority());
Temp.SetState("就绪");
InitMemory(Temp);// init the momery;
Insert(Temp);
c.tcpuinfo.setText("名称为"+name+"的进程解挂并放入就绪队列中!");
SuspendHead = SuspendHead.next;
k--;
}
else{
while(Compare_b != null && (!Compare_b.GetName().equals(name))){
Compare_f = Compare_b;
Compare_b = Compare_b.next;
}
if(Compare_b == null){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -