找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2935|回复: 2
收起左侧

关于时间轮转调度的操作系统的JAVA程序设计

[复制链接]
ID:574923 发表于 2019-6-30 10:56 | 显示全部楼层 |阅读模式
《操作系统原理》是计算机科学与技术专业的,也是研究生入学考试中计算机专业综合中所涉及的内容。理论性强,纯粹的理论学习相对枯燥乏味,不易理解。通过设计,可加强学生对原理知识的理解。
二、设计的任务和要求

本次设计的题目是,时间片轮转调度算法的模拟实现。要求在充分理解时间片轮转调度算法原理的基础上,编写一个可视化的算法模拟程序。

具体任务如下:

1、根据需要,合理设计PCB结构,以适用于时间片轮转调度算法;

2、设计模拟指令格式,并以文件形式存储,程序能够读取文件并自动生成指令序列。

3、根据文件内容,建立模拟进程队列,并能采用时间片轮转调度算法对模拟进程进行调度。

任务要求:

1、进程的个数,进程的内容(即进程的功能序列)来源于一个进程序列描述文件。

2、需将调度过程输出到一个运行日志文件。

3、开发平台及语言不限。

4、要求设计一个Windows可视化应用程序。


3、需求分析
在计算机系统中,可能同时有数百个批处理作业存放在磁盘的作业队列中,或者有数百个终端与主机相连接,这样一来内存和处理器等资源便供不应求。如何从这些作业中挑选作业进入主存运行、如何在进程之间分配处理器时间,无疑是操作系统资源管理中的一个重要问题。因此引入处理器调度用来完成涉及处理器分配的工作,而其中的低级调度,执行分配CPU 的程序即分派程序(dispatcher),它是操作系统最为核心的部分,执行十分频繁,低级调度策略优劣直接影响到整个系统的性能。
引入的目的是按照某种原则决定就绪队列中的哪个进程或内核级线程能获得处理器,并将处理器出让给它进行工作。
4、总体设计
  

5、详细设计与实现[含代码和实现界面]

【代码】
  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;//进程名称
  31.               public List<Instruction> pInstructions;//进程中指针列表
  32.               public int CurrentInstruction;//当前运行指令索引
  33.               PCB(){
  34.                             this.pInstructions = new ArrayList<Instruction>();
  35.               }
  36. }

  37. class Instruction{
  38.               public char IName;//指令类型
  39.               public double IRemainTime;//指令运行时间
  40.               public double IRuntime;////指令剩余运行时间
  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.               //创建各种队列,用ArrayList实现
  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.                             //开始调度按钮事件
  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,"请输入时间片大小!");
  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.                             //暂停调度按钮事件
  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.             //根据每个进程的首指令将进程分配到各个队列中

  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.               //每隔一个时间间隔运行一次
  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.                             //只要有队列不为空就一直调度
  320.                             if(ReadyQueue.size()!=0||InputWaitingQueue.size()!=0||OutputWaitingQueue.size()!=0||PureWaitingQueue.size()!=0) {
  321.                                          
  322.                                           //只有一个CPU,所以只能对就绪队列的第一个PCB的运行指令时间进行减一操作
  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的好处,动态添加和删除,[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.                                           //输入等待队列和输出等待队列以及其它等待队列之所以采用for循环是因为任务书上说I/O设备不限,所以可以对这几个队列的每个PCB进行减一
  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都没了就清空这个控件
  454.                                           if(ReadyQueue.size()==0) {
  455.                                                         textRunningProcess.setText("");
  456.                                           }
  457.                                          
  458.                                           //每进行一次调度就更新一次控件的信息
  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)+"次调度情况:\r\n"+"就绪队列:\r\n"+str1+"\r\n"+"输入等待队列:\r\n"+str2+"\r\n"
  486.                                                                                                                                             +"输出等待队列:\r\n"+str3+"\r\n"+"其它等待队列:\r\n"+str4+"\r\n";
  487.                                           writeFile(schedule);
  488.                             }
  489.                             else {
  490.                                           JOptionPane.showMessageDialog(null, "调度结束!");
  491.                                           time.cancel();
  492.                             }
  493.               }
  494.               public static void writeFile(String text) {
  495.         try {
  496.             File writeName = new File("F:\\Java201903\\output.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
  497. //            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
  498.             try (FileWriter writer = new FileWriter(writeName,true);//追加
  499.                  BufferedWriter out = new BufferedWriter(writer)
  500.             ) {
  501.                 out.write(text);
  502.                 out.flush(); // 把缓存区内容压入文件
  503.             }
  504.         } catch (IOException e) {
  505.             e.printStackTrace();
  506.         }
  507.     }

  508. }
复制代码

这次课设我提前有尝试自己来实现,查了一些网上的思路,书本上的算法,然后就试着慢慢写,遇到不懂的问了同学和老师。收获很多,在队列中用add和remove两个函数来实现。JAVA的好处是很多算法都直接调用就行了,对我很友好。代码不是很简洁,应该试着再改一下的。我觉得我应该花更多的时间在代码的学习和运用上。

以上内容Word格式文档51黑下载地址:
操作系统课程设计报告.doc (335 KB, 下载次数: 23)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:934927 发表于 2021-6-9 18:37 | 显示全部楼层
The public type Test must be defined in its own file 楼主您好 我直接复制您的代码报错了 请问源文件还在吗
回复

使用道具 举报

ID:1058238 发表于 2022-12-19 09:58 | 显示全部楼层
wizardbcat 发表于 2021-6-9 18:37
The public type Test must be defined in its own file 楼主您好 我直接复制您的代码报错了 请问源文件还 ...

把你的包名改成one就好了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表