?? cpudd.java
字號:
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 + -