操作系统课程 实验报告(完整版).docx
中 南 大 学操作系统实验报告姓 名:孙 福 星专 业 班 级:软件 1006班学 号: 完 成 日 期: 2011.11.22 进程调度与内存管理一、 实验目的在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就续进程个数大于处理器数时,就必须依照某种策略来决定哪些进程优先占用处理器。实验模拟实现处理机调度,以加深了解处理机调度的工作,并体会优先级和时间片轮转调度算法的具体实施方法。帮助了解在不同的存储管理方式下,应怎样实现主存空间的分配和回收。二、实验要求 1、可随机输入若干进程,并按优先权排序; 2、从就绪队首选进程运行:优先权-1/要求运行时间-1 要求运行时间=0时,撤销该进程3、重新排序,进行下轮调度。4、可随时增加进程;5、规定道数,设置后备队列和挂起状态。若内存中进程少于规定道数,可自动从后备队列调度一作业进入。被挂起进程入挂起队列,设置解挂功能用于将指定挂起进程解挂入就绪队列。6、每次调度后,显示各进程状态。7、自行假设主存空间大小,预设操作系统所占大小并构造未分分区表; 表目内容:起址、长度、状态(未分/空表目) 8、结合以上实验,PCB增加为:PID,要求运行时间,优先权,状态,所需主存大小,主存起始位置, PCB指针 9、采用最先适应算法分配主存空间;10、进程完成后,回收主存,并与相邻空闲分区合并。11、采用图形界面;三、实验内容选择一个调度算法,实现处理机调度。1、设计一个按优先权调度算法实现处理机调度的程序;2、设计按时间片轮转实现处理机调度的程序。3、主存储器空间的分配和回收。在可变分区管理方式下,采用最先适应算法实现主存空间的分配和回收。四、实验原理该模拟系统采用java语言实现,要实现的功能有新建进程、进程调度、挂起进程、解挂进程、删除进程,道数和时间片大小可以由用户自己调整,有两种调度策略:按优先权调度和按时间片轮转调度。每个进程可能有5种状态:新建(new)、就绪(ready)、运行(running)、阻塞(waiting)、挂起(suspend)。每个状态都有一个队列用来存放处于该状态的进程,不同的调度策略采用不同的队列实现。当创建进程时,如果内存中的进程数还没达到规定道数,则将新建进程插入就绪队列,如果内存中进程数已经达到规定道数,则插到后备队列,后备队列中的进程的状态为new。CPU每次调度时都从就绪队列中取进程,在进程执行过程中如果下一个操作时IO操作,则将进程插入到waiting队列。在系统运行过程中可以执行进程挂起操作,但执行的挂起操作时系统自动暂停运行,在弹出窗口选择要挂起的进程后,将选中的进程从原来的队列中删除并插入到挂起队列。进行解挂操作时将选中的进程从挂起队列中删除并插入该进程原来所处的队列。Ø 按优先级调度: 当选择按优先权调度时,所有队列都采用优先队列,优先队列采用一个有序链表实现,进程的优先权值越大代表优先级越高,优先队列中的进程按优先权从大到小排列,当新进程插入时根据该进程的优先权插入到队列中的合适位置,插入后保持队列按优先权从大到小排列,如果新进程与队列中某个进程优先权值相等,则该新进程插到那个进程后面,以遵循先来先服务的规则。当要从队列中取出进程时总是取队列中第一个进程,因为该进程的优先级最高。Ø 按时间片轮转调度:当选择按时间片轮转调度时,所有队列都采用先进先出队列,先进先出队列采用一个普通单向链表实现,当新进程插入时插入到队列的末尾,当要取进程时取队首进程,这样就实现了先进先出。Ø 内存管理该实验基于实验一完成,核心是内存的分配和回收,在实验一的基础上增加内存管理部分,在新建进程的时候增加一个输入内存大小的输入框,在进程进入内存时要分配内存,在进程销毁时要回收内存,如果进入内存时内存不足,则将进程插入到后备队列等待下次调度。系统维护一个内存表,每个表项代表一个空间,每个空间保存了该空间的起始地址和空间大小以及空间使用状态。初始时只有一个空间,当CPU启动时要分配内存,内存分配采用最先适应算法。回收内存时如果有相邻空闲空间,则要进行空闲空间合并。五、源代码及截图:1.divDTO:public class divDTOprivate int divBase;private int length;private int divFlag;public divDTO(int divBase,int length,int divFlag)this.divBase = divBase;this.divFlag = divFlag;this.length = length;public divDTO()public void setDivBase(int base)this.divBase = base;public int getDivBase()return this.divBase;public void setLength(int length)this.length = length;public int getLength()return this.length;public void setDivFlag(int flag)this.divFlag = flag;public int getDivFalg()return this.divFlag;2.PcbDTO:public class PcbDTOstatic final int Running = 1;static final int Ready = 2;static final int Waiting = 3;private String processName;private int runTime;private int prority;private int processState;private int base;private int limit;private int pcbFlag;public PcbDTO(String name, int time,int pro,int base,int limit)this.processName = name;this.runTime = time;this.prority = pro;this.processState = 0;this.limit = limit;this.base = base;public PcbDTO()this.pcbFlag = 0;public void setProcessName(String name)this.processName = name;public String getProcessName()return processName;public void setRunTime(int time)this.runTime = time;public int getRunTime()return this.runTime;public void setPrority(int prority)this.prority = prority;public int getPrority()return this.prority;public void setProcessState(int state)this.processState = state;public String getProcessState()String s = new String();if(this.processState = 1)s = "running"else if(this.processState = 2)s = "ready"else if(this.processState = 3)s = "waiting"return s;public int getBase()return this.base;public void setBase(int base)this.base = base;public void setLimit(int limit)this.limit = limit;public int getLimit()return this.limit;3. import javax.swing.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;public class MainFrame private JList readyList;private JList waitingList;private JList jobList;private JButton susButton;private JButton relaxButton;private JButton startButton;private JButton newButton;private JLabel nameLabel;private JLabel prorityLabel;private JLabel timeLabel;private JLabel jobLabel;private JLabel readyLabel;private JLabel waitingLabel;private JLabel runningLabel;private JLabel spaceLabel;private JLabel divLabel;private JLabel allocLabel;private JTable readyTable;private JTable runningTable;private JTable divTable;private JTable allocTable;private JTextField nameText;private JTextField timeText;private JTextField spaceText;private JComboBox prorityCom;private JPanel newPanel;private JPanel waitingPanel;private JPanel readyPanel;Vector jobVectorName;Vector jobDtoVector;Vector waitingVectorName;Vector waitingDtoVector;PcbDTO readyDtoArray;PcbDTO newDtoArray;divDTO divDtoArray;PcbDTO newSort;Object readydata;Object runningdata;Object divdata;Object allocdata;int first;int end;int point;PcbDTO a;public MainFrame() a = new PcbDTO();first = 0;end = 0;point = 0;JFrame jf = new JFrame("进程调度-ws");Container c = jf.getContentPane();c.setLayout(null);/ c.setBackground(Color.pink);newPanel = new JPanel();newPanel.setLayout(null);waitingPanel = new JPanel();waitingPanel.setLayout(null);/ waitingPanel.setBackground(Color.pink);readyPanel = new JPanel();readyPanel.setLayout(null);susButton = new JButton("挂起");relaxButton = new JButton("释放");startButton = new JButton("开始");newButton = new JButton("新建进程");nameLabel = new JLabel("进程名");prorityLabel = new JLabel("优先级");timeLabel = new JLabel("运行时间");jobLabel = new JLabel("后备队列");readyLabel = new JLabel("就绪队列");waitingLabel = new JLabel("等待队列");runningLabel = new JLabel("运行进程");spaceLabel = new JLabel("需要空间");divLabel = new JLabel("未分分区表");allocLabel = new JLabel("内存分配表");nameText = new JTextField();timeText = new JTextField();spaceText = new JTextField();prorityCom = new JComboBox();prorityCom.setToolTipText("优先级");readyDtoArray = new PcbDTO6;newSort = new PcbDTO6;for (int i = 0; i < 6; i+) newSorti = new PcbDTO();newDtoArray = new PcbDTO100;jobDtoVector = new Vector();jobVectorName = new Vector();waitingDtoVector = new Vector();waitingVectorName = new Vector();divDtoArray = new divDTO20;for (int i = 0; i < 20; i+) divDtoArrayi = new divDTO();divDtoArrayi.setDivFlag(0);divDtoArray0.setDivFlag(1);divDtoArray0.setDivBase(20);divDtoArray0.setLength(180);readydata = new Object64;runningdata = new Object23;divdata = new Object203;allocdata = new Object203;String col1 = "进程", "时间", "优先级", "状态" ;String col2 = "进程", "时间", "优先级" ;String col3 = "起址", "长度", "状态" ;String col4 = "起址", "长度", "占用进程" ;readyTable = new JTable(readydata, col1);/ readyTable.setEnabled(false);runningTable = new JTable(runningdata, col2);runningTable.setRowHeight(22);runningTable.setEnabled(false);allocTable = new JTable(allocdata, col4);allocTable.setEnabled(false);divTable = new JTable(divdata, col3);divTable.setEnabled(false);divTable.setValueAt(String.valueOf(20), 0, 0);divTable.setValueAt(String.valueOf(180), 0, 1);divTable.setValueAt(String.valueOf(1), 0, 2);JScrollPane runningSP = new JScrollPane();JScrollPane readySP2 = new JScrollPane();JScrollPane divSP = new JScrollPane();JScrollPane allocSP = new JScrollPane();runningSP.getViewport().add(runningTable);readySP2.getViewport().add(readyTable);divSP.getViewport().add(divTable);allocSP.getViewport().add(allocTable);/ int prorityArray = new int10;for (int i = 0; i < 10; i+) prorityCom.addItem(i);/ prorityArrayi = i;jobList = new JList();waitingList = new JList();JScrollPane readySP = new JScrollPane(readyList);JScrollPane jobSP = new JScrollPane(jobList);JScrollPane waitingSP = new JScrollPane(waitingList);newPanel.setSize(450, 100);newPanel.setLocation(0, 0);nameLabel.setSize(80, 20);nameLabel.setLocation(10, 5);nameText.setSize(100, 25);nameText.setLocation(10, 30);prorityLabel.setSize(80, 20);prorityLabel.setLocation(120, 5);prorityCom.setSize(100, 25);prorityCom.setLocation(120, 30);timeLabel.setSize(80, 20);timeLabel.setLocation(230, 5);timeText.setSize(100, 25);timeText.setLocation(230, 30);spaceLabel.setSize(80, 20);spaceLabel.setLocation(340, 5);spaceText.setSize(100, 25);spaceText.setLocation(340, 30);newButton.setSize(100, 20);newButton.setLocation(320, 70);waitingPanel.setSize(190, 410);waitingPanel.setLocation(0, 100);jobLabel.setSize(100, 20);jobLabel.setLocation(10, 2);jobSP.setSize(180, 105);jobSP.setLocation(10, 25);waitingLabel.setSize(100, 20);waitingLabel.setLocation(10, 129);waitingSP.setSize(180, 105);waitingSP.setLocation(10, 150);divLabel.setSize(100, 20);divLabel.setLocation(10, 253);divSP.setSize(180, 113);divSP.setLocation(10, 273);relaxButton.setSize(80, 20);relaxButton.setLocation(110, 388);readyPanel.setSize(260, 410);readyPanel.setLocation(190, 100);readyLabel.setSize(100, 22);readyLabel.setLocation(10, 2);allocLabel.setSize(100, 20);allocLabel.setLocation(10, 232);startButton.setSize(80, 20);startButton.setLocation(177, 388);susButton.setSize(80, 20);susButton.setLocation(95, 388);readySP2.setSize(250, 117);readySP2.setLocation(10, 25);runningLabel.setLocation(10, 142);runningLabel.setSize(100, 20);runningSP.setSize(250, 65);runningSP.setLocation(10, 167);allocSP.setSize(250, 130);allocSP.setLocation(10, 255);c.add(newPanel);newPanel.add(nameLabel);newPanel.add(nameText);newPanel.add(prorityLabel);newPanel.add(prorityCom);newPanel.add(timeText);newPanel.add(timeLabel);newPanel.add(newButton);newPanel.add(spaceLabel);newPanel.add(spaceText);c.add(waitingPanel);waitingPanel.add(jobLabel);waitingPanel.add(jobSP);waitingPanel.add(waitingLabel);waitingPanel.add(waitingSP);waitingPanel.add(divLabel);waitingPanel.add(divSP);waitingPanel.add(relaxButton);c.add(readyPanel);readyPanel.add(readyLabel);readyPanel.add(allocLabel);readyPanel.add(runningLabel);readyPanel.add(startButton);readyPanel.add(susButton);readyPanel.add(allocSP);readyPanel.add(runningSP);readyPanel.add(readySP2);jf.setSize(470, 550);jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);jf.setLocationRelativeTo(null);jf.setVisible(true);startButton.addActionListener(new MyActionListener();newButton.addActionListener(new MyActionListener();susButton.addActionListener(new MyActionListener();relaxButton.addActionListener(new MyActionListener();public void sus() try Thread.sleep(1000); catch (Exception ex) class MyActionListener implements ActionListener public void actionPerformed(ActionEvent e) int count = 0;PcbDTO test = new PcbDTO();JButton jb = (JButton) e.getSource();int max = -1;if (jb = startButton) / while(true)/ int runAllocFlag = -1;if (String) runningTable.getValueAt(0, 0) = null| (String) runningTable.getValueAt(0, 0) = "") try Thread.sleep(0); catch (Exception ex) / System.out.println("到3");for (int j = first; j != end;) if (!readyDtoArrayj.getProcessState().equals("waiting") max = j;break;j = (j + 1) % 6;for (int j = first; j % 6 != end;) if (!readyDtoArrayj.getProcessState().equals("waiting") if (readyDtoArrayj.getPrority() > readyDtoArraymax.getPrority() max = j;j = (j + 1) % 6;if (max >= 0) a = readyDtoArraymax;readyDtoArraymax = readyDtoArrayfirst;readyDtoArrayfirst = a;readyTable.setValueAt(readyDtoArraymax.getProcessName(), max, 0);readyTable.setValueAt(readyDtoArraymax.getRunTime(),max, 1);readyTable.setValueAt(readyDtoArraymax.getPrority(),max, 2);readyTable.setValueAt(readyDtoArraymax.getProcessState(), max, 3);readyTable.setValueAt("", first, 0);readyTable.setValueAt("", first, 1);readyTable.setValueAt("", first, 2);readyTable.setValueAt("", first, 3);runningTable.setValueAt(a.getProcessName(), 0, 0);runningTable.setValueAt(a.getRunTime(), 0, 1);runningTable.setValueAt(a.getPrority(), 0, 2);readyDtoArrayfirst.setRunTime(readyDtoArrayfirst.getRunTime() - 1);if (0 != readyDtoArrayfirst.getPrority() readyDtoArrayfirst.setPrority(readyDtoArrayfirst.getPrority() - 1);first = (first + 1) % 6; else System.out.println("cpu等待中"); else /* * try Thread.sleep(2000); catch(InterruptedException * e1) System.out.println(e1); */ System.out.println("到1");runningTable.setValueAt("", 0, 0);runningTable.setValueAt("", 0, 1);runningTable.setValueAt("", 0, 2);/ 如果运行时间为0则撤销进程,否则将进程重新添加到就绪队列中if (a.getRunTime() <= 0) / 收回内存空间for (int i = 0; i < point; i+) if (newSorti.getBase() >= a.getBase() newSorti = newSorti + 1;point-;/ 设置内存分配表的内容for (int i = 0; i < point; i+) allocTable.setValueAt(String.valueOf(newSorti.getBase(), i, 0);allocTable.setValueAt(String.valueOf(newSorti.getLimit(), i, 1);allocTable.setValueAt(newSorti.getProcessName(),i, 2);allocTable.setValueAt("", point, 0);allocTable.setValueAt("", point, 1);allocTable.setValueAt("", point, 2);/ 把收回的内存加入到记录未分分区的数组int memoryEnd = 0;int location = 0;int up = -1;/int down = -1;for (int i