《操作系統(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)界面]
【代碼】 - package one;
-
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
-
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
-
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.awt.event.ActionEvent;
- import javax.swing.JTextField;
- import javax.swing.JList;
- import javax.swing.JOptionPane;
- import javax.swing.JTextPane;
- import javax.swing.JTextArea;
-
-
- class PCB{
- public String pname;//進(jìn)程名稱
- public List<Instruction> pInstructions;//進(jìn)程中指針列表
- public int CurrentInstruction;//當(dāng)前運(yùn)行指令索引
- PCB(){
- this.pInstructions = new ArrayList<Instruction>();
- }
- }
-
- class Instruction{
- public char IName;//指令類型
- public double IRemainTime;//指令運(yùn)行時(shí)間
- public double IRuntime;////指令剩余運(yùn)行時(shí)間
- Instruction(){
- }
- }
-
- public class Test extends JFrame {
-
- private JPanel contentPane;
- JTextField text_timeslice = new JTextField();
- JTextField textRunningProcess = new JTextField();
- JTextPane text_ready_queue = new JTextPane();
- JTextPane text_iwait_queue = new JTextPane();
- JTextPane text_other_queue = new JTextPane();
- JTextPane text_owait_queue = new JTextPane();
- JTextField text_count = new JTextField();
- Timer time = new Timer();
- int count = 0;
- int num = 0;
- //創(chuàng)建各種隊(duì)列,用ArrayList實(shí)現(xiàn)
- List<PCB> AllQueue = new ArrayList<PCB>();
- List<PCB> ReadyQueue = new ArrayList<PCB>();
- List<PCB> InputWaitingQueue = new ArrayList<PCB>();
- List<PCB> OutputWaitingQueue = new ArrayList<PCB>();
- List<PCB> PureWaitingQueue = new ArrayList<PCB>();
-
-
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Test frame = new Test();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public Test() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 956, 621);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- JLabel label = new JLabel("\u65F6\u95F4\u7247:");
- label.setBounds(650, 53, 60, 18);
- contentPane.add(label);
-
- JButton btnOpenFile = new JButton("\u6253\u5F00\u6587\u4EF6");
- btnOpenFile.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JFileChooser chooser = new JFileChooser();
- if (chooser.showOpenDialog(btnOpenFile)==JFileChooser.APPROVE_OPTION) {//
- File file = chooser.getSelectedFile();
- textRunningProcess.setText(file.getName());
- readFile(file);
- };
- }
- });
- btnOpenFile.setBounds(76, 49, 113, 27);
- contentPane.add(btnOpenFile);
-
- //開始調(diào)度按鈕事件
- JButton btnStartSchedule = new JButton("\u5F00\u59CB\u8C03\u5EA6");
- btnStartSchedule.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(text_timeslice.getText().equals("")) {
- JOptionPane.showMessageDialog(null,"請(qǐng)輸入時(shí)間片大小!");
- }
- else {
- TimerTask task = new TimerTask() {
- public void run() {
- RunOneTime();
-
- }
- };
- btnStartSchedule.setEnabled(false);
- time.schedule(task,new Date(),Long.parseLong(text_timeslice.getText()) );
- }
- }
- });
- btnStartSchedule.setBounds(253, 49, 113, 27);
- contentPane.add(btnStartSchedule);
-
- //暫停調(diào)度按鈕事件
- JButton btnPauseSchedule = new JButton("\u6682\u505C\u8C03\u5EA6");
- btnPauseSchedule.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- time.cancel();
- }
- });
- btnPauseSchedule.setBounds(438, 49, 113, 27);
- contentPane.add(btnPauseSchedule);
-
-
- text_timeslice.setBounds(714, 50, 113, 24);
- contentPane.add(text_timeslice);
- text_timeslice.setColumns(10);
-
- JLabel label_1 = new JLabel("\u5F53\u524D\u8FD0\u884C\u8FDB\u7A0B:");
- label_1.setBounds(76, 108, 103, 18);
- contentPane.add(label_1);
-
-
- textRunningProcess.setBounds(76, 139, 103, 24);
- contentPane.add(textRunningProcess);
- textRunningProcess.setColumns(10);
-
- JLabel label_2 = new JLabel("\u5C31\u7EEA\u961F\u5217");
- label_2.setBounds(57, 205, 72, 18);
- contentPane.add(label_2);
-
- JLabel label_3 = new JLabel("\u8F93\u5165\u7B49\u5F85\u961F\u5217");
- label_3.setBounds(291, 205, 96, 18);
- contentPane.add(label_3);
-
- JLabel label_4 = new JLabel("\u8F93\u51FA\u7B49\u5F85\u961F\u5217");
- label_4.setBounds(497, 205, 96, 18);
- contentPane.add(label_4);
-
- JLabel label_5 = new JLabel("\u5176\u5B83\u7B49\u5F85\u961F\u5217");
- label_5.setBounds(721, 205, 96, 18);
- contentPane.add(label_5);
-
- text_ready_queue.setBounds(43, 236, 146, 205);
- contentPane.add(text_ready_queue);
-
-
- text_iwait_queue.setBounds(273, 236, 146, 205);
- contentPane.add(text_iwait_queue);
-
- text_owait_queue.setBounds(490, 236, 146, 205);
- contentPane.add(text_owait_queue);
-
-
- text_other_queue.setBounds(704, 236, 146, 205);
- contentPane.add(text_other_queue);
-
-
-
-
-
- }
- public void readFile(File file){//讀文件
- BufferedReader bReader;
- try {
- bReader=new BufferedReader(new FileReader(file));
- StringBuffer sBuffer=new StringBuffer();
- String str;
- while((str=bReader.readLine())!=null){
- sBuffer.append(str+'\n');
- switch(str.charAt(0)) {
- case 'P':
- PCB p = new PCB();
- p.pname = str;
- AllQueue.add(p);
- break;
- case 'C':
- Instruction ins1 = new Instruction();
- ins1.IRemainTime = Integer.parseInt(str.substring(1,3));
- ins1.IName = 'C';
- AllQueue.get(AllQueue.size()-1).pInstructions.add(ins1);
- break;
- case 'I':
- Instruction ins2 = new Instruction();
- ins2.IRemainTime = Integer.parseInt(str.substring(1,3));
- ins2.IName = 'I';
- AllQueue.get(AllQueue.size()-1).pInstructions.add(ins2);
- break;
- case 'O':
- Instruction ins3 = new Instruction();
- ins3.IRemainTime = Integer.parseInt(str.substring(1,3));
- ins3.IName = 'O';
- AllQueue.get(AllQueue.size()-1).pInstructions.add(ins3);
- break;
- case 'W':
- Instruction ins4 = new Instruction();
- ins4.IRemainTime = Integer.parseInt(str.substring(1,3));
- ins4.IName = 'W';
- AllQueue.get(AllQueue.size()-1).pInstructions.add(ins4);
- break;
- case 'H':
- Instruction ins5 = new Instruction();
- ins5.IRemainTime = Integer.parseInt(str.substring(1,3));
- ins5.IName = 'H';
- AllQueue.get(AllQueue.size()-1).pInstructions.add(ins5);
- break;
- }//switch
- }//while
-
-
- // String s1 = "";
- // for(PCB p:ReadyQueue) {
- // s1 = s1+p.pname+"\r\n";
- // }
- // text_ready_queue.setText(s1);
-
-
- //根據(jù)每個(gè)進(jìn)程的首指令將進(jìn)程分配到各個(gè)隊(duì)列中
-
- // for(PCB p:AllQueue) {
- // switch(p.pInstructions.get(0).IName) {
- // case 'C':
- // ReadyQueue.add(p);
- // break;
- // case 'I':
- // InputWaitingQueue.add(p);
- // break;
- // case 'O':
- // OutputWaitingQueue.add(p);
- // break;
- // case 'W':
- // PureWaitingQueue.add(p);
- // break;
- // case 'H':
- // break;
- // }
- // }
- //
- String s1 = "";
- for(PCB p:AllQueue) {
- s1 = s1+p.pname+"\r\n";
- }
- text_ready_queue.setText(s1);
- //將界面正確顯示
- // String s1 = "";
- // for(PCB p:ReadyQueue) {
- // s1 = s1+p.pname+"\r\n";
- // }
- // text_ready_queue.setText(s1);
- //
- // String s2 = "";
- // for(PCB p:InputWaitingQueue) {
- // s2 = s2+p.pname+"\r\n";
- // }
- // text_iwait_queue.setText(s2);
- //
- // String s3 = "";
- // for(PCB p:OutputWaitingQueue) {
- // s3 = s3+p.pname+"\r\n";
- // }
- // text_owait_queue.setText(s3);
- //
- // String s4 = "";
- // for(PCB p:PureWaitingQueue) {
- // s4 = s4+p.pname+"\r\n";
- // }
- // text_other_queue.setText(s4);
-
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
-
-
- //每隔一個(gè)時(shí)間間隔運(yùn)行一次
- public void RunOneTime() {
- int size = AllQueue.size();
- if(num<size) {
- PCB p = AllQueue.get(num);
- switch(p.pInstructions.get(0).IName) {
- case 'C':
- ReadyQueue.add(p);
- break;
- case 'I':
- InputWaitingQueue.add(p);
- break;
- case 'O':
- OutputWaitingQueue.add(p);
- break;
- case 'W':
- PureWaitingQueue.add(p);
- break;
- case 'H':
- break;
- }
- num++;
- }
-
- //只要有隊(duì)列不為空就一直調(diào)度
- if(ReadyQueue.size()!=0||InputWaitingQueue.size()!=0||OutputWaitingQueue.size()!=0||PureWaitingQueue.size()!=0) {
-
- //只有一個(gè)CPU,所以只能對(duì)就緒隊(duì)列的第一個(gè)PCB的運(yùn)行指令時(shí)間進(jìn)行減一操作
- if(ReadyQueue.size()!=0) {
- if(ReadyQueue.get(0).pInstructions.get(ReadyQueue.get(0).CurrentInstruction).IRemainTime==0) {
- ReadyQueue.get(0).CurrentInstruction++;
- switch(ReadyQueue.get(0).pInstructions.get(ReadyQueue.get(0).CurrentInstruction).IName) {
- case 'C':
- ReadyQueue.add(ReadyQueue.get(0));//先加再刪,ArrayList的好處,動(dòng)態(tài)添加和刪除,[1,2,3]把1刪了再加,就是[2,,3,1]了
- ReadyQueue.remove(ReadyQueue.get(0));
- break;
- case 'I':
- InputWaitingQueue.add(ReadyQueue.get(0));
- ReadyQueue.remove(ReadyQueue.get(0));
- break;
- case 'O':
- OutputWaitingQueue.add(ReadyQueue.get(0));
- ReadyQueue.remove(ReadyQueue.get(0));
- break;
- case 'W':
- PureWaitingQueue.add(ReadyQueue.get(0));
- ReadyQueue.remove(ReadyQueue.get(0));
- break;
- case 'H':
- ReadyQueue.remove(ReadyQueue.get(0));
- break;
- }
- }
- else {
- ReadyQueue.get(0).pInstructions.get(ReadyQueue.get(0).CurrentInstruction).IRemainTime--;
- textRunningProcess.setText(ReadyQueue.get(0).pname);
- ReadyQueue.add(ReadyQueue.get(0));
- ReadyQueue.remove(ReadyQueue.get(0));
- }
- }
-
- //輸入等待隊(duì)列和輸出等待隊(duì)列以及其它等待隊(duì)列之所以采用for循環(huán)是因?yàn)槿蝿?wù)書上說I/O設(shè)備不限,所以可以對(duì)這幾個(gè)隊(duì)列的每個(gè)PCB進(jìn)行減一
- if(InputWaitingQueue.size()!=0) {
- for(int i=0;i<=InputWaitingQueue.size()-1;i++) {
- if(InputWaitingQueue.get(i).pInstructions.get(InputWaitingQueue.get(i).CurrentInstruction).IRemainTime==0) {
- InputWaitingQueue.get(i).CurrentInstruction++;
- switch(InputWaitingQueue.get(i).pInstructions.get(InputWaitingQueue.get(i).CurrentInstruction).IName) {
- case 'C':
- ReadyQueue.add(InputWaitingQueue.get(i));
- InputWaitingQueue.remove(InputWaitingQueue.get(i));
- break;
- case 'I':
- InputWaitingQueue.add(InputWaitingQueue.get(i));
- InputWaitingQueue.remove(InputWaitingQueue.get(i));
- break;
- case 'O':
- OutputWaitingQueue.add(InputWaitingQueue.get(i));
- InputWaitingQueue.remove(InputWaitingQueue.get(i));
- break;
- case 'W':
- PureWaitingQueue.add(InputWaitingQueue.get(i));
- InputWaitingQueue.remove(InputWaitingQueue.get(i));
- break;
- case 'H':
- InputWaitingQueue.remove(InputWaitingQueue.get(i));
- break;
- }
- }
- else {
- InputWaitingQueue.get(i).pInstructions.get(InputWaitingQueue.get(i).CurrentInstruction).IRemainTime--;
- }
- }
- }
-
- if(OutputWaitingQueue.size()!=0) {
- for(int i=0;i<=OutputWaitingQueue.size()-1;i++) {
- if(OutputWaitingQueue.get(i).pInstructions.get(OutputWaitingQueue.get(i).CurrentInstruction).IRemainTime==0) {
- OutputWaitingQueue.get(i).CurrentInstruction++;
- switch(OutputWaitingQueue.get(i).pInstructions.get(OutputWaitingQueue.get(i).CurrentInstruction).IName) {
- case 'C':
- ReadyQueue.add(OutputWaitingQueue.get(i));
- OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
- break;
- case 'I':
- InputWaitingQueue.add(OutputWaitingQueue.get(i));
- OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
- break;
- case 'O':
- OutputWaitingQueue.add(OutputWaitingQueue.get(i));
- OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
- break;
- case 'W':
- PureWaitingQueue.add(OutputWaitingQueue.get(i));
- OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
- break;
- case 'H':
- OutputWaitingQueue.remove(OutputWaitingQueue.get(i));
- break;
- }
- }
- else {
- OutputWaitingQueue.get(i).pInstructions.get(OutputWaitingQueue.get(i).CurrentInstruction).IRemainTime--;
- }
- }
- }
-
- if(PureWaitingQueue.size()!=0) {
- for(int i=0;i<=PureWaitingQueue.size()-1;i++) {
- if(PureWaitingQueue.get(i).pInstructions.get(PureWaitingQueue.get(i).CurrentInstruction).IRemainTime==0) {
- PureWaitingQueue.get(i).CurrentInstruction++;
- switch(PureWaitingQueue.get(i).pInstructions.get(PureWaitingQueue.get(i).CurrentInstruction).IName) {
- case 'C':
- ReadyQueue.add(PureWaitingQueue.get(i));
- PureWaitingQueue.remove(PureWaitingQueue.get(i));
- break;
- case 'I':
- InputWaitingQueue.add(PureWaitingQueue.get(i));
- PureWaitingQueue.remove(PureWaitingQueue.get(i));
- break;
- case 'O':
- OutputWaitingQueue.add(PureWaitingQueue.get(i));
- PureWaitingQueue.remove(PureWaitingQueue.get(i));
- break;
- case 'W':
- PureWaitingQueue.add(PureWaitingQueue.get(i));
- PureWaitingQueue.remove(PureWaitingQueue.get(i));
- break;
- case 'H':
- PureWaitingQueue.remove(PureWaitingQueue.get(i));
- break;
- }
- }
- else {
- PureWaitingQueue.get(i).pInstructions.get(PureWaitingQueue.get(i).CurrentInstruction).IRemainTime--;
- }
- }
- }
-
- //所有用到CPU資源的PCB都沒了就清空這個(gè)控件
- if(ReadyQueue.size()==0) {
- textRunningProcess.setText("");
- }
-
- //每進(jìn)行一次調(diào)度就更新一次控件的信息
- String str1 = "";
- for(int i=0;i<ReadyQueue.size();i++) {
- str1 = str1+ReadyQueue.get(i).pname+"\r\n";
- }
- text_ready_queue.setText(str1);
-
- String str2 = "";
- for(int i=0;i<InputWaitingQueue.size();i++) {
- str2 = str2+InputWaitingQueue.get(i).pname+"\r\n";
- }
- text_iwait_queue.setText(str2);
-
- String str3 = "";
- for(int i=0;i<OutputWaitingQueue.size();i++) {
- str3 = str3+OutputWaitingQueue.get(i).pname+"\r\n";
- }
- text_owait_queue.setText(str3);
-
- String str4 = "";
- for(int i=0;i<PureWaitingQueue.size();i++) {
- str4 = str4+PureWaitingQueue.get(i).pname+"\r\n";
- }
- text_other_queue.setText(str4);
-
- count++;
-
- String schedule = "第"+String.valueOf(count)+"次調(diào)度情況:\r\n"+"就緒隊(duì)列:\r\n"+str1+"\r\n"+"輸入等待隊(duì)列:\r\n"+str2+"\r\n"
- +"輸出等待隊(duì)列:\r\n"+str3+"\r\n"+"其它等待隊(duì)列:\r\n"+str4+"\r\n";
- writeFile(schedule);
- }
- else {
- JOptionPane.showMessageDialog(null, "調(diào)度結(jié)束!");
- time.cancel();
- }
- }
- public static void writeFile(String text) {
- try {
- File writeName = new File("F:\\Java201903\\output.txt"); // 相對(duì)路徑,如果沒有則要建立一個(gè)新的output.txt文件
- // writeName.createNewFile(); // 創(chuàng)建新文件,有同名的文件的話直接覆蓋
- try (FileWriter writer = new FileWriter(writeName,true);//追加
- BufferedWriter out = new BufferedWriter(writer)
- ) {
- out.write(text);
- out.flush(); // 把緩存區(qū)內(nèi)容壓入文件
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
復(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)
2019-6-30 10:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|