找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3696|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

關(guān)于時(shí)間輪轉(zhuǎn)調(diào)度的操作系統(tǒng)的JAVA程序設(shè)計(jì)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
#
ID:574923 發(fā)表于 2019-6-30 10:56 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式
《操作系統(tǒng)原理》是計(jì)算機(jī)科學(xué)與技術(shù)專業(yè)的,也是研究生入學(xué)考試中計(jì)算機(jī)專業(yè)綜合中所涉及的內(nèi)容。理論性強(qiáng),純粹的理論學(xué)習(xí)相對(duì)枯燥乏味,不易理解。通過設(shè)計(jì),可加強(qiáng)學(xué)生對(duì)原理知識(shí)的理解。
二、設(shè)計(jì)的任務(wù)和要求

本次設(shè)計(jì)的題目是,時(shí)間片輪轉(zhuǎn)調(diào)度算法的模擬實(shí)現(xiàn)。要求在充分理解時(shí)間片輪轉(zhuǎn)調(diào)度算法原理的基礎(chǔ)上,編寫一個(gè)可視化的算法模擬程序。

具體任務(wù)如下:

1、根據(jù)需要,合理設(shè)計(jì)PCB結(jié)構(gòu),以適用于時(shí)間片輪轉(zhuǎn)調(diào)度算法;

2、設(shè)計(jì)模擬指令格式,并以文件形式存儲(chǔ),程序能夠讀取文件并自動(dòng)生成指令序列。

3、根據(jù)文件內(nèi)容,建立模擬進(jìn)程隊(duì)列,并能采用時(shí)間片輪轉(zhuǎn)調(diào)度算法對(duì)模擬進(jìn)程進(jìn)行調(diào)度。

任務(wù)要求:

1、進(jìn)程的個(gè)數(shù),進(jìn)程的內(nèi)容(即進(jìn)程的功能序列)來源于一個(gè)進(jìn)程序列描述文件。

2、需將調(diào)度過程輸出到一個(gè)運(yùn)行日志文件。

3、開發(fā)平臺(tái)及語言不限。

4、要求設(shè)計(jì)一個(gè)Windows可視化應(yīng)用程序。


3、需求分析
在計(jì)算機(jī)系統(tǒng)中,可能同時(shí)有數(shù)百個(gè)批處理作業(yè)存放在磁盤的作業(yè)隊(duì)列中,或者有數(shù)百個(gè)終端與主機(jī)相連接,這樣一來內(nèi)存和處理器等資源便供不應(yīng)求。如何從這些作業(yè)中挑選作業(yè)進(jìn)入主存運(yùn)行、如何在進(jìn)程之間分配處理器時(shí)間,無疑是操作系統(tǒng)資源管理中的一個(gè)重要問題。因此引入處理器調(diào)度用來完成涉及處理器分配的工作,而其中的低級(jí)調(diào)度,執(zhí)行分配CPU 的程序即分派程序(dispatcher),它是操作系統(tǒng)最為核心的部分,執(zhí)行十分頻繁,低級(jí)調(diào)度策略優(yōu)劣直接影響到整個(gè)系統(tǒng)的性能。
引入的目的是按照某種原則決定就緒隊(duì)列中的哪個(gè)進(jìn)程或內(nèi)核級(jí)線程能獲得處理器,并將處理器出讓給它進(jìn)行工作。
4、總體設(shè)計(jì)
  

5、詳細(xì)設(shè)計(jì)與實(shí)現(xiàn)[含代碼和實(shí)現(xiàn)界面]

【代碼】
  1. package one;

  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;

  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JLabel;
  8. import javax.swing.JButton;
  9. import javax.swing.JFileChooser;

  10. import java.awt.event.ActionListener;
  11. import java.io.BufferedReader;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Timer;
  22. import java.util.TimerTask;
  23. import java.awt.event.ActionEvent;
  24. import javax.swing.JTextField;
  25. import javax.swing.JList;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.JTextPane;
  28. import javax.swing.JTextArea;


  29. class PCB{
  30.               public String pname;//進(jìn)程名稱
  31.               public List<Instruction> pInstructions;//進(jìn)程中指針列表
  32.               public int CurrentInstruction;//當(dāng)前運(yùn)行指令索引
  33.               PCB(){
  34.                             this.pInstructions = new ArrayList<Instruction>();
  35.               }
  36. }

  37. class Instruction{
  38.               public char IName;//指令類型
  39.               public double IRemainTime;//指令運(yùn)行時(shí)間
  40.               public double IRuntime;////指令剩余運(yùn)行時(shí)間
  41.               Instruction(){
  42.               }            
  43. }

  44. public class Test extends JFrame {

  45.               private JPanel contentPane;
  46.               JTextField text_timeslice = new JTextField();
  47.               JTextField textRunningProcess = new JTextField();
  48.               JTextPane text_ready_queue = new JTextPane();
  49.               JTextPane text_iwait_queue = new JTextPane();
  50.               JTextPane text_other_queue = new JTextPane();
  51.               JTextPane text_owait_queue = new JTextPane();
  52.               JTextField text_count = new JTextField();
  53.               Timer time = new Timer();
  54.               int count = 0;
  55.               int num = 0;
  56.               //創(chuàng)建各種隊(duì)列,用ArrayList實(shí)現(xiàn)
  57.               List<PCB> AllQueue = new ArrayList<PCB>();
  58.               List<PCB> ReadyQueue = new ArrayList<PCB>();
  59.               List<PCB> InputWaitingQueue = new ArrayList<PCB>();
  60.               List<PCB> OutputWaitingQueue = new ArrayList<PCB>();
  61.               List<PCB> PureWaitingQueue = new ArrayList<PCB>();
  62.             
  63.             
  64.             
  65.               /**
  66.               * Launch the application.
  67.               */
  68.               public static void main(String[] args) {
  69.                             EventQueue.invokeLater(new Runnable() {
  70.                                           public void run() {
  71.                                                         try {
  72.                                                                       Test frame = new Test();
  73.                                                                       frame.setVisible(true);
  74.                                                         } catch (Exception e) {
  75.                                                                       e.printStackTrace();
  76.                                                         }
  77.                                           }
  78.                             });
  79.               }

  80.               /**
  81.               * Create the frame.
  82.               */
  83.               public Test() {
  84.                             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85.                             setBounds(100, 100, 956, 621);
  86.                             contentPane = new JPanel();
  87.                             contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  88.                             setContentPane(contentPane);
  89.                             contentPane.setLayout(null);
  90.                            
  91.                             JLabel label = new JLabel("\u65F6\u95F4\u7247:");
  92.                             label.setBounds(650, 53, 60, 18);
  93.                             contentPane.add(label);
  94.                            
  95.                             JButton btnOpenFile = new JButton("\u6253\u5F00\u6587\u4EF6");
  96.                             btnOpenFile.addActionListener(new ActionListener() {
  97.                                           public void actionPerformed(ActionEvent arg0) {
  98.                                                         JFileChooser chooser = new JFileChooser();
  99.                 if (chooser.showOpenDialog(btnOpenFile)==JFileChooser.APPROVE_OPTION) {//
  100.                     File file = chooser.getSelectedFile();
  101.                     textRunningProcess.setText(file.getName());
  102.                     readFile(file);
  103.                 };
  104.                                           }
  105.                             });
  106.                             btnOpenFile.setBounds(76, 49, 113, 27);
  107.                             contentPane.add(btnOpenFile);
  108.                            
  109.                             //開始調(diào)度按鈕事件
  110.                             JButton btnStartSchedule = new JButton("\u5F00\u59CB\u8C03\u5EA6");
  111.                             btnStartSchedule.addActionListener(new ActionListener() {
  112.                                           public void actionPerformed(ActionEvent e) {
  113.                                                         if(text_timeslice.getText().equals("")) {
  114.                                                                       JOptionPane.showMessageDialog(null,"請(qǐng)輸入時(shí)間片大小!");
  115.                                                         }
  116.                                                         else {
  117.                                                                       TimerTask task = new TimerTask() {
  118.                                                                                     public void run() {
  119.                                                                                                   RunOneTime();
  120.                                                                                                 
  121.                                                                                     }
  122.                                                                       };
  123.                                                                       btnStartSchedule.setEnabled(false);
  124.                                                                       time.schedule(task,new Date(),Long.parseLong(text_timeslice.getText()) );
  125.                                                         }
  126.                                           }
  127.                             });
  128.                             btnStartSchedule.setBounds(253, 49, 113, 27);
  129.                             contentPane.add(btnStartSchedule);
  130.                            
  131.                             //暫停調(diào)度按鈕事件
  132.                             JButton btnPauseSchedule = new JButton("\u6682\u505C\u8C03\u5EA6");
  133.                             btnPauseSchedule.addActionListener(new ActionListener() {
  134.                                           public void actionPerformed(ActionEvent e) {
  135.                                                         time.cancel();
  136.                                           }
  137.                             });
  138.                             btnPauseSchedule.setBounds(438, 49, 113, 27);
  139.                             contentPane.add(btnPauseSchedule);
  140.                            
  141.                            
  142.                             text_timeslice.setBounds(714, 50, 113, 24);
  143.                             contentPane.add(text_timeslice);
  144.                             text_timeslice.setColumns(10);
  145.                            
  146.                             JLabel label_1 = new JLabel("\u5F53\u524D\u8FD0\u884C\u8FDB\u7A0B:");
  147.                             label_1.setBounds(76, 108, 103, 18);
  148.                             contentPane.add(label_1);
  149.                            
  150.                            
  151.                             textRunningProcess.setBounds(76, 139, 103, 24);
  152.                             contentPane.add(textRunningProcess);
  153.                             textRunningProcess.setColumns(10);
  154.                            
  155.                             JLabel label_2 = new JLabel("\u5C31\u7EEA\u961F\u5217");
  156.                             label_2.setBounds(57, 205, 72, 18);
  157.                             contentPane.add(label_2);
  158.                            
  159.                             JLabel label_3 = new JLabel("\u8F93\u5165\u7B49\u5F85\u961F\u5217");
  160.                             label_3.setBounds(291, 205, 96, 18);
  161.                             contentPane.add(label_3);
  162.                            
  163.                             JLabel label_4 = new JLabel("\u8F93\u51FA\u7B49\u5F85\u961F\u5217");
  164.                             label_4.setBounds(497, 205, 96, 18);
  165.                             contentPane.add(label_4);
  166.                            
  167.                             JLabel label_5 = new JLabel("\u5176\u5B83\u7B49\u5F85\u961F\u5217");
  168.                             label_5.setBounds(721, 205, 96, 18);
  169.                             contentPane.add(label_5);
  170.                            
  171.                             text_ready_queue.setBounds(43, 236, 146, 205);
  172.                             contentPane.add(text_ready_queue);
  173.                            
  174.                            
  175.                             text_iwait_queue.setBounds(273, 236, 146, 205);
  176.                             contentPane.add(text_iwait_queue);
  177.                            
  178.                             text_owait_queue.setBounds(490, 236, 146, 205);
  179.                             contentPane.add(text_owait_queue);
  180.                            
  181.                            
  182.                             text_other_queue.setBounds(704, 236, 146, 205);
  183.                             contentPane.add(text_other_queue);
  184.                            

  185.                            
  186.                            
  187.                            
  188.               }
  189.               public void readFile(File file){//讀文件
  190.         BufferedReader bReader;
  191.         try {
  192.             bReader=new BufferedReader(new FileReader(file));
  193.             StringBuffer sBuffer=new StringBuffer();
  194.             String str;
  195.             while((str=bReader.readLine())!=null){
  196.                 sBuffer.append(str+'\n');
  197.                 switch(str.charAt(0)) {
  198.                 case 'P':
  199.                               PCB p = new PCB();
  200.                               p.pname = str;
  201.                               AllQueue.add(p);
  202.                               break;
  203.                 case 'C':
  204.                               Instruction ins1 = new Instruction();
  205.                               ins1.IRemainTime = Integer.parseInt(str.substring(1,3));
  206.                               ins1.IName = 'C';
  207.                               AllQueue.get(AllQueue.size()-1).pInstructions.add(ins1);
  208.                               break;
  209.                 case 'I':
  210.                               Instruction ins2 = new Instruction();
  211.                               ins2.IRemainTime = Integer.parseInt(str.substring(1,3));
  212.                               ins2.IName = 'I';
  213.                               AllQueue.get(AllQueue.size()-1).pInstructions.add(ins2);
  214.                               break;
  215.                 case 'O':
  216.                               Instruction ins3 = new Instruction();
  217.                               ins3.IRemainTime = Integer.parseInt(str.substring(1,3));
  218.                               ins3.IName = 'O';
  219.                               AllQueue.get(AllQueue.size()-1).pInstructions.add(ins3);
  220.                               break;
  221.                 case 'W':
  222.                               Instruction ins4 = new Instruction();
  223.                               ins4.IRemainTime = Integer.parseInt(str.substring(1,3));
  224.                               ins4.IName = 'W';
  225.                               AllQueue.get(AllQueue.size()-1).pInstructions.add(ins4);
  226.                               break;
  227.                 case 'H':
  228.                               Instruction ins5 = new Instruction();
  229.                               ins5.IRemainTime = Integer.parseInt(str.substring(1,3));
  230.                               ins5.IName = 'H';
  231.                               AllQueue.get(AllQueue.size()-1).pInstructions.add(ins5);
  232.                               break;
  233.                 }//switch                    
  234.             }//while


  235. //            String s1 = "";
  236. //            for(PCB p:ReadyQueue) {
  237. //                          s1 = s1+p.pname+"\r\n";
  238. //            }
  239. //            text_ready_queue.setText(s1);


  240.             //根據(jù)每個(gè)進(jìn)程的首指令將進(jìn)程分配到各個(gè)隊(duì)列中

  241. //            for(PCB p:AllQueue) {
  242. //                           switch(p.pInstructions.get(0).IName) {
  243. //                           case 'C':
  244. //                                         ReadyQueue.add(p);
  245. //                                         break;
  246. //                           case 'I':
  247. //                                         InputWaitingQueue.add(p);
  248. //                                         break;
  249. //                           case 'O':
  250. //                                         OutputWaitingQueue.add(p);
  251. //                                         break;
  252. //                           case 'W':
  253. //                                         PureWaitingQueue.add(p);
  254. //                                         break;
  255. //                           case 'H':
  256. //                                         break;
  257. //                           }
  258. //             }
  259. //           
  260.             String s1 = "";
  261.           for(PCB p:AllQueue) {
  262.                         s1 = s1+p.pname+"\r\n";
  263.           }
  264.           text_ready_queue.setText(s1);
  265.             //將界面正確顯示
  266. //            String s1 = "";
  267. //            for(PCB p:ReadyQueue) {
  268. //                          s1 = s1+p.pname+"\r\n";
  269. //            }
  270. //            text_ready_queue.setText(s1);
  271. //           
  272. //            String s2 = "";
  273. //            for(PCB p:InputWaitingQueue) {
  274. //                          s2 = s2+p.pname+"\r\n";
  275. //            }
  276. //            text_iwait_queue.setText(s2);
  277. //           
  278. //            String s3 = "";
  279. //            for(PCB p:OutputWaitingQueue) {
  280. //                          s3 = s3+p.pname+"\r\n";
  281. //            }
  282. //            text_owait_queue.setText(s3);
  283. //           
  284. //            String s4 = "";
  285. //            for(PCB p:PureWaitingQueue) {
  286. //                          s4 = s4+p.pname+"\r\n";
  287. //            }
  288. //            text_other_queue.setText(s4);

  289.         } catch (Exception e) {
  290.             // TODO: handle exception
  291.         }
  292.     }
  293.             
  294.             
  295.               //每隔一個(gè)時(shí)間間隔運(yùn)行一次
  296.               public void RunOneTime() {
  297.                             int size = AllQueue.size();
  298.                             if(num<size) {
  299.                                           PCB p = AllQueue.get(num);
  300.                                           switch(p.pInstructions.get(0).IName) {
  301.                                  case 'C':
  302.                                                ReadyQueue.add(p);
  303.                                                break;
  304.                                  case 'I':
  305.                                                InputWaitingQueue.add(p);
  306.                                                break;
  307.                                  case 'O':
  308.                                                OutputWaitingQueue.add(p);
  309.                                                break;
  310.                                  case 'W':
  311.                                                PureWaitingQueue.add(p);
  312.                                                break;
  313.                                  case 'H':
  314.                                                break;
  315.                                  }
  316.                                           num++;
  317.                             }
  318.                            
  319.                             //只要有隊(duì)列不為空就一直調(diào)度
  320.                             if(ReadyQueue.size()!=0||InputWaitingQueue.size()!=0||OutputWaitingQueue.size()!=0||PureWaitingQueue.size()!=0) {
  321.                                          
  322.                                           //只有一個(gè)CPU,所以只能對(duì)就緒隊(duì)列的第一個(gè)PCB的運(yùn)行指令時(shí)間進(jìn)行減一操作
  323.                                           if(ReadyQueue.size()!=0) {
  324.                                                         if(ReadyQueue.get(0).pInstructions.get(ReadyQueue.get(0).CurrentInstruction).IRemainTime==0) {
  325.                                                                       ReadyQueue.get(0).CurrentInstruction++;
  326.                                                                       switch(ReadyQueue.get(0).pInstructions.get(ReadyQueue.get(0).CurrentInstruction).IName) {
  327.                                                                       case 'C':
  328.                                                                                     ReadyQueue.add(ReadyQueue.get(0));//先加再刪,ArrayList的好處,動(dòng)態(tài)添加和刪除,[1,2,3]把1刪了再加,就是[2,,3,1]了
  329.                                                                                     ReadyQueue.remove(ReadyQueue.get(0));
  330.                                                                                     break;
  331.                                                                       case 'I':
  332.                                                                                     InputWaitingQueue.add(ReadyQueue.get(0));
  333.                                                                                     ReadyQueue.remove(ReadyQueue.get(0));
  334.                                                                                     break;
  335.                                                                       case 'O':
  336.                                                                                     OutputWaitingQueue.add(ReadyQueue.get(0));
  337.                                                                                     ReadyQueue.remove(ReadyQueue.get(0));
  338.                                                                                     break;
  339.                                                                       case 'W':
  340.                                                                                     PureWaitingQueue.add(ReadyQueue.get(0));
  341.                                                                                     ReadyQueue.remove(ReadyQueue.get(0));
  342.                                                                                     break;
  343.                                                                       case 'H':
  344.                                                                                     ReadyQueue.remove(ReadyQueue.get(0));
  345.                                                                                     break;
  346.                                                                       }
  347.                                                         }
  348.                                                         else {
  349.                                                                       ReadyQueue.get(0).pInstructions.get(ReadyQueue.get(0).CurrentInstruction).IRemainTime--;
  350.                                                                       textRunningProcess.setText(ReadyQueue.get(0).pname);
  351.                                                                       ReadyQueue.add(ReadyQueue.get(0));
  352.                                                                       ReadyQueue.remove(ReadyQueue.get(0));
  353.                                                         }
  354.                                           }
  355.                                          
  356.                                           //輸入等待隊(duì)列和輸出等待隊(duì)列以及其它等待隊(duì)列之所以采用for循環(huán)是因?yàn)槿蝿?wù)書上說I/O設(shè)備不限,所以可以對(duì)這幾個(gè)隊(duì)列的每個(gè)PCB進(jìn)行減一
  357.                                           if(InputWaitingQueue.size()!=0) {
  358.                                                         for(int i=0;i<=InputWaitingQueue.size()-1;i++) {
  359.                                                                       if(InputWaitingQueue.get(i).pInstructions.get(InputWaitingQueue.get(i).CurrentInstruction).IRemainTime==0) {
  360.                                                                                     InputWaitingQueue.get(i).CurrentInstruction++;
  361.                                                                                     switch(InputWaitingQueue.get(i).pInstructions.get(InputWaitingQueue.get(i).CurrentInstruction).IName) {
  362.                                                                                     case 'C':
  363.                                                                                                   ReadyQueue.add(InputWaitingQueue.get(i));
  364.                                                                                                   InputWaitingQueue.remove(InputWaitingQueue.get(i));
  365.                                                                                                   break;
  366.                                                                                     case 'I':
  367.                                                                                                   InputWaitingQueue.add(InputWaitingQueue.get(i));
  368.                                                                                                   InputWaitingQueue.remove(InputWaitingQueue.get(i));
  369.                                                                                                   break;
  370.                                                                                     case 'O':
  371.                                                                                                   OutputWaitingQueue.add(InputWaitingQueue.get(i));
  372.                                                                                                   InputWaitingQueue.remove(InputWaitingQueue.get(i));
  373.                                                                                                   break;
  374.                                                                                     case 'W':
  375.                                                                                                   PureWaitingQueue.add(InputWaitingQueue.get(i));
  376.                                                                                                   InputWaitingQueue.remove(InputWaitingQueue.get(i));
  377.                                                                                                   break;
  378.                                                                                     case 'H':
  379.                                                                                                   InputWaitingQueue.remove(InputWaitingQueue.get(i));
  380.                                                                                                   break;
  381.                                                                                     }
  382.                                                                       }
  383.                                                                       else {
  384.                                                                                     InputWaitingQueue.get(i).pInstructions.get(InputWaitingQueue.get(i).CurrentInstruction).IRemainTime--;
  385.                                                                       }
  386.                                                         }
  387.                                           }
  388.                                          
  389.                                           if(OutputWaitingQueue.size()!=0) {
  390.                                                         for(int i=0;i<=OutputWaitingQueue.size()-1;i++) {
  391.                                                                       if(OutputWaitingQueue.get(i).pInstructions.get(OutputWaitingQueue.get(i).CurrentInstruction).IRemainTime==0) {
  392.                                                                                     OutputWaitingQueue.get(i).CurrentInstruction++;
  393.                                                                                     switch(OutputWaitingQueue.get(i).pInstructions.get(OutputWaitingQueue.get(i).CurrentInstruction).IName) {
  394.                                                                                     case 'C':
  395.                                                                                                   ReadyQueue.add(OutputWaitingQueue.get(i));
  396.                                                                                                   OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
  397.                                                                                                   break;
  398.                                                                                     case 'I':
  399.                                                                                                   InputWaitingQueue.add(OutputWaitingQueue.get(i));
  400.                                                                                                   OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
  401.                                                                                                   break;
  402.                                                                                     case 'O':
  403.                                                                                                   OutputWaitingQueue.add(OutputWaitingQueue.get(i));
  404.                                                                                                   OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
  405.                                                                                                   break;
  406.                                                                                     case 'W':
  407.                                                                                                   PureWaitingQueue.add(OutputWaitingQueue.get(i));
  408.                                                                                                   OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
  409.                                                                                                   break;
  410.                                                                                     case 'H':
  411.                                                                                                   OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
  412.                                                                                                   break;
  413.                                                                                     }
  414.                                                                       }
  415.                                                                       else {
  416.                                                                                     OutputWaitingQueue.get(i).pInstructions.get(OutputWaitingQueue.get(i).CurrentInstruction).IRemainTime--;
  417.                                                                       }
  418.                                                         }
  419.                                           }
  420.                                          
  421.                                           if(PureWaitingQueue.size()!=0) {
  422.                                                         for(int i=0;i<=PureWaitingQueue.size()-1;i++) {
  423.                                                                       if(PureWaitingQueue.get(i).pInstructions.get(PureWaitingQueue.get(i).CurrentInstruction).IRemainTime==0) {
  424.                                                                                     PureWaitingQueue.get(i).CurrentInstruction++;
  425.                                                                                     switch(PureWaitingQueue.get(i).pInstructions.get(PureWaitingQueue.get(i).CurrentInstruction).IName) {
  426.                                                                                     case 'C':
  427.                                                                                                   ReadyQueue.add(PureWaitingQueue.get(i));
  428.                                                                                                   PureWaitingQueue.remove(PureWaitingQueue.get(i));
  429.                                                                                                   break;
  430.                                                                                     case 'I':
  431.                                                                                                   InputWaitingQueue.add(PureWaitingQueue.get(i));
  432.                                                                                                   PureWaitingQueue.remove(PureWaitingQueue.get(i));
  433.                                                                                                   break;
  434.                                                                                     case 'O':
  435.                                                                                                   OutputWaitingQueue.add(PureWaitingQueue.get(i));
  436.                                                                                                   PureWaitingQueue.remove(PureWaitingQueue.get(i));
  437.                                                                                                   break;
  438.                                                                                     case 'W':
  439.                                                                                                   PureWaitingQueue.add(PureWaitingQueue.get(i));
  440.                                                                                                   PureWaitingQueue.remove(PureWaitingQueue.get(i));
  441.                                                                                                   break;
  442.                                                                                     case 'H':
  443.                                                                                                   PureWaitingQueue.remove(PureWaitingQueue.get(i));
  444.                                                                                                   break;
  445.                                                                                     }
  446.                                                                       }
  447.                                                                       else {
  448.                                                                                     PureWaitingQueue.get(i).pInstructions.get(PureWaitingQueue.get(i).CurrentInstruction).IRemainTime--;
  449.                                                                       }
  450.                                                         }
  451.                                           }
  452.                                          
  453.                                           //所有用到CPU資源的PCB都沒了就清空這個(gè)控件
  454.                                           if(ReadyQueue.size()==0) {
  455.                                                         textRunningProcess.setText("");
  456.                                           }
  457.                                          
  458.                                           //每進(jìn)行一次調(diào)度就更新一次控件的信息
  459.                                           String str1 = "";
  460.                                           for(int i=0;i<ReadyQueue.size();i++) {
  461.                                                         str1 = str1+ReadyQueue.get(i).pname+"\r\n";
  462.                                           }
  463.                                           text_ready_queue.setText(str1);
  464.                                          
  465.                                           String str2 = "";
  466.                                           for(int i=0;i<InputWaitingQueue.size();i++) {
  467.                                                         str2 = str2+InputWaitingQueue.get(i).pname+"\r\n";
  468.                                           }
  469.                                           text_iwait_queue.setText(str2);
  470.                                          
  471.                                           String str3 = "";
  472.                                           for(int i=0;i<OutputWaitingQueue.size();i++) {
  473.                                                         str3 = str3+OutputWaitingQueue.get(i).pname+"\r\n";
  474.                                           }
  475.                                           text_owait_queue.setText(str3);
  476.                                          
  477.                                           String str4 = "";
  478.                                           for(int i=0;i<PureWaitingQueue.size();i++) {
  479.                                                         str4 = str4+PureWaitingQueue.get(i).pname+"\r\n";
  480.                                           }
  481.                                           text_other_queue.setText(str4);
  482.                                          
  483.                                           count++;
  484.                                          
  485.                                           String schedule = "第"+String.valueOf(count)+"次調(diào)度情況:\r\n"+"就緒隊(duì)列:\r\n"+str1+"\r\n"+"輸入等待隊(duì)列:\r\n"+str2+"\r\n"
  486.                                                                                                                                             +"輸出等待隊(duì)列:\r\n"+str3+"\r\n"+"其它等待隊(duì)列:\r\n"+str4+"\r\n";
  487.                                           writeFile(schedule);
  488.                             }
  489.                             else {
  490.                                           JOptionPane.showMessageDialog(null, "調(diào)度結(jié)束!");
  491.                                           time.cancel();
  492.                             }
  493.               }
  494.               public static void writeFile(String text) {
  495.         try {
  496.             File writeName = new File("F:\\Java201903\\output.txt"); // 相對(duì)路徑,如果沒有則要建立一個(gè)新的output.txt文件
  497. //            writeName.createNewFile(); // 創(chuàng)建新文件,有同名的文件的話直接覆蓋
  498.             try (FileWriter writer = new FileWriter(writeName,true);//追加
  499.                  BufferedWriter out = new BufferedWriter(writer)
  500.             ) {
  501.                 out.write(text);
  502.                 out.flush(); // 把緩存區(qū)內(nèi)容壓入文件
  503.             }
  504.         } catch (IOException e) {
  505.             e.printStackTrace();
  506.         }
  507.     }

  508. }
復(fù)制代碼

這次課設(shè)我提前有嘗試自己來實(shí)現(xiàn),查了一些網(wǎng)上的思路,書本上的算法,然后就試著慢慢寫,遇到不懂的問了同學(xué)和老師。收獲很多,在隊(duì)列中用add和remove兩個(gè)函數(shù)來實(shí)現(xiàn)。JAVA的好處是很多算法都直接調(diào)用就行了,對(duì)我很友好。代碼不是很簡(jiǎn)潔,應(yīng)該試著再改一下的。我覺得我應(yīng)該花更多的時(shí)間在代碼的學(xué)習(xí)和運(yùn)用上。

以上內(nèi)容Word格式文檔51黑下載地址:
操作系統(tǒng)課程設(shè)計(jì)報(bào)告.doc (335 KB, 下載次數(shù): 25)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:1058238 發(fā)表于 2022-12-19 09:58 | 只看該作者
wizardbcat 發(fā)表于 2021-6-9 18:37
The public type Test must be defined in its own file 樓主您好 我直接復(fù)制您的代碼報(bào)錯(cuò)了 請(qǐng)問源文件還 ...

把你的包名改成one就好了
回復(fù)

使用道具 舉報(bào)

樓主
ID:934927 發(fā)表于 2021-6-9 18:37 | 只看該作者
The public type Test must be defined in its own file 樓主您好 我直接復(fù)制您的代碼報(bào)錯(cuò)了 請(qǐng)問源文件還在嗎
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表