操作系統(tǒng)實驗一_第1頁
操作系統(tǒng)實驗一_第2頁
操作系統(tǒng)實驗一_第3頁
操作系統(tǒng)實驗一_第4頁
操作系統(tǒng)實驗一_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、中南大學(xué)操作系統(tǒng)實驗報告 姓名: 學(xué)號: 班級: 軟件工程指導(dǎo)老師: 胡志剛完成時間: 2014-11實驗1CPU Scheduling實驗學(xué)時: 2 實驗地點: 二綜204 實驗日期: 2014/11/19 一、實驗?zāi)康亩嗟老到y(tǒng)中,當(dāng)就緒進程數(shù)大于處理機數(shù)時,須按照某種策略決定哪些進程優(yōu)先占用處理機。本實驗?zāi)M實現(xiàn)處理機調(diào)度,加深了解處理機調(diào)度的工作過程。二、實驗內(nèi)容選擇或者自行設(shè)計一個調(diào)度算法,實現(xiàn)處理機調(diào)度。三、實驗要求構(gòu)建PCB,內(nèi)容至少涵蓋: 進程名/PID; 要求運行時間(單位時間); 優(yōu)先權(quán); 狀態(tài): PCB指針;1、可隨機輸入若干進程,并按優(yōu)先權(quán)排序2、采用動態(tài)優(yōu)先權(quán)調(diào)度,從就

2、緒隊首選進程運行: 優(yōu)先權(quán)-1/要求運行時間-1 要求運行時間為0時,撤銷該進程3、重新排序,進行下輪調(diào)度4、最好采用圖形界面5、可動態(tài)增加進程6、規(guī)定道數(shù),設(shè)置后備隊列和掛起狀態(tài)7、如果內(nèi)存中進程數(shù)少于規(guī)定道數(shù),可自動從后備隊列通過作業(yè)調(diào)度選擇一作業(yè)進入,8、作業(yè)調(diào)度算法可自行選擇9、被掛起進程入掛起隊列,設(shè)置解掛功能用于將指定掛起進程解掛并入就緒隊列10、每次調(diào)度后,顯示各進程狀態(tài)。四、實驗代碼Pcb:進程控制塊,用于保存進程的信息)Reading:就緒隊列,進入cpu中執(zhí)行,6,按優(yōu)先級順序,時間片為1sReserve:后備隊列,存放進程,當(dāng)就緒隊列中進程數(shù)不足6個,從該隊列調(diào)出Susp

3、end:掛起隊列,就緒隊列中的進程掛起后進入該隊列PcbGUI:程序界面以及執(zhí)行的核心類Pcb:public class Pcb private String name; /進程名稱private int time; /時間private int priority; /優(yōu)先級,越大表明優(yōu)先級越高public Pcb(String n , int t, int p)=n;this.time=t;this.priority=p;public String getName() return name;public void setName(String name) this.nam

4、e = name;public int getTime() return time;public void setTime(int time) this.time = time;public int getPriority() return priority;public void setPriority(int priority) this.priority = priority;public void change()this.time-;this.priority-;public String toString() return this.getName() + "_"

5、; + this.getTime() + "_" + this.getPriority() + "n"類Reading:import java.util.ArrayList;import java.util.Iterator;public class Reading implements Iterable<Pcb> private ArrayList<Pcb> readlist; public ArrayList<Pcb> getPCBItems() return this.readlist; public Readi

6、ng() this.readlist = new ArrayList<Pcb>(); public void addItem(Pcb PcbItem) this.readlist.add(PcbItem); public void removeItem(Pcb PCbItem) this.readlist.remove(PCbItem); public Pcb getItem(Pcb processPCB) for (Pcb pCbItem : this.readlist) if (pCbItem.equals(processPCB) return pCbItem; return

7、null; public Pcb getItem(String pid) for (Pcb pcBItem : this.readlist) if (pcBItem.getName().equals(pid) return pcBItem; return null; public int getNumberOfItems() return this.readlist.size(); public String getItemsProperties() String itemsProperties = new StringgetNumberOfItems(); int i = 0; for(It

8、erator iterator1 = readlist.iterator(); iterator1.hasNext();) Pcb stu_Item = (Pcb)iterator1.next(); itemsPropertiesi+ = stu_Item.toString(); return itemsProperties; public Iterator<Pcb> iterator() return this.readlist.iterator();類Reserve:import java.util.ArrayList;import java.util.Iterator;pub

9、lic class Reserve implements Iterable<Pcb>private ArrayList<Pcb> reservelist;public Reserve()reservelist = new ArrayList<Pcb>();public Iterator<Pcb> iterator()return this.reservelist.iterator();public void add(Pcb o)reservelist.add(o);public Pcb getfirst(int i)return reservel

10、ist.get(i);public Pcb getprocess(String n)for(Pcb o : reservelist)if(n.equals(o.getName()return o;return null;public int getNumberOfItems() return this.reservelist.size();public void removeItem(Pcb PCbItem) this.reservelist.remove(PCbItem);public String getItemsProperties() String itemsProperties =

11、new StringgetNumberOfItems(); int i = 0; for(Iterator iterator1 = reservelist.iterator(); iterator1.hasNext();) Pcb stu_Item = (Pcb)iterator1.next(); itemsPropertiesi+ = stu_Item.toString(); return itemsProperties;類Suspend:import java.util.ArrayList;import java.util.Iterator;public class Suspend imp

12、lements Iterable<Pcb>private ArrayList<Pcb> suspendlist;public Suspend()suspendlist = new ArrayList<Pcb>();public void add(Pcb o)this.suspendlist.add(o);public Iterator<Pcb> iterator() return this.suspendlist.iterator();public Pcb getprocess(String n)for(Pcb o : suspendlist)i

13、f(n.equals(o.getName()return o;return null;public int getNumberOfItems() return this.suspendlist.size();public void removeItem(Pcb PCbItem) this.suspendlist.remove(PCbItem); public String getItemsProperties() String itemsProperties = new StringgetNumberOfItems(); int i = 0; for(Iterator iterator1 =

14、suspendlist.iterator(); iterator1.hasNext();) Pcb stu_Item = (Pcb)iterator1.next(); itemsPropertiesi+ = stu_Item.toString(); return itemsProperties;public Pcb getItem(String pid) for (Pcb pcBItem : this.suspendlist) if (pcBItem.getName().equals(pid) return pcBItem; return null; 類PcbGUI:import java.a

15、wt.*;import java.util.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class PcbGUI extends JPanel/* Window width in pixels */static private int WIDTH = 830;/* Window height in pixels */static private int HEIGHT = 600 ;static private int RESERVE_LIST_SIZE = 110;static

16、 private int RESERVE_LIST_ROWS = 15;static private int READ_LIST_SIZE = 110;static private int READ_LIST_ROWS = 15;static private int SUSPEND_LIST_SIZE = 110;static private int SUSPEND_LIST_ROWS = 14;static private int MEMORY_LIST_SIZE = 110;static private int MEMORY_LIST_ROWS = 15;private static JT

17、extField PnameTextField;private JTextField PtimeTextField;private JTextField PpriorityTextField;private JTextField LimitTextField;private JTextField run;private JTextArea STextArea;private JButton Addbutton;private JButton Beginbutton;private JButton Pausebutton;private JButton Suspendbutton;private

18、 JButton Exitbutton;private JButton Hsbutton;private JList ReserveList;private JList ReadList;private JList SuspendList;private JList MemoryList;private Reserve reslist; /后備隊列private Reading realist; /就緒隊列private Suspend slist; /掛起隊列private int Pause; /Pause=1表示運行,Pause=0表示暫停private boolean flag = f

19、alse; /控制線程運行public static void main(String args) / TODO Auto-generated method stubJFrame frame = new JFrame("CPU Scheduling");frame.setContentPane(new PcbGUI();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(WIDTH, HEIGHT);frame.setResizable(true);frame.setVisible(true)

20、;frame.setLocationRelativeTo(null);PnameTextField.requestFocus();public PcbGUI()Pause = 1;reslist = new Reserve();realist = new Reading();slist = new Suspend();Font x = new Font("Serif",1,15);Font f=new Font("楷體",Font.PLAIN,14);/設(shè)置后備隊列ReserveList = new JList();ReserveList.setSele

21、ctionMode(ListSelectionModel.SINGLE_SELECTION);ReserveList.setVisibleRowCount(RESERVE_LIST_ROWS);ReserveList.setFixedCellWidth(RESERVE_LIST_SIZE);JPanel ProcessPanel1 = new JPanel();ProcessPanel1.setBorder(BorderFactory.createTitledBorder("ReserveList");ProcessPanel1.add (new JScrollPane(R

22、eserveList,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);ProcessPanel1.setBounds(10, 10, 150, 370);ProcessPanel1.setBackground(null);/設(shè)置就緒隊列ReadList = new JList();ReadList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);ReadList.setVisibleRowCount(READ_LIST_ROWS

23、);ReadList.setFixedCellWidth(READ_LIST_SIZE);JPanel ProcessPanel2 = new JPanel();ProcessPanel2.setBorder(BorderFactory.createTitledBorder("ReadingList");ProcessPanel2.add (new JScrollPane(ReadList,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);ProcessPanel2.s

24、etBounds(160, 10, 150, 370);/設(shè)置運行框JPanel Run = new JPanel();Run.setBorder(BorderFactory.createTitledBorder("Running");run = new JTextField(8);run.setHorizontalAlignment(JTextField.CENTER);run.setBorder(BorderFactory.createEmptyBorder();run.setFont(x);run.setEditable(false);Run.add(run);Run

25、.setBounds(310, 10, 200, 80);/設(shè)置添加JPanel AddPanel = new JPanel();AddPanel.setLayout(null);AddPanel.setBorder(BorderFactory.createTitledBorder("Add");AddPanel.setBounds(310,100,200,280);Addbutton = new JButton("添加(A)");Addbutton.setMnemonic(KeyEvent.VK_A);Addbutton.setFont(f);Addb

26、utton.setBounds(65, 220, 90, 35);PnameTextField = new JTextField(5);PtimeTextField = new JTextField(5);PpriorityTextField = new JTextField(5);LimitTextField = new JTextField(5);PnameTextField.setHorizontalAlignment(JTextField.CENTER);PtimeTextField.setHorizontalAlignment(JTextField.CENTER);Ppriority

27、TextField.setHorizontalAlignment(JTextField.CENTER);LimitTextField.setHorizontalAlignment(JTextField.CENTER);PnameTextField.setBounds(110, 35, 60, 23);PtimeTextField.setBounds(110, 75, 60, 23);PpriorityTextField.setBounds(110, 115, 60, 23);LimitTextField.setBounds(110, 155, 60, 23);JLabel NameLabel

28、= new JLabel("Name");JLabel TimeLabel = new JLabel("Time");JLabel PriorityLabel = new JLabel("Priority");JLabel LimitLabel = new JLabel("Limit");NameLabel.setBounds(30, 35, 60, 23);TimeLabel.setBounds(30, 75, 60, 23);PriorityLabel.setBounds(30, 115, 60, 23);Li

29、mitLabel.setBounds(30, 155, 60, 23);AddPanel.add(Addbutton);AddPanel.add(PnameTextField);AddPanel.add(PtimeTextField);AddPanel.add(PpriorityTextField);AddPanel.add(LimitTextField);AddPanel.add(NameLabel);AddPanel.add(TimeLabel);AddPanel.add(PriorityLabel);AddPanel.add(LimitLabel);/設(shè)置掛起隊列SuspendList

30、= new JList();SuspendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);SuspendList.setVisibleRowCount(SUSPEND_LIST_ROWS);SuspendList.setFixedCellWidth(SUSPEND_LIST_SIZE);JPanel ProcessPanel3 = new JPanel();ProcessPanel3.setBorder(BorderFactory.createTitledBorder("SuspendList");JPa

31、nel ProcessPanel4 = new JPanel();ProcessPanel4.add (new JScrollPane(SuspendList,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);Hsbutton = new JButton("解掛(U)");Hsbutton.setMnemonic(KeyEvent.VK_U);Hsbutton.setFont(f);ProcessPanel3.add(ProcessPanel4,BorderLayout

32、.NORTH);ProcessPanel3.add(Hsbutton,BorderLayout.SOUTH);ProcessPanel3.setBounds(510, 10, 150, 370);/設(shè)置開始按鈕Beginbutton = new JButton("開始(B)");Beginbutton.setBounds(650, 415, 90, 35);Beginbutton.setFont(f);Beginbutton.setMnemonic(KeyEvent.VK_B);/設(shè)置暫停按鈕Pausebutton = new JButton("暫停(P)&quo

33、t;);Pausebutton.setBounds(470, 415, 90, 35);Pausebutton.setFont(f);Pausebutton.setMnemonic(KeyEvent.VK_P);/設(shè)置掛起按鈕Suspendbutton = new JButton("掛起(S)");Suspendbutton.setBounds(470, 490, 90, 35);Suspendbutton.setFont(f);Suspendbutton.setMnemonic(KeyEvent.VK_S);/設(shè)置退出按鈕Exitbutton = new JButton(

34、"退出(X)");Exitbutton.setBounds(650, 490, 90, 35);Exitbutton.setFont(f);Exitbutton.setMnemonic(KeyEvent.VK_X);/設(shè)置備注STextArea = new JTextArea();STextArea.setBounds(20,18,310,115);STextArea.setFont(new Font("Calibri", Font.PLAIN, 18);STextArea.setBackground(null);STextArea.setEditabl

35、e(false);JPanel SPanel = new JPanel();SPanel.setLayout(null);SPanel.setBorder(BorderFactory.createTitledBorder("Status");SPanel.setBounds(10, 390, 350, 140);SPanel.add(STextArea);setLayout(null);add(ProcessPanel1);add(ProcessPanel2);add(Run);add(AddPanel);add(ProcessPanel3);add(Beginbutton

36、);add(Pausebutton);add(Suspendbutton);add(Exitbutton);/初始添加一些進程realist.addItem(new Pcb("a",3,4);realist.addItem(new Pcb("b",4,6);realist.addItem(new Pcb("c",8,9);sortReadyPCB();reslist.add(new Pcb("d",3,4);reslist.add(new Pcb("e",3,4);reslist.add(new

37、 Pcb("f",3,4);reslist.add(new Pcb("g",3,4);reslist.add(new Pcb("h",3,4);ReserveList.setListData(reslist.getItemsProperties(); /消息響應(yīng)函數(shù)Addbutton.addActionListener(new AddListener();Exitbutton.addActionListener(new ExitListener();Beginbutton.addActionListener(new BeginList

38、ener();Suspendbutton.addActionListener(new SuspendListener();Pausebutton.addActionListener(new PauseListener();Hsbutton.addActionListener(new HsListener();ReadList.addListSelectionListener(new DisplayListener();ReserveList.addListSelectionListener(new DisplayListener2();SuspendList.addListSelectionL

39、istener(new DisplayListener1();/對就緒隊列按優(yōu)先級進行排序public void sortReadyPCB() Reading currentReadyPCB = new Reading();int num = realist.getNumberOfItems();if(num > 0) for(int i=num; i>=1; i-) Iterator readyIterator = realist.iterator();Pcb currentItem = (Pcb) readyIterator.next();for( ; readyIterato

40、r.hasNext(); ) Pcb nowItem = (Pcb) readyIterator.next();if(currentItem.getPriority() < nowItem.getPriority() currentItem = null;currentItem = nowItem;currentReadyPCB.addItem(currentItem);realist.removeItem(currentItem);realist = null;realist = currentReadyPCB;ReadList.setListData(realist.getItems

41、Properties(); /線程,CPU處理進程class newthread extends Threadint t;int TimeSlice;boolean Flag = true;public void run()while(Flag)if(getPause() = 1)while(getPause() = 1)try new Thread().sleep(1000); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();if(realist.getNumberOfIt

42、ems() < 6)if(reslist.getNumberOfItems() > 0) while(realist.getNumberOfItems() < 6) if(reslist.getNumberOfItems() > 0) Iterator bwackupPCBIterator = reslist.iterator();Pcb newItem = (Pcb) bwackupPCBIterator.next();while(bwackupPCBIterator.hasNext() ) newItem = (Pcb) bwackupPCBIterator.nex

43、t();if(newItem = null) break;if(newItem != null) Pcb nowItem = new Pcb(newItem.getName(), newItem.getTime(), newItem.getPriority();realist.addItem(nowItem);reslist.removeItem(newItem);ReserveList.setListData(reslist.getItemsProperties(); else break;sortReadyPCB();ReserveList.setListData(reslist.getI

44、temsProperties();Iterator readyIterator = realist.iterator();Pcb runningItem = null;if(readyIterator.hasNext() runningItem = (Pcb) readyIterator.next();if(runningItem != null) t = runningItem.getTime();TimeSlice = 1;for(int i=TimeSlice; i>0; i-) if(t > 0) runningItem.change();run.setText(runni

45、ngItem.getName();t-;sortReadyPCB();try Thread.sleep(1000); catch (Exception ee) ee.printStackTrace();if(t <= 0) Flag = false;realist.removeItem(runningItem);runningItem = null;sortReadyPCB();if(realist.getNumberOfItems() > 0) Flag = getFlag();else Flag = false; else Flag = false;if(Pause = 1)

46、while(getPause()=1) try Thread.sleep(1000); catch (Exception ee) ee.printStackTrace();if(getPause() = 0) Flag = true;public boolean getFlag() return this.flag;public int getPause()return Pause;/添加按鈕的消息響應(yīng)函數(shù)class AddListener implements ActionListener public void actionPerformed(ActionEvent event)Strin

47、g str1 = PnameTextField.getText();int n1 = Integer.parseInt(PtimeTextField.getText();int n2 = Integer.parseInt(PpriorityTextField.getText();if(realist.getNumberOfItems()<6)realist.addItem(new Pcb(str1,n1,n2);elsereslist.add(new Pcb(str1,n1,n2);ReadList.setListData(realist.getItemsProperties();Res

48、erveList.setListData(reslist.getItemsProperties();PnameTextField.requestFocus();PnameTextField.selectAll();/退出按鈕的消息響應(yīng)函數(shù)class ExitListener implements ActionListener public void actionPerformed(ActionEvent event) System.exit(0);/開始按鈕的消息響應(yīng)函數(shù)class BeginListener implements ActionListener public void actionPerformed(ActionEvent event) ReadList.setListData(realist.getItemsProperties();Pause = 0;Thread run = new newthread();flag = true;run.start();/掛起按鈕的消息響應(yīng)函數(shù)class SuspendListener implements ActionListener public void actionPerformed(ActionEvent event) String selectedReadyItem = null;Stri

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論