进程调度
来源:互联网 发布:淘宝上好的文具店 编辑:程序博客网 时间:2024/05/14 12:26
package MyProcess;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
public class MyProcess implements Runnable {
private String pcb[][]=new String[100][15];
private JFrame f;
private JPanel panel1,panel2,panel3;
private JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;
public JTable tabel1,tabel2,tabel3,tabel4,tabel5,tabel6,table7,table8;
public int pcbCount=0;
public int type=0;
public String str1[]={"ID","Priority","inTime","totalTime","donetime"};
public int n1=0; //进程表中进程数量
public int n3=0;
public int n4=0;
public int n5=0;
public int n6=0;
public int n7=0;
Boolean start=false;
Thread thread;
public MyProcess(){
f=new JFrame("进程调度");
f.setSize(950, 800);
Container c=f.getContentPane();
c.setBounds(0, 0, 950, 800);
button1=new JButton("创建进程 ");
button1.setBounds(40, 700, 120, 25);
c.add(button1);
button1.addActionListener(new CreateProcessListener() );
button5=new JButton("开始调度");
button5.setBounds(350,700, 120, 25);
c.add(button5);
button5.addActionListener(new StartListener());
button10=new JButton("清除就绪队列");
button10.setBounds(320, 700, 120, 25);
button10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int j=tabel1.getRowCount();
for (int i = 1; i<j; i++) {
deleteTable(tabel1, i);
}
n1=0;
pcbCount=0;
}
}
);
button11=new JButton("清除完成队列");
button11.setBounds(460,700, 120, 25);
button11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int j=tabel3.getRowCount();
for (int i = 1; i<j; i++) {
deleteTable(tabel3, i);
}
n3=0;
pcbCount=0;
}
}
);
button6=new JButton("退出");
button6.setBounds(600,700, 120, 25);
c.add(button6);
button6.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel2=new JPanel();//选择调度算法
JLabel label2=new JLabel("选择一种调度算法:");
button7=new JButton("时间片轮转法");
button7.addActionListener(new TimeListener());
button8=new JButton("优先级算法 ");
button8.addActionListener(new PriorityListener());
button9=new JButton("多级反馈算法");
button9.addActionListener(new SomeLiatener());
label2.setBounds(150,10,120,20);
c.add(label2);
button7.setBounds(270,10,150,25);
c.add(button7);
button8.setBounds(430,10,150,25);
c.add(button8);
button9.setBounds(590,10,150,25);
c.add(button9);
f.add(panel2);
panel3=new JPanel();
tabel1=new JTable(11,5); //就绪队列
JLabel label3=new JLabel("就绪队列");
addTable(tabel1, str1, 0);
tabel2=new JTable(11,5); //正在执行队列
JLabel label4=new JLabel("正在执行队列");
addTable(tabel2, str1, 0);
tabel3=new JTable(11,5); //完成队列
addTable(tabel3, str1, 0);
JLabel label5=new JLabel("完成队列");
label5.setBounds(50,260,130,20);
c.add(label5);
tabel4=new JTable(11,5); //低级反馈队列
addTable(tabel4, str1, 0);
JLabel label6=new JLabel("低级反馈队列");
label6.setBounds(500,260,150,20);
c.add(label6);
tabel5=new JTable(11,5); //中级队列
addTable(tabel5, str1, 0);
JLabel label7=new JLabel("中级队列");
label7.setBounds(50,470,150,20);
c.add(label7);
tabel6=new JTable(11,5); //高级反馈队列
addTable(tabel6, str1, 0);
JLabel label8=new JLabel("高级反馈队列");
label8.setBounds(500,470,150,20);
c.add(label8);
table7=new JTable(11,5); //挂起队列
addTable(table7, str1, 0);
table8=new JTable(11,5); //阻塞队列
addTable(table8, str1, 0);
label3.setBounds(50,40,150,20);
c.add(label3);
tabel1.setBounds(50,70,400,180);
c.add(tabel1);
label4.setBounds(500,40,150,20);
c.add(label4);
tabel2.setBounds(500, 70, 400, 180);
c.add(tabel2);
label4.setBounds(500, 40, 150, 20);
c.add(label4);
tabel3.setBounds(50,285,400,180);
c.add(tabel3);
tabel4.setBounds(500,285,400,180);
c.add(tabel4);
tabel5.setBounds(50,500,400,180);
c.add(tabel5);
tabel6.setBounds(500,500,400,180);
c.add(tabel6);
f.add(panel3,BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
thread=new Thread(this);
thread.start();
}
public static String getTime() {
Date time;
String strtime;
SimpleDateFormat df;
time = new Date();
df = new SimpleDateFormat("HH:mm:ss", Locale
.getDefault());
strtime = df.format(time);
return strtime;
} //获取时间
public void addTable(JTable table, String[] str,int p) {
for (int i = 0; i < str.length; i++) {
table.setValueAt(str[i],p , i);
}
} // 向表格中添加一个进程的数据
public void deleteTable(JTable table,int i) {
for (int j = 0; j < 5; j++) {
table.setValueAt(null, i, j);
}
} //从表格中删除数据
public String[] getTableValue(JTable table ,int i) {
int j=table.getColumnCount();
String s[]=new String[j];
for (int k = 0; k < j; k++) {
s[k]=table.getValueAt(i, k).toString();
}
return s;
} //获得表格中一整行信息
public void sortProcess(JTable table) {
//int r=table.getRowCount();
for(int i=1; i<pcbCount; i++){
for(int j=0; j<pcbCount-i; j++){
if(Integer.parseInt(pcb[j][1])>Integer.parseInt(pcb[j+1][1])){
String temp[]=pcb[j];
pcb[j]=pcb[j+1];
pcb[j+1]=temp;
}
}
} // 对进程表格中所有进程按优先级排序
for(int i=0;i<pcbCount;i++){
String str2[]={pcb[i][0], pcb[i][1],pcb[i][2],pcb[i][3],pcb[i][4]};
addTable(table, str2, i+1);
} //将排好序的进程放到进程队列中
//以上完成 :生成好的进程队列中的所有进程按优先级排序并在进程表中显示
} //把进程按优先级用冒泡排序排序
class CreateProcessListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
int Priority=(int)(Math.random()*5+6);
String inTime= MyProcess.getTime();
int totalTime=(int)(Math.random()*6+1);
//Process p=new Process(ID,Priority,subTime,0,totalTime);
pcb[pcbCount][0]=String.valueOf(pcbCount+1);//进程号
pcb[pcbCount][1]=String.valueOf(Priority);//进程优先级
pcb[pcbCount][2]=inTime; //进程进入时间
pcb[pcbCount][3]=String.valueOf(totalTime);//进程需要的总时间
pcb[pcbCount][4]=String.valueOf(0); //进程已经运行的时间
String array[]=new String[] {
pcb[pcbCount][0],pcb[pcbCount][1],pcb[pcbCount][2],
pcb[pcbCount][3],pcb[pcbCount][4]};
addTable(tabel1, array, n1+1);
pcbCount++;
n1++;
}
catch(Exception e1 ){
String str="最多添加10个进程";
JOptionPane.showMessageDialog(null,str);
}
}
} //实现添加进程按钮的内部类
class PriorityListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
type=1;
}
}//实现优先级算法按钮
public class TimeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
type=2;
}
} //实现时间片轮转算法按钮
public class SomeLiatener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
type=3;
}
}//实现多级反馈算法按钮
public class WakeListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String st[]={String.valueOf(table7.getValueAt(1,0)),
String.valueOf(table7.getValueAt(1,1)),
String.valueOf(table7.getValueAt(1,2)),
String.valueOf(table7.getValueAt(1,3)),
String.valueOf(table7.getValueAt(1,4))};
n1++;
addTable(tabel1, st, n1);
}
} //实现唤醒按钮
public class BlockedListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String strBlocked[]={String.valueOf(tabel2.getValueAt(1,0)),
String.valueOf(tabel2.getValueAt(1,1)),
String.valueOf(tabel2.getValueAt(1,2)),
String.valueOf(tabel2.getValueAt(1,3)),
String.valueOf(tabel2.getValueAt(1,4))};
/*if(String.valueOf(tabel2.getValueAt(1,0))==null){
JOptionPane.showMessageDialog(null, "正在执行队列中没有进程");
}*/
addTable(table7, strBlocked,n7+1);
n7++;
deleteTable(tabel2, 1);}
}//实现挂起按钮
public class StartListener implements ActionListener { //实现开始调度进程
public void actionPerformed(ActionEvent arg0) {
start=true;
String str;
if(type==0){
str="请先选择调度算法";
JOptionPane.showMessageDialog(null, str);}
if(type==1){
str="您选择的是优先级算法";
JOptionPane.showMessageDialog(null, str);}
if(type==2){
str="您选择的是时间片轮转算法";
JOptionPane.showMessageDialog(null, str);}
if(type==3){
str="您选择的是多级反馈算法";
JOptionPane.showMessageDialog(null, str);
} //输出选择的调度算法
}
}
public void run(){
while(true){
if(start==true){ //优先级算法
try{
Thread.sleep(1000);
}catch(InterruptedException e1){}
if(type==1){
sortProcess(tabel1);
while (n1>0) {
String strTime= String.valueOf(tabel1.getValueAt(n1, 3));
int allTime=Integer.parseInt(strTime);
int dt=Integer.parseInt(String.valueOf(tabel1.getValueAt(n1, 4)));
String str3[]={ String.valueOf(tabel1.getValueAt(n1, 0)),
String.valueOf(tabel1.getValueAt(n1, 1)),
String.valueOf(tabel1.getValueAt(n1, 2)),
String.valueOf(tabel1.getValueAt(n1, 3)),
String.valueOf(tabel1.getValueAt(n1, 4))};
//JOptionPane.showMessageDialog(null, "将第"+Integer.parseInt(tabel1.getValueAt(n1, 0).toString())+"进程放到正在运行表");
addTable(tabel2, str3, 1); // 把优先级最高的进程放到正在运行表中,并把进程队列中该进程的有关信息删除
deleteTable(tabel1, n1); //从就绪队列中删除该进程的信息
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
n1--;
int t=Integer.parseInt(String.valueOf(tabel2.getValueAt(1, 4)))+1;//进程已经做过的时间加1
pcb[pcbCount-1][4]=String.valueOf(t);
int p=Integer.parseInt(String.valueOf(tabel2.getValueAt(1, 1)))-1;//进程优先级加1
pcb[pcbCount-1][1]=String.valueOf(p);
String str4[]={ String.valueOf(tabel2.getValueAt(1, 0)),
String.valueOf(p),String.valueOf(tabel2.getValueAt(1,2)),
String.valueOf(tabel2.getValueAt(1, 3)),String.valueOf(t)};
int m=Integer.parseInt(String.valueOf(tabel2.getValueAt(1,0)));
addTable(tabel2, str4, 1);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
//JOptionPane.showMessageDialog(null, m+"进程本次执行结束");
int j=Integer.parseInt(String.valueOf(tabel2.getValueAt(1,3)));
if(t==j){
addTable(tabel3, str4, n3+1);
deleteTable(tabel2, 1);
//JOptionPane.showMessageDialog(null, "进程"+m+"执行完毕放入完成队列,同时清除正在执行列表中的进程");
n3++;
pcbCount--;
System.out.println(tabel2.getValueAt(1, 0));
}//优先级最高的进程执行完毕,将该进程放入完成队列
if(t<j){
n1++;
addTable(tabel1, str4, n1);
sortProcess(tabel1);
deleteTable(tabel2, 1);
//JOptionPane.showMessageDialog(null, "进程"+m+"没有执行完毕放回就绪队列,同时清除正在执行列表中的进程");
}//优先级最高的进程没有执行完毕,将该进程放入就绪队列
}
} //优先级算法
if(type==2){
while(n1>0){
String str3[]={String.valueOf(tabel1.getValueAt(1,0)),
String.valueOf(tabel1.getValueAt(1,1)),
String.valueOf(tabel1.getValueAt(1,2)),
String.valueOf(tabel1.getValueAt(1,3)),
String.valueOf(tabel1.getValueAt(1,4))};
int j=Integer.parseInt(String.valueOf(tabel1.getValueAt(1,0)));
addTable(tabel2, str3, 1); //
//JOptionPane.showMessageDialog(null, j+"进程正在执行");
for(int i=1;i<=n1-1;i++){
String temp[];
temp=getTableValue(tabel1, i+1);
addTable(tabel1, temp, i);
}
deleteTable(tabel1, n1);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
n1--;
int t=Integer.parseInt(String.valueOf(tabel2.getValueAt(1, 4)))+1;//进程已经做过的时间加1
String str4[]={String.valueOf(tabel2.getValueAt(1,0)),
String.valueOf(tabel2.getValueAt(1,1)),
String.valueOf(tabel2.getValueAt(1,2)),
String.valueOf(tabel2.getValueAt(1,3)),
String.valueOf(t)};
int dt=Integer.parseInt(String.valueOf(t));
int at=Integer.parseInt(str3[3]);
addTable(tabel2, str4, 1);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
//JOptionPane.showMessageDialog(null, "进程执行完毕");
if(dt<at){
n1++;
addTable(tabel1, str4, n1);
deleteTable(tabel2, 1);
//JOptionPane.showMessageDialog(null, j+"进程没有执行完送到就绪对列尾部,同时删除执行队列中内容");
}
if(dt==at){
//addTable(tabel3, str4, n1+1);
addTable(tabel3, str4, (n3+1));
n3++;
deleteTable(tabel2, 1);
//JOptionPane.showMessageDialog(null, j+"进程执行完送到完成对列,同时删除执行队列中内容");
}
}
}//时间片轮转算法
if(type==3){
while(n1>0){
sortProcess(tabel1);
for (int i = 1; i <= n1; i++) {
String temp[]=getTableValue(tabel1, i);
addTable(tabel4, temp, i);
}
for(int i=1;i<=n1;i++){
deleteTable(tabel1, i);
}
JOptionPane.showMessageDialog(null, "将就绪队列中的进程按优先级排好顺序后放到低级反馈队列中");
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
n4=n1;
n1=0;
while(n4>0){
String str3[]={
String.valueOf(tabel4.getValueAt(n4,0)),
String.valueOf(tabel4.getValueAt(n4,1)),
String.valueOf(tabel4.getValueAt(n4,2)),
String.valueOf(tabel4.getValueAt(n4,3)),
String.valueOf(tabel4.getValueAt(n4,4))};
addTable(tabel2, str3, 1);
//JOptionPane.showMessageDialog(null, "将第"+Integer.parseInt(tabel4.getValueAt(n4, 0).toString())+"进程放到正在运行表");
deleteTable(tabel4, n4);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
n4--;
int t=Integer.parseInt(String.valueOf(tabel2.getValueAt(1, 4)))+1;//进程已经做过的时间加1
pcb[pcbCount-1][4]=String.valueOf(t);
int p=Integer.parseInt(String.valueOf(tabel2.getValueAt(1, 1)))-1;//进程优先级加1
pcb[pcbCount-1][1]=String.valueOf(p);
String str4[]={ String.valueOf(tabel2.getValueAt(1, 0)),
String.valueOf(p),String.valueOf(tabel2.getValueAt(1,2)),
String.valueOf(tabel2.getValueAt(1, 3)),String.valueOf(t)};
int m=Integer.parseInt(String.valueOf(tabel2.getValueAt(1,0)));
addTable(tabel2, str4, 1);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
//JOptionPane.showMessageDialog(null, m+"进程本次执行完毕");
int j=Integer.parseInt(String.valueOf(tabel2.getValueAt(1,3)));
if(t==j){
addTable(tabel3, str4, n3+1);
deleteTable(tabel2, 1);
//JOptionPane.showMessageDialog(null, "进程"+m+"执行完毕放入完成队列,同时清除正在执行列表中的进程");
n3++;
pcbCount--;
//System.out.println(tabel2.getValueAt(1, 0));
}//优先级最高的进程执行完毕,将该进程放入完成队列
if(t<j){
addTable(tabel5, str4, n5+1);
n5++;
//sortProcess(tabel4);
deleteTable(tabel2, 1);
//JOptionPane.showMessageDialog(null, "进程"+m+"没有执行完毕放到中级反馈队列,同时清除正在执行列表中的进程");
}//优先级最高的进程没有执行完毕,将该进程放入就绪队列
} //把低级反馈队列中执行完毕的进程放到完成队列,没完成的进程放到中级反馈队列中
while(n5>0){
String str5[]={ String.valueOf(tabel5.getValueAt(1, 0)),
String.valueOf(tabel5.getValueAt(1, 1)),
String.valueOf(tabel5.getValueAt(1, 2)),
String.valueOf(tabel5.getValueAt(1, 3)),
String.valueOf(tabel5.getValueAt(1, 4))};
//JOptionPane.showMessageDialog(null, "将中级反馈队列中优先级最高的进程放到正在执行队列");
addTable(tabel2, str5, 1);
int dt=Integer.parseInt(String.valueOf(tabel5.getValueAt(1, 4)))+2;
int at=Integer.parseInt(String.valueOf(tabel5.getValueAt(1, 3)));
int p=Integer.parseInt(String.valueOf(tabel5.getValueAt(1, 1)))-1;
int m=Integer.parseInt(String.valueOf(tabel2.getValueAt(1,0)));
if(dt>at)
dt=at;
for(int i=1;i<=n5-1;i++){
String temp[];
temp=getTableValue(tabel5, i+1);
addTable(tabel5, temp, i);
}
deleteTable(tabel5, n5);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
n5--;
String str6[]={String.valueOf(tabel2.getValueAt(1, 0)),
String.valueOf(p),String.valueOf(tabel2.getValueAt(1,2)),
String.valueOf(at),String.valueOf(dt)};
addTable(tabel2, str6, 1);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
//JOptionPane.showMessageDialog(null, "进程本次执行完毕");
if(dt==at){
n3++;
//JOptionPane.showMessageDialog(null,"进程"+m+"执行完毕放到完成队列中");
addTable(tabel3, str6, n3);
}
if(dt<at){
n6++;
//JOptionPane.showMessageDialog(null, "进程"+m+"没有执行放到高级反馈队列中");
addTable(tabel6, str6, n6);
}
}
while(n6>0){
String str7[]={String.valueOf(tabel6.getValueAt(1, 0)),
String.valueOf(tabel6.getValueAt(1, 1)),
String.valueOf(tabel6.getValueAt(1, 2)),
String.valueOf(tabel6.getValueAt(1, 3)),
String.valueOf(tabel6.getValueAt(1, 4)),};
addTable(tabel2, str7, 1);
//JOptionPane.showMessageDialog(null, "将中级反馈队列中优先级最高的进程放到正在执行队列");
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
//addTable(tabel2, str5, 1);
int dt=Integer.parseInt(String.valueOf(tabel6.getValueAt(1, 4)))+3;
int at=Integer.parseInt(String.valueOf(tabel6.getValueAt(1, 3)));
int p=Integer.parseInt(String.valueOf(tabel6.getValueAt(1, 1)))-1;
int m=Integer.parseInt(String.valueOf(tabel6.getValueAt(1,0)));
//addTable(tabel2, str7, 1);
if(dt>at)
dt=at;
for(int i=1;i<=n6-1;i++){
String temp[];
temp=getTableValue(tabel6, i+1);
addTable(tabel6, temp, i);
}
deleteTable(tabel6, n6);
n6--;
String str6[]={String.valueOf(tabel2.getValueAt(1, 0)),
String.valueOf(p),String.valueOf(tabel2.getValueAt(1,2)),
String.valueOf(at),String.valueOf(dt)};
addTable(tabel2, str6, 1);
try{
Thread.sleep(2000);
}catch(InterruptedException e1){}
//JOptionPane.showMessageDialog(null, "进程执行完毕");
if(dt==at){
n3++;
//JOptionPane.showMessageDialog(null,"进程"+m+"执行完毕放到完成队列中");
addTable(tabel3, str6, n3);
}
if(dt<at){
n6++;
//JOptionPane.showMessageDialog(null, "进程"+m+"没有执行放到高级反馈队列中");
addTable(tabel6, str6, n6);
}
}
deleteTable(tabel2, 1);
}
}
}}
}
public static void main(String[] args) {
new MyProcess();
}
}//结束类的定义