/**
单 位:中南大学软件学院0501班
文 件:CpuScheduler.java
项 目:操作系统cpu调度算法模拟程序
作 者:刘欣
创建时间:2007年5月14日
**/
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){
c.tcpuinfo.setText("挂起队列中没有找到你想要解挂的名字为"+name+"的进程!");
}
else{
Temp = new Pcb(name,Compare_b.GetTime(),Compare_b.GetPriority());
Temp.SetState("就绪");
Insert(Temp);
InitMemory(Temp);// init the momery;
c.tcpuinfo.setText("已经将名称为"+name+"的进程解挂并放入就绪队列中!");
Compare_f.next = Compare_b.next;
k--;
}
}
}
public static void InitMem(){
MemTable = new memory();
MemTable.Setmem_beg(0);
MemTable.Setmem_size(100);
MemTable.Setmem_state(0);
}
public boolean InitMemory(Pcb temp){
memory mem_temp,last,now;
last = MemTable;
mem_temp = new memory(temp.GetName(),temp.Getmem_size());
boolean flag = true;
if(mem_temp.Getmem_size() > 100 ){
c.tcpuinfo.setText(mem_temp.GetPcd_name()+"is too big");
flag = false;
}
if(MemTable == null){
return flag;
}
if(mem_temp.Getmem_size() + nowsize > 100){
c.tcpuinfo.setText("memory has been full please wait");
flag = false;
}
if(MemTable.Getmem_state() == 0){// if the first is empty;
if(MemTable.Getmem_size() > mem_temp.Getmem_size()){
mem_temp.next = MemTable;
mem_temp.Setmem_beg( last.Getmem_beg() );
mem_temp.Setmem_state(1);
MemTable.Setmem_beg(mem_temp.Getmem_beg() + mem_temp.Getmem_size());
MemTable.Setmem_size( MemTable.Getmem_size()-mem_temp.Getmem_size() );
MemTable = mem_temp;
nowsize += mem_temp.Getmem_size();
return flag;
}
if (MemTable.Getmem_size() == mem_temp.Getmem_size()){
// MemTable.SetPcd_name(mem_temp.GetPcd_name());
mem_temp = MemTable;
mem_temp.Setmem_state(1);
mem_temp.next = MemTable;
nowsize += mem_temp.Getmem_size();
return flag;
}
}
// begin obtain the other;
if(last != null){
now = last.next;
while(now != null){// search the teble for sutible memory;
if(now.Getmem_state() == 0){// if the first is empty;
if(now.Getmem_size() > mem_temp.Getmem_size()){
mem_temp.next = now;
mem_temp.Setmem_beg( now.Getmem_beg() );
mem_temp.Setmem_state(1);
now.Setmem_beg(mem_temp.Getmem_beg() + mem_temp.Getmem_size());
now.Setmem_size( now.Getmem_size()-mem_temp.Getmem_size() );
last.next = mem_temp;
nowsize += mem_temp.Getmem_size();
return flag;
}
else if (now.Getmem_size() == mem_temp.Getmem_size()){
now.SetPcd_name(mem_temp.GetPcd_name());
mem_temp = now;
mem_temp.Setmem_state(1);
last.next = mem_temp;
nowsize += mem_temp.Getmem_size();
return flag;
}
}
last = now;
// if(last != null){
now = now.next;
// }
}
}
return flag;
}
public void ReleaseMem(Pcb Temp){
memory mem_temp,last,now;
mem_temp = new memory(Temp.GetName(),Temp.Getmem_size());
if(MemTable == null){
c.tcpuinfo.setText("无内存可释放");
// return false;
}
last = MemTable;
now = last.next;
if (MemTable.GetPcd_name() == mem_temp.GetPcd_name()){//如果第一个就是要释放的分区;
MemTable.Setmem_state(0);
MemTable.SetPcd_name(null);
if(now != null && now.Getmem_state() == 0 ){//如果后邻接分区也是空闲的;
MemTable.Setmem_size(MemTable.Getmem_size() + now.Getmem_size());
MemTable.SetPcd_name(null);
nowsize -= mem_temp.Getmem_size();
MemTable.next = null;
// return true;
}
}
while (now != null){//在链表中寻找要释放的分区;
if(now.GetPcd_name() == mem_temp.GetPcd_name()){//找到;
nowsize -= mem_temp.Getmem_size();
now.Setmem_state(0);
if(now.next != null && now.next.Getmem_state() == 0){//查看后面相邻结点是否有空闲;
last.next = now.next;
now.next.Setmem_beg(now.Getmem_beg());
now.next.Setmem_size(now.Getmem_size() + now.next.Getmem_size());
now = last.next;
now.SetPcd_name(null);
}
if(last.Getmem_state() == 0){//查看前面相邻结点是否有空闲;
last.next = now.next;
last.Setmem_size(last.Getmem_size() + now.Getmem_size());
last.SetPcd_name(null);
now = last.next;
// now.SetPcd_name(null);
}
}
last = now;
now = now.next;
}
}
public void Schedule(){
c.tcpuinfo.setText("对进程进行一轮调度!");
Pcb Temp;
Temp = ReadyHead;
while(Temp != null){
Temp.SubTime();
if(Temp.GetTime() == 0){
c.tcpuinfo.setText("就绪队列中名为"+Temp.GetName()+"的进程已经运行完!");
Delete(Temp);
n--;
if(WaitHead != null){
c.tcpuinfo.setText("从后备队列中添加新进程到就绪队列中!");
Insert();
n++;
}
}
if(Temp.GetPriority() != 0){
Temp.SubPriority();
}
Temp = Temp.next;
}
}
//输出READY队列信息;
public void PrintCpuinfo_ready(){
Pcb Temp;
int row = 0;
Temp = ReadyHead;
c.Lready.removeAll();
while(Temp != null && row < 6){
String str = " " + "name:" + Temp.GetName() + "time" + Temp.GetTime() + "priority" + Temp.GetPriority();
c.Lready.add(str);
Temp = Temp.next;
row++;
}
}
//output the wait;
public void PrintCpuinfo_wait(){
Pcb Temp;
int row = 0;
Temp = WaitHead;
c.Lwaiting.removeAll();
PrintCpuinfo_ready();
while(Temp != null && row < 6){
String str = " " + "name:" + Temp.GetName() + "time" + Temp.GetTime() + "priority" + Temp.GetPriority();
//System.out.println(str);
c.Lwaiting.add(str);
Temp = Temp.next;
row++;
}
}
//out put the memory;
public void PrintMemoryinfo(){
memory Temp,Temp1;
Temp = MemTable;
Temp1 = MemTable;
c.L_memory_gra.removeAll();
c.L_memory_table.removeAll();
while(Temp != null /*&& Temp.Getmem_state() == 1*/){
String str = " " + "name:" + Temp.GetPcd_name() + "begin" + Temp.Getmem_beg() + "size" + Temp.Getmem_size();
//if(Temp.Getmem_state() == 1){
c.L_memory_gra.add(str);
//}
Temp = Temp.next;
}
while(Temp1 != null/* && Temp1.Getmem_state() == 0*/){
String str1 = "begin" + Temp1.Getmem_beg() + "size" + Temp1.Getmem_size() + "状态" +Temp1.Getmem_state();
if(Temp1.Getmem_state() == 0){
c.L_memory_table.add(str1);
}
Temp1 = Temp1.next;
}
}
public void PrintCpuinfo_all(){
Pcb Temp;
int row = 0;
Temp = SuspendHead;
c.Lready.removeAll();
c.Lwaiting.removeAll();
PrintCpuinfo_ready();
PrintCpuinfo_wait();
while(Temp != null && row < 6){
String str = " " + "name:" + Temp.GetName() + "time" + Temp.GetTime() + "priority" + Temp.GetPriority();
//System.out.println(str);
c.Lsuspend.add(str);
Temp = Temp.next;
row++;
}
}
//删除READY中被替换的结点;
public void Delete(Pcb Temp){
ReleaseMem(Temp);
Pcb Compare_f;
Pcb Compare_b;
Compare_b = ReadyHead;
Compare_f = ReadyHead;
if(ReadyHead == Temp){
ReadyHead = ReadyHead.next;
}
else{
while(Compare_b != null && Compare_b != Temp){
Compare_f = Compare_b;
Compare_b = Compare_b.next;
}
Compare_f.next = Compare_b.next;
}
//System.out.println(Temp.GetName());
}
//插入新结点;
public void Insert(){
Pcb Temp;
Temp = WaitHead;
Temp.SetState("就绪");
WaitHead = WaitHead.next;
Pcb Compare_f;
Pcb Compare_b;
Compare_b = ReadyHead;
Compare_f = ReadyHead;
if(Temp.GetPriority() > ReadyHead.GetPriority() ){
Temp.next = ReadyHead;
ReadyHead = Temp;
}
else{
while(Compare_b != null && Compare_b.GetPriority() >= Temp.GetPriority() ){
Compare_f = Compare_b;
Compare_b =Compare_b.next;
}
Compare_f.next = Temp;
Temp.next = Compare_b;
}
String str = " " + "name:" + Temp.GetName() + "time" + Temp.GetTime() + "priority" + Temp.GetPriority();
System.out.println(str);
m--;
}
//插入新结点;
public void Insert(Pcb Temp){
Pcb R_Temp;
Pcb Min;
Pcb Compare_f;
Pcb Compare_b;
Pcb comp;
Compare_b=ReadyHead;
Compare_f=ReadyHead;
R_Temp=ReadyHead;
Min=ReadyHead;
if(n>=5){
while(R_Temp != null){
if(Min.GetPriority() > R_Temp.GetPriority())
Min=R_Temp;
R_Temp=R_Temp.next;
}
comp=new Pcb(Min.GetName(),Min.GetTime(),Min.GetPriority());
n--;
// String str = "comp" + "name:" + comp.GetName() + "time" + comp.GetTime() + "priority" + comp.GetPriority();
BuildWaitingQueue(comp.GetName(),comp.GetTime(),comp.GetPriority());
m++;
Delete(Min);
}
if(Temp.GetPriority() > ReadyHead.GetPriority()){
Temp.next = ReadyHead;
ReadyHead = Temp;
n++;
}
else{
while(Compare_b != null && Compare_b.GetPriority() >= Temp.GetPriority()){
Compare_f=Compare_b;
Compare_b=Compare_b.next;
}
Compare_f.next=Temp;
Temp.next = Compare_b;
n++;
}
}
String GetSuspendName(){
int index = c.Lready.getSelectedIndex();
String a = c.Lready.getItem(index);
c.Lready.remove(index);
System.out.println(a);
String b = a.substring(6, 8);
System.out.println(b);
return b;
}
String GetReleaseSuspendName(){
int index = c.Lsuspend.getSelectedIndex();
String a = c.Lsuspend.getItem(index);
c.Lsuspend.remove(index);
System.out.println(a);
String b = a.substring(6, 8);
System.out.println(b);
return b;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == c.Bgetp_r){
String name;
int time;
int priority;
int size;
name = c.tp_name.getText().trim();
String t = c.tp_time.getText().trim();
String p = c.tp_priority.getText().trim();
String s = c.tp_memory_size.getText().trim();
c.tp_name.setText("");
c.tp_time.setText("");
c.tp_priority.setText("");
c.tp_memory_size.setText("");
time = Integer.parseInt(t);
priority = Integer.parseInt(p);
size = Integer.parseInt(s);
if(ReadyNum < 6){
Pcb Temp = new Pcb(name, time, priority, "就绪", size);
boolean flag = InitMemory(Temp);
if(flag == true){
BuildReadyQueue(name,time,priority,"就绪",size);
PrintCpuinfo_ready();
PrintMemoryinfo();
ReadyNum++;
}
else
c.tcpuinfo.setText("内存满");
}
else{
if(WaitNum < 6){
BuildWaitingQueue(name,time,priority);
PrintCpuinfo_wait();
WaitNum++;
}
}
}
if(e.getSource() == c.BCpuscheduler){
Schedule();
PrintCpuinfo_wait();
PrintMemoryinfo();
}
if(e.getSource() == c.BgetP_s){
String name = GetSuspendName();
BuilidSuspendQueue(name);
PrintCpuinfo_all();
PrintMemoryinfo();
}
if(e.getSource() == c.Bgetp_s_r){
String name = GetReleaseSuspendName();
GetSuspendToReady(name);
PrintCpuinfo_all();
PrintMemoryinfo();
}
}
public static void main(String arg[]){
InitMem();
c.display();
}
}