目录

1 基本信息... 2

2 实验内容与目的... 2

2.1 实验内容:2

2.2 实验目的... 2

3 主要设计思路和流程图... 3

3.1 算法流程图... 3

3.2 系统设计架构... 4

3.3 主要UI设计... 5

4 主要数据结构及其说明... 6

4.1 PCB. 6

4.2 ProQueueBase. 7

5 程序运行时的初值和运行结果... 7

5.1 系统随机生成初值测试... 7

5.2 手动输入测试... 8

5.3 文件输入测试... 9

附录... 10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 基本信息

l  项目名称: 模拟进程调度管理系统

l  完成人姓名:

l  学号:

l  完成日期: 2017年5月20日

2 实验内容与目的

2.1 实验内容:

     编写程序完成单处理器系统的进程调度,要求采用基于时间片多队列反馈式调度策略调度策略。具体内容:

l  确定PCB内容及其组织方式。

l  要求模拟进程空闲(新)、就绪、运行、阻塞和完成5个状态。

l  实现进程创建、进程调度、进程阻塞、进程唤醒和进程撤销5个原语。

l  能够模拟进程从生到灭的完整过程。

2.2 实验目的

l  加深进程概念理解,明确进程与程序区别。

l  理解操作系统中进程的组织、创建和调度等方法。

 

3 主要设计思路和流程图

3.1 算法流程图

操作系统:模拟进程调度管理系统


图1 多级反馈队列调度算法流程图

3.2 系统设计架构

操作系统:模拟进程调度管理系统

图2 系统设计包图

3.3 主要UI设计

3.3.1 就绪队列参数配置

操作系统:模拟进程调度管理系统

3 就绪参数配置

3.3.2 输入参数及容错提示

操作系统:模拟进程调度管理系统

4 输入进程及容错提示

4 主要数据结构及其说明

4
.1 PCB


操作系统:模拟进程调度管理系统

图5 PCB类图

4.2 ProQueueBase

     ProQueueBase 是封装好的队列,NewQueue(新建队列)、ReadyQueue(就绪队列)、BlockQueue(挂起队列)都继承了它。

操作系统:模拟进程调度管理系统

图6 队列基类类图

5 程序运行时的初值和运行结果

5.1 系统随机生成初值测试

操作系统:模拟进程调度管理系统

图7 系统随机生成进程

操作系统:模拟进程调度管理系统

图8 系统随机生成测试结果

总运行时间:36

5.2 手动输入测试

操作系统:模拟进程调度管理系统

图9 手动输入进程

操作系统:模拟进程调度管理系统

图10 手动输入进程挂起展示

操作系统:模拟进程调度管理系统

图11 手动输入测试结果

总运行时间:57

5.3 文件输入测试

  文件输入中,数据格式为:进程名、优先级、总需时间、是否发送挂起、到达时间

操作系统:模拟进程调度管理系统

图12 文件输入数据

操作系统:模拟进程调度管理系统

图13 文件输入进程

操作系统:模拟进程调度管理系统

图14 文件输入测试结果

总运行时间:103

附录

由于源码较多只放了重要代码

/src/business/ ScheduProcessByMF

packagebusiness;

 

importjava.2awt.event.Mo3useEvent;

im4portjava.awt.event.MouseListener;

importjava.util.Queue;

 

importjavax.swing.JTable;

 

importorg.omg.CORBA.PRIVATE_MEMBER;

 

importdata.BlockQueue;

importdata.NewQueue;

importdata.PCB;

importdata.ReadyQueue;

 

publicclass ScheduProcessByMF extends Thread{

 

         private ReadyQueue[] readyQueue;          //多就绪队列

         private NewQueue newQueue;                //新建队列

         private BlockQueue blockQueue;            //挂起队列

 

         private JTable statusTable;               //进程显示条

        

         private int SYS_TIME;                     //模拟系统时

         private int current_i;                    // 当前就绪队列

         PCB pcb_run_tp = null;                    // 就绪队列中的进程临时变量

         public boolean exit =false;               //用户线程管理

        

         public ScheduProcessByMF(JTablestatusTable,ReadyQueue[] readyQueue,NewQueue newQueue) {

                   // TODO Auto-generatedconstructor stub

                   this.readyQueue = readyQueue;

                   this.statusTable =statusTable;

                   this.newQueue = newQueue;

            this.blockQueue = new BlockQueue(30);

            System.out.println("gouzao....."+newQueue.getSize()+"/"+newQueue.getCap());

         }

 

         @Override

         public void run() {

                   while(!exit)

                   {

                            SYS_TIME = 0;                                                // 系统时初始化

                            //      PCB pcb_new_tp = null;                                        // 新建队列中的进程临时变量

                                     PCBpcb_ready_tp = null;                                      // 就绪队列中的进程临时变量

                                    

                                     inttime_round = -1;                                          // 时间片

                                     booleanhasFinished = false;                                  // 进程是否执行完

                            //      boolean hasweak = false;                                      // 进程是否唤醒

                                    

                                    

                                     while (true){

                                                 try {                                                   // 模拟系统运行1S

                                                                 Thread.sleep(1000);

                                                        }catch (InterruptedException ex) {

                                                                 System.out

                                                                                    .println("InterruptedExceptionin ProgressBarThread");

                                                        }

                                                 SYS_TIME++;                                             //系统时+1

                                                 System.out.println("SYS_TIME....."+SYS_TIME);

                                                 /**

                                                  * new -> ready

                                                  */

                                                 NewQueueToReadyQueue();                                   // 加入就绪队列

                                                 /**

                                                  * ready->run

                                                  */

                                                

                                                 if(pcb_run_tp==null)                                      // 如果当前运行的进程为空

                                                 {

                                                         

                                                          pcb_run_tp = getNextPCB();                            // 获取下一个要执行的进程

                                                        //  System.out.println("获取下一个要执行的进程....."+pcb_run_tp.getName());

                                                          if(pcb_run_tp!=null)

                                                          {

                                                                   time_round = getTimeRound(pcb_run_tp);            // 获取时间片大小 

                                                                   statusTable.setValueAt("运行",pcb_run_tp.local , 1);

                                                                   hasFinished = false;              

                                                          }

                                                          else                                                  // 所有进程执行完毕

                                                          {}

                                                 }

                                                 else

                                                 {

                                                          pcb_run_tp.r_time++;                                      // 已运行时间+1

                                                          time_round--;                                             //时间片-1

                                                          System.out.println("时间片....."+time_round);

                                                          statusTable.setValueAt("运行",pcb_run_tp.local , 1);

                                                         statusTable.setValueAt(pcb_run_tp.getR_time(), pcb_run_tp.local,4);  //动态更改显示已运行时间

                                                         statusTable.setValueAt((pcb_run_tp.r_time)                  //动态更改运行状态条

                                                                                    *100

                                                                                    /pcb_run_tp.need_time, pcb_run_tp.local,5);

                                                         

                                                          current_i = pcb_run_tp.queue;

                                                 }

                                                 System.out.println("出来时间片....."+time_round);

                                                 /**

                                                  * run->ready

                                                  */

                                                 if(time_round==0 && !hasFinished)                                      //时间片用完但进程没执行完

                                                 {

                                                          time_round = -1;

                                                          RunningToReady(pcb_run_tp);                         //将进程放入就绪队列

                                                     continue;

                                                 }

                                                

                                                 /**

                                                  * run->block

                                                  * 可能挂起的进程以一个较大的概率挂起

                                                  */

                                                 if(!hasFinished)

                                                 {

                                                          if(requestForBlock(pcb_run_tp)&&!pcb_run_tp.hasblocked)    //是否发起挂起

                                                          {

                                                                   time_round = -1;

                                                                   RunningToBlock(pcb_run_tp);                        //将进程添加至挂起队列

                                                                   continue;

                                                          }

                                                 }

                                    

                                                 statusTable.addMouseListener(newMyMouseListener(pcb_run_tp)); //通过鼠标双击挂起进程

                                                 if(pcb_run_tp == null) continue;

                                                 /**

                                                  * block->ready

                                                  */

                                                            BlockToReady();                                       // 将挂起进程添加至就绪队列 

                                                 /**

                                                  * run->exit

                                                  */

                                                

                                                if((pcb_run_tp!=null)&&pcb_run_tp.getR_time()>=pcb_run_tp.getNeed_time())     //已运行时间==总需时间

                                                 {

                                                            hasFinished = true;

                                                            time_round = -1;

                                                            RunningToFinish(pcb_run_tp);                        // 进程结束

                                                           

                                                 }

                                     }

                                    

                   }

        

                  

         }

 

 

         @Override

         @Deprecated

         public void destroy() {

                   // TODO Auto-generated methodstub

                   super.destroy();

         }

 

         private void RunningToFinish(PCBpcb_tp) {

                   // TODO Auto-generated methodstub

                    statusTable.setValueAt("结束",pcb_tp.local , 1);

                    pcb_run_tp = null;

         }

 

         private void BlockToReady() {

                   // TODO Auto-generated methodstub

                   PCB pcb_tp=null;

                   while((pcb_tp =blockQueue.readHead())!=null && hasRoom()&& pcb_tp.block_time<= SYS_TIME)

                   {

                      readyQueue[0].add(blockQueue.getHead());

                      pcb_tp.queue = 0;

                      statusTable.setValueAt("就绪",pcb_tp.local , 1);

                   }

         }

 

         private boolean hasRoom() {

                   // TODO Auto-generated methodstub

                   if(readyQueue[0].getSize()< readyQueue[0].getCap())

                            return true;

                   return false;

         }

 

         private void RunningToBlock(PCB pcb_tp){

                   // TODO Auto-generated methodstub

                    statusTable.setValueAt("挂起",pcb_tp.local , 1);

                   pcb_tp.hasblocked = true;

                    blockQueue.add(pcb_tp);

                    pcb_run_tp=null;

         }

   /**

    * 产生一个基于总需时间的随机概率

    * @param pcb_tp

    * @return

    */

         private boolean requestForBlock(PCBpcb_tp) {

                   // TODO Auto-generated methodstub

                   int gailv =  (int) (Math.random() *pcb_tp.getNeed_time());     //随机发起

                   if(pcb_tp.block &&gailv>pcb_tp.getNeed_time()/3)

                   {

                              pcb_tp.block_time = SYS_TIME+(int)(Math.random() * 6);//产生一个随机挂起时间

                              return true;

                   }

                   else

                      return false;

         }

 

         private void RunningToReady(PCB pcb_tp){

                   // TODO Auto-generated methodstub

                   if(pcb_tp.queue ==readyQueue.length-1)            // 如果进程在最低级队列

                   {

                            readyQueue[pcb_tp.queue].add(pcb_tp);         // 则加在本队列

           

                   }

                   else                                              // 否则加在下一级队列

                   {

                            readyQueue[pcb_tp.queue+1].add(pcb_tp);

                            pcb_tp.queue =pcb_tp.queue+1;

                   }

                     statusTable.setValueAt("就绪",pcb_tp.local , 1);

                     pcb_run_tp = null;

         }

 

         private int getTimeRound(PCB pcb_tp) {

                   // TODO Auto-generated methodstub

                   returnreadyQueue[pcb_tp.queue].getTime_round();

         }

 

         private PCB getNextPCB() {

                   // TODO Auto-generated methodstub

                   PCB pcb_tp=null;

                  

                   for(inti=0;i<readyQueue.length;i++)

                   {

                            if((pcb_tp=readyQueue[i].getHead())!=null)

                            {

                                     returnpcb_tp;

                            }

                   }

                   return null;

         }

 

         private void NewQueueToReadyQueue() {

                   // TODO Auto-generated methodstub

                   while(true)

                   {

                             PCB pcb_new_tp = newQueue.readHead();                        // 读新建队列第一个进程但不弹出

                            System.out.println("newto....."+newQueue.getSize()+"/"+newQueue.getCap());

                             if(pcb_new_tp == null)

                             {

                                      System.out.println(".......新建为空");

                                      break;

                             }

                             else if(pcb_new_tp.arr_time <= SYS_TIME&& hasRoom()){           // 如果进程到达时间等于当前系统时且就绪队列有空间

                                       readyQueue[0].add(pcb_new_tp);                              // 加入就绪队列

                                       newQueue.getHead();                                         // 弹出新建队列

                                       statusTable.setValueAt("就绪",pcb_new_tp.local , 1);

                                       pcb_new_tp.queue = 0;

                                      

                              }  

                              else if(pcb_new_tp.arr_time <= SYS_TIME )

                              {

                                       statusTable.setValueAt("新建",pcb_new_tp.local , 1);

                              }

                              else{

                                     break;

                            }

                   }

                  

         }

 

 class MyMouseListener implementsMouseListener{

 

          PCB pcb_tp;

         public MyMouseListener(PCB pcb_tp) {

                   // TODO Auto-generatedconstructor stub

                   this.pcb_tp = pcb_tp;

         }

 

         public void mouseClicked(MouseEvent e){

                   // TODO Auto-generated methodstub

                   if(e.getClickCount() == 2){

                             int row =statusTable.rowAtPoint(e.getPoint());

                              RunningToBlock(pcb_run_tp);                        //将进程添加至挂起队列

                              pcb_run_tp=null;

                   }

         }

 

         public void mouseEntered(MouseEventarg0) {

                   // TODO Auto-generated methodstub

                  

         }

 

         public void mouseExited(MouseEventarg0) {

                   // TODO Auto-generated methodstub

                  

         }

 

         public void mousePressed(MouseEventarg0) {

                   // TODO Auto-generated methodstub

                  

         }

 

         public void mouseReleased(MouseEventarg0) {

                   // TODO Auto-generated methodstub

                  

         }

 }

 

}

/src/data/PCB

package data;

 

import javax.swing.JProgressBar;

 

publicclass PCB {

 

    publicstaticfinalintNEW=0;

    publicstaticfinalintREADY=1;

    publicstaticfinalintBLOCK=2;

    publicstaticfinalintRUNNING=3;

    publicstaticfinalintEXIT=4;

   

    publicintname;          // 进程标识符

    publicintstatus;        // 进程状态NEW-新建,READY-就绪RUNNING-执行,EXIT-完成, BLOCK-阻塞

    publicintpri;           // 进程优先级

    publicintneed_time;      // 进程总时间

    publicintr_time;        // 已运行时间,以时间片为单位,当减至 0 时该进程终止

    public PCB next;           // 下一个进程控制块

    publicintlocal;         // 该进程在显示板上的位置

    publicbooleanblock;     //是否会出现时间等待

    public JProgressBar bar;   // 进度条

    publicintarr_time;      //到达时间

    publicintqueue;         //进程当前所在就绪队列

    publicintblock_time = 0;    //挂起时间

    publicbooleanhasblocked =false; //是否挂起挂起过

   

    public PCB(int name, int status, int pri, int need_time, int r_time,JProgressBar bar,int local,boolean block) {

       this.name = name;

       this.status = status;

       this.pri = pri;

       this.need_time = need_time;

       this.r_time = r_time;

       this.bar=bar;

       this.local=local;

       this.block=block;

       this.hasblocked=false;

    }

 

    public PCB(int name, int status, int pri, int need_time, int r_time,JProgressBar bar,int local,boolean block,int arr_time){

        this.name = name;

       this.status = status;

       this.pri = pri;

       this.need_time = need_time;

       this.r_time = r_time;

       this.bar=bar;

       this.local=local;

       this.block=block;

       this.arr_time = arr_time;

       this.hasblocked=false;

    }

    public PCB(int name, int status, int pri, int need_time, int r_time,boolean block,int arr_time) {

       this.name = name;

       this.status = status;

       this.pri = pri;

       this.need_time = need_time;

       this.r_time = r_time;

        this.block = block;

        this.hasblocked=false;

        this.arr_time = arr_time;

    }

 

    publicint getName() {

       returnname;

    }

 

    publicvoid setName(int name) {

       this.name = name;

    }

 

    publicint getStatus() {

       returnstatus;

    }

 

    publicvoid setStatus(int status) {

       this.status = status;

    }

 

    publicint getPri() {

       returnpri;

    }

 

    publicvoid setPri(int pri) {

       this.pri = pri;

    }

 

    publicint getNeed_time() {

       returnneed_time;

    }

 

    publicvoid setNeed_time(int need_time){

       this.need_time = need_time;

    }

 

    publicint getR_time() {

       returnr_time;

    }

 

    publicvoid setR_time(int r_time) {

       this.r_time = r_time;

    }

 

    public PCB getNext() {

       returnnext;

    }

 

    publicvoid setNext(PCB next) {

       this.next = next;

    }

 

    public JProgressBar getBar() {

       returnbar;

    }

 

    publicvoid setBar(JProgressBar bar) {

       this.bar = bar;

    }

     

}

/src/UI/Main

package UI;

 

import staticjava.lang.System.exit;

import staticjavax.swing.BorderFactory.createEtchedBorder;

import staticjavax.swing.JOptionPane.INFORMATION_MESSAGE;

import staticjavax.swing.JOptionPane.showConfirmDialog;

import staticjavax.swing.UIManager.setLookAndFeel;

 

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.FlowLayout;

importjava.awt.Font;

importjava.awt.GridLayout;

importjava.awt.Menu;

importjava.awt.MenuBar;

importjava.awt.MenuItem;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.KeyEvent;

importjava.awt.event.KeyListener;

importjava.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

importjava.util.ArrayList;

importjava.util.Collections;

importjava.util.Comparator;

importjava.util.HashMap;

importjava.util.HashSet;

importjava.util.List;

importjava.util.Map;

importjava.util.Queue;

importjava.util.Set;

importjava.util.regex.Matcher;

importjava.util.regex.Pattern;

 

importjavax.swing.ButtonGroup;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JProgressBar;

importjavax.swing.JRadioButton;

importjavax.swing.JScrollPane;

importjavax.swing.JTable;

importjavax.swing.JTextField;

importjavax.swing.SwingConstants;

importjavax.swing.WindowConstants;

import javax.swing.border.EtchedBorder;

importjavax.swing.border.TitledBorder;

importjavax.swing.table.DefaultTableCellRenderer;

importjavax.swing.table.DefaultTableModel;

importjavax.swing.table.TableColumn;

 

importorg.omg.PortableInterceptor.SYSTEM_EXCEPTION;

 

 

importbusiness.CreatProcess;

importbusiness.ScheduProcessByMF;

importbusiness.SchedulProcess;

import data.PCB;

importdata.NewQueue;

importdata.ReadyQueue;

importuitils.AddProcessDialog;

importuitils.ConfigQueueDialog;

importuitils.ProgressBarObserver;

importuitils.ProgressBarObservable;

importuitils.ProgressBarRenderer;

importuitils.Timeing;

 

public class Mainextends JFrame{

 

         private JPanel toolsPanel;

         private JPanel processListJPanel;

        

         private JButton creat_pro_button;

         private JButton clear_pro_button;

         private JButton start_pro_button;

         private JButton reset_pro_button;

         private JTextField creat_pro_edit;

        

         private MenuItem item_start;

         private MenuItem item_start_quick;

         private MenuItem item_pro_config;

         private MenuItem item_pro_clear;

         private MenuItem item_nqueue_config;

         private MenuItem item_rqueue_config;

        

         private int number_pro = 0;

                  

         private javax.swing.JLabeltotal_time_label;

         public JScrollPane statusScrollPane1;

        

         public static StatusTableModelstatusTableModel;

         public static ProgressBarObservableprogressBarObservable;

         public JTable statusTable;

        

         public NewQueue nullQueue;             //空白队列

         public ReadyQueue[] readyQueues;       //多级就绪队列

         public ArrayList<PCB> arraylist =new ArrayList<PCB>();

         public ArrayList<PCB>resetArraylist = new ArrayList<PCB>();

         public static boolean start_time =true; // 控制总时间的运行

        

         private boolean firstRun = true;

         private Runnable proc;

         private Set<Integer> set = newHashSet<Integer>();

         private ScheduProcessByMF p1 = null;

         private Thread p2 = null;

        

         public Object[][] object;

         private String find = "";

        

         private int size_of_ReadyQueue=10;

         private int num_of_ReadyQueue=5;

         private int size_of_nullQueue=20;

        

         private Queue<PCB> queue_pcb;

        

         public static void main(String[] args)throws Exception {

                   //设置外观

                   setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

                   setDefaultLookAndFeelDecorated(true);

                   JFrame frame = new Main();

                    frame.setTitle("单处理器系统的进程调度_2014118122_任浩");

                    frame.addWindowListener(new WindowAdapter(){   // 确认是否要退出吗?

                            @Override

                    public void windowClosing(WindowEvent e) {

                            //      exit(0);

                                    

                                       int option = showConfirmDialog(null, "您确认要退出吗?", "温馨提示",

                                      JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if

                                      (option == JOptionPane.YES_OPTION) { exit(0);}

                                      

                            }

                   });

                  

                   frame.setUndecorated(false);//禁用或启用此窗体的装饰

                   frame.setSize(1000,800);

                   frame.setLocationRelativeTo(null);

                   frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

                   frame.setVisible(true);

         }

         public Main() throws Exception {

                   initViews();

                   initData();

         //      this.add(addPath(),BorderLayout.SOUTH);

         }

        

        

         /**

          * 设置主窗体

          * @throws Exception

          */

     void initViews() throws Exception

     {

                  JPanelcenterFrame = new JPanel(new GridLayout(1, 2, 10, 10));

                   toolsPanel = new JPanel(newFlowLayout(FlowLayout.LEFT));// (new

 

                   initMenus();

                  

                   creat_pro_edit = newJTextField(5); // 10,

                   Font font = newFont("TimesRoman", Font.BOLD, 15);

                   creat_pro_edit.setHorizontalAlignment(SwingConstants.CENTER);

                   creat_pro_edit.setToolTipText("温馨提示:在这里输入你要创建的进程个数,然后点击右边的\"创建进程\"按钮。");

        

                   //添加边框

                   EtchedBorder etchedBorder =(EtchedBorder) createEtchedBorder();

                   TitledBorder title = newTitledBorder(etchedBorder);

                   title.setTitleFont(font);

                   title.setTitleColor(Color.BLACK);

                   toolsPanel.setBorder(title);

        //输入框添加监听事件

                   creat_pro_edit.addKeyListener(newInputNumListener());

                   //显示时间

                   total_time_label = newjavax.swing.JLabel();

                   total_time_label.setText("运行时间");

                  

        

                   toolsPanel.add(total_time_label);

 

                   this.add(toolsPanel,BorderLayout.NORTH);

 

                   processListJPanel = newJPanel(new BorderLayout(5, 5));

 

                   TitledBorder processListTitle= new TitledBorder("进程");

                   title.setTitleFont(newFont("TimesRoman", Font.BOLD, 15));

                   title.setTitleColor(Color.BLACK);

                   processListJPanel.setBorder(processListTitle);

 

                   statusTableModel = newStatusTableModel();

                   statusTable = newJTable(statusTableModel);

                  

                   statusTable.getColumnModel().getColumn(0).setPreferredWidth(100);

                   statusTable.getColumnModel().getColumn(1).setPreferredWidth(100);

                   statusTable.getColumnModel().getColumn(2).setPreferredWidth(100);

                   statusTable.getColumnModel().getColumn(3).setPreferredWidth(100);

                   statusTable.getColumnModel().getColumn(4).setPreferredWidth(100);

                   statusTable.getColumnModel().getColumn(5).setPreferredWidth(250);

                   //statusTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

                  

                   DefaultTableCellRenderercellrender = new DefaultTableCellRenderer();

                   cellrender.setHorizontalAlignment(SwingConstants.CENTER);

                   statusTable.setDefaultRenderer(String.class,cellrender);

                  

                   statusScrollPane1 = newJScrollPane(statusTable);

                  

 

                   progressBarObservable = newProgressBarObservable();  //被观察者

                   ProgressBarObserverprogressBarObserver = new ProgressBarObserver(); //观察者

                   progressBarObservable.addObserver(progressBarObserver);

        

                   JScrollPane scrollPane = newJScrollPane(statusTable);

                   processListJPanel.add(scrollPane);

 

                   centerFrame.add(processListJPanel);

        

                   this.add(centerFrame, BorderLayout.CENTER);

     }

 

     /**

      *  设置菜单项

      */

     void initMenus()

     {

    MenuBarjMenuBar = new MenuBar();

   

                  MenujMenu = new Menu("开始");

                  jMenu.setFont(newFont("TimesRoman", Font.BOLD, 14));// 设置菜单显示的字体

                  item_start= new MenuItem("开始模拟");

                  item_start.setFont(newFont("TimesRoman", Font.PLAIN, 15));

                  item_start.addActionListener(newMenuItemListener());

                  jMenu.add(item_start);

                 

                  item_start_quick= new MenuItem("快速添加");

                  item_start_quick.setFont(newFont("TimesRoman", Font.PLAIN, 15));

                  item_start_quick.addActionListener(newMenuItemListener());

                  jMenu.add(item_start_quick);

                 

                  item_pro_config= new MenuItem("添加进程");

                  item_pro_config.setFont(newFont("TimesRoman", Font.PLAIN, 15));

                  item_pro_config.addActionListener(newMenuItemListener());

                  jMenu.add(item_pro_config);

                 

                  item_pro_clear= new MenuItem("清空");

                  item_pro_clear.setFont(newFont("TimesRoman", Font.PLAIN, 15));

                  item_pro_clear.addActionListener(newMenuItemListener());

                  jMenu.add(item_pro_clear);

                 

                  MenujMenu2 = new Menu("配置队列");

                  jMenu2.setFont(newFont("TimesRoman", Font.BOLD, 14));// 设置菜单显示的字体

                  item_nqueue_config= new MenuItem("配置空队列");

                  item_nqueue_config.setFont(newFont("TimesRoman", Font.PLAIN, 15));

                 item_nqueue_config.addActionListener(newMenuItemListener());

                  jMenu2.add(item_nqueue_config);

                   item_rqueue_config = newMenuItem("配置就绪队列");

                   item_rqueue_config.setFont(newFont("TimesRoman", Font.PLAIN, 15));

                   item_rqueue_config.addActionListener(newMenuItemListener());

                  jMenu2.add(item_rqueue_config);

 

                  jMenuBar.add(jMenu);

                  jMenuBar.add(jMenu2);

                  this.setMenuBar(jMenuBar);//菜单Bar放到JFrame上

     }

    

     void initData(){           

     int round_queue=1;  //就绪队列时间片

     readyQueues = new ReadyQueue[num_of_ReadyQueue];

     for (int i = 0; i < num_of_ReadyQueue; i++){

              

                readyQueues[i] = newReadyQueue(size_of_ReadyQueue,round_queue); //队列大小  时间片大小

                round_queue = round_queue*2;

                   }

   

   

     }

     private class MenuItemListener implementsActionListener {

 

                 publicvoid actionPerformed(ActionEvent e) {

                          if(e.getSource() == item_start) {

                                  

                                   if(arraylist.size() == 0) {

            

                                               JOptionPane.showMessageDialog(null,"请先创建进程!", "系统提示",

                                                                 JOptionPane.INFORMATION_MESSAGE);

                                               return;

                                     }

                                     if(firstRun) {

                                               firstRun= false;

                                               Runnabletime = new Timeing(total_time_label);

                                               p2= new Thread(time);

                                               p2.start();

                                     }

                   /*              

                                PCB    pcb_tp= nullQueue.getHead();

                                int count_tp=0;

                       while(pcb_tp!=null) {

                                    

                                                        readyQueues[0].add(pcb_tp);

                                                       

                                                        count_tp++;

                                                         if(count_tp==size_of_ReadyQueue)

                                                       break;

                                                         pcb_tp=nullQueue.getHead();

                                               }*/

                                     //readyQueues[0].newToReadyQueue(nullQueue,-1);

                            //      System.out.println("main 新建..."+nullQueue.getSize()+".."+nullQueue.readHead().arr_time);

                            //      proc = newScheduProcessByMF(statusTable,readyQueues,nullQueue);

                                     p1 = newScheduProcessByMF(statusTable,readyQueues,nullQueue);

                            //      p1 = new Thread(proc);

                                    

                                p1.start();

                                    

                          }

                         

                          elseif(e.getSource() == item_start_quick){

                                  

                                   StringinputValue = JOptionPane.showInputDialog("Please input the number ofprocess");

                                   intnum = Integer.parseInt(inputValue);                 //获取用户输入的进程个数

                                   CreatProcesscreatProcess=new CreatProcess(num);       //声明创建进程对象    

                                   Map<String,Object> map = creatProcess.createRandomProcess(); //随机创建进程,返回map【queue,arrylist】

                                   nullQueue=(NewQueue) map.get("queue");                 //获取返回队列

                                   arraylist= (ArrayList<PCB>) map.get("list");           //获取返回list

                         

                             addToPanel(num,arraylist);

                                    

                          }

                          elseif(e.getSource() == item_pro_config){                //添加进程

                                   ArrayList<PCB>p_list=AddProcessDialog.display();

                                   intnum = p_list.size();  

                                   CreatProcesscreatProcess=new CreatProcess(num);

                                   Map<String,Object> map=creatProcess.createProcessBylist(p_list);

                                   nullQueue=(NewQueue) map.get("queue");                //获取返回队列

                                   arraylist= (ArrayList<PCB>) map.get("list");           //获取返回list

                         

                             addToPanel(num,arraylist);

                          }

             else if(e.getSource() ==item_nqueue_config){

                         StringinputValue = JOptionPane.showInputDialog("设置空队列的大小(默认为20)");

                                         size_of_nullQueue= Integer.parseInt(inputValue);

                          }

             else if(e.getSource() ==item_rqueue_config){

                 Map<String,String> caipinMap = newHashMap<String,String>();

                 int[] tptp = new int[2];

                          tptp=ConfigQueueDialog.display(caipinMap);

                          num_of_ReadyQueue=tptp[0];             //获取就绪队列数

                          size_of_ReadyQueue=tptp[1];            //获取就绪队列大小

                 

                 }

             else if(e.getSource() ==item_pro_clear){

                 try {

                                               clearRow();

                                     } catch(InterruptedException e1) {

                                               //TODO Auto-generated catch block

                                               e1.printStackTrace();

                                     }

                         }

                 }

 

       }

     public void addToPanel(int num,ArrayList<PCB> arraylist_l)

     {

               object= new Object[num + 1][5];

                                     // 将进程显示在面板上

                                     for(inti=1;i<=num;i++)

                                     {

                                               System.out.print(i);

                                               //PCBpcb_tp=nullQueue.getItem(i);

                                               PCBpcb_tp= arraylist_l.get(i-1);

                                               object[i]= new Object[] { pcb_tp.getName(), pcb_tp.getStatus(), pcb_tp.getPri(),pcb_tp.getNeed_time(),

                                                                 pcb_tp.getR_time(),0 };

                                               statusTableModel.addProgressBar(object[i]);

                                               statusTable.setValueAt("新建", i-1, 1);

                                               ProgressBarRendererprogressBarRenderer = new ProgressBarRenderer();

                                               TableColumnstatusColumn = statusTable.getColumn("进程状态条");

                                               statusColumn.setCellRenderer(progressBarRenderer);

                                     }

     }

  

     private class InputNumListener implementsKeyListener {

 

                  publicvoid keyTyped(KeyEvent e) {

                           //System.out.println(jtfMessage.getText());

                  }

 

                  publicvoid keyPressed(KeyEvent e) {

                           //System.out.println(jtfMessage.getText());

                  }

 

                  publicvoid keyReleased(KeyEvent e) {

                           //System.out.println(jtfMessage.getText());

                           find= creat_pro_edit.getText();

                           isNum(find);

                 }

 

        }

  

     void isNum(String f) {

 

                   Pattern pattern =Pattern.compile("[0-9]*");

                   Matcher isNum =pattern.matcher(f);

                   if (!isNum.matches()) {

                            JOptionPane.showMessageDialog(null,"你的输入不是正数!", "系统提示",

                                               JOptionPane.INFORMATION_MESSAGE);

                            this.creat_pro_edit.setText("");

                            number_pro = 0;

                   } else {

                            number_pro =Integer.parseInt(f);

                   }

         }

 

         void createRamdonProcess(int number) {

 

                 

         }

 

 

         private void clearRow() throwsInterruptedException{

                   if (arraylist.size() == 0) {

                            JOptionPane.showMessageDialog(null,"请先创建进程!", "系统提示",

                                               JOptionPane.INFORMATION_MESSAGE);

                            return;

                   }

                   DefaultTableModel m =(StatusTableModel) statusTable.getModel();

                   m.setRowCount(0);

                   m.fireTableDataChanged();

                   arraylist = newArrayList<PCB>();

                   Main.start_time = true;

                   firstRun = true;

        CreatProcess.local = 0;

        p1.join();

        p1.exit = true;

        p1.destroy();

                   if (p1.isAlive()) {

                            //System.out.println("p1.isAlive()");

                            p1.stop();

                   }

                   p2.destroy();

                   if (Thread.currentThread().isAlive()){

                            Thread.currentThread().stop();

                   }

                   total_time_label.setText("运行时间");

         }

        

         

 

       

}

相关文章:

  • 2022-01-06
  • 2022-01-23
  • 2022-02-14
  • 2021-09-08
  • 2021-10-03
  • 2021-06-05
猜你喜欢
  • 2021-09-10
  • 2021-05-26
  • 2022-01-13
  • 2022-12-23
  • 2021-08-12
  • 2021-11-14
相关资源
相似解决方案