版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验一 CPU调度一、实验内容 选择一个调度算法,实现处理机调度。二、实验目的 多道系统中,当就绪进程数大于处理机数时,须按照某种策略决定哪些进程优先占用处理机。本实验模拟实现处理机调度,以加深了解处理机调度的工作。三、实验题目 1、设计一个按优先权调度算法实现处理机调度的程序; 2、设计按时间片轮转实现处理机调度的程序。四、实验要求PCB内容: 进程名/PID; 要求运行时间(单位时间); 优先权; 状态: PCB指针;1、可随机输入若干进程,并按优先权排序;2、从就绪队首选进程运行:优先权-1/要求运行时间-1 要求运行时间=0时,撤销该进程3、重新排序,进行下轮调度;4、最好采用图形界面
2、;5、可随时增加进程;6、规定道数,设置后备队列和挂起状态。若内存中进程少于规定道数,可自动从后备队列调度一作业进入。被挂起进程入挂起队列,设置解挂功能用于将指定挂起进程解挂入就绪队列。7、每次调度后,显示各进程状态。五、实验源码/* * (#)CPUScheduling.java * * * author Liang Jiabin * version 1.00 2010/11/5 */import java.awt.*;import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionE
3、vent;import java.util.Vector;public class CPUScheduling extends Threadprivate JFrame frame;private JScrollPane reserveSp,waitingSp,finishSp;private JList reserveList, readyList, waitingList, finishList;private DefaultListModel reserveLm, readyLm, waitingLm, finishLm;private JLabel reserveLbl, readyL
4、bl, CPULbl, waitingLbl, finishLbl;private JTextField CPUTf;private JLabel needTimeLbl, priorityLbl; private JComboBox needTimeCb, priorityCb; private JButton newBtn, suspendBtn, unsuspendBtn, exitBtn;private JLabel rToRLbl,rToCPULbl,CPUToRLbl,CPUToWLbl,wToRLbl;private JLabel CPUToFLbl,warningLbl, ex
5、plainLbl;private int number = 1; /进程名序号private String CPUProcess = ;private String CPUProcessId = ;private String CPUProcessPriority = 0;private String CPUProcessTime = 0;private String maxPriorityProcess = ;private String maxPriorityProcessId = ;private String maxPriorityProcessTime = ;private Stri
6、ng maxPriorityProcessPriority = 0;private Vector waitingReadyV = new Vector(1,1); public CPUScheduling() frame = new JFrame(CPU Scheduling); Container c = frame.getContentPane(); c.setBackground(Color.pink); c.setLayout(null); reserveLbl = new JLabel(后备队列); reserveLbl.setBounds(50,20,60,30); c.add(r
7、eserveLbl); reserveLm = new DefaultListModel(); reserveList = new JList(reserveLm); reserveSp = new JScrollPane(reserveList); reserveSp.setBounds(30,50,100,200); c.add(reserveSp); rToRLbl = new JLabel(); rToRLbl.setBounds(145,100,60,30); c.add(rToRLbl); readyLbl = new JLabel(就绪队列); readyLbl.setBound
8、s(220,20,60,30); c.add(readyLbl); readyLm = new DefaultListModel(); readyList = new JList(readyLm); readyList.setFixedCellHeight(30); readyList.setBounds(200,50,100,200); c.add(readyList); rToCPULbl = new JLabel(); rToCPULbl.setBounds(320,50,50,30); c.add(rToCPULbl); CPUToRLbl = new JLabel(); CPUToR
9、Lbl.setBounds(320,65,50,30); c.add(CPUToRLbl); CPULbl = new JLabel(CPU); CPULbl.setBounds(410,20,50,30); c.add(CPULbl); CPUTf = new JTextField(20); CPUTf.setEditable(false); CPUTf.setBounds(370,55,100,30); c.add(CPUTf); CPUToWLbl = new JLabel(|); CPUToWLbl.setBounds(415,80,20,50); c.add(CPUToWLbl);
10、wToRLbl = new JLabel(); CPUToFLbl.setBounds(480,55,50,30); c.add(CPUToFLbl); finishLbl = new JLabel(完成队列); finishLbl.setBounds(560,20,60,30); c.add(finishLbl); finishLm = new DefaultListModel(); finishList = new JList(finishLm); finishSp = new JScrollPane(finishList); finishSp.setBounds(540,50,100,2
11、00); c.add(finishSp); needTimeLbl = new JLabel(要求运行时间); needTimeLbl.setBounds(10,330,100,30); c.add(needTimeLbl); String time = 1,2,3,4,5,6,7,8,9; needTimeCb = new JComboBox(time); needTimeCb.setEditable(true); needTimeCb.setBounds(95,330,60,30); c.add(needTimeCb); priorityLbl = new JLabel(优先权); pri
12、orityLbl.setBounds(175,330,50,30); c.add(priorityLbl); String priority = 1,2,3,4,5,6,7,8,9; priorityCb = new JComboBox(priority); priorityCb.setBounds(220,330,60,30); c.add(priorityCb); newBtn = new JButton(增加); newBtn.setBounds(120,380,60,30); c.add(newBtn); warningLbl = new JLabel(); warningLbl.se
13、tForeground(Color.RED); warningLbl.setBounds(360,350,200,30); c.add(warningLbl); suspendBtn = new JButton(挂起); suspendBtn.setBounds(340,380,60,30); c.add(suspendBtn); unsuspendBtn = new JButton(解挂); unsuspendBtn.setBounds(440,380,60,30); c.add(unsuspendBtn); exitBtn = new JButton(退出); exitBtn.setBou
14、nds(560,380,60,30); c.add(exitBtn); explainLbl = new JLabel(注:进程信息表示为 “进程名,需要运行时间,优先权”); explainLbl.setBounds(10,420,500,30); c.add(explainLbl); /Button事件注册 ButtonListener btnListener = new ButtonListener(); newBtn.addActionListener(btnListener); suspendBtn.addActionListener(btnListener); unsuspendB
15、tn.addActionListener(btnListener); exitBtn.addActionListener(btnListener); /调度器线程开启 this.start(); frame.setSize(720,520); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* * 开启线程 */ public synchronized void run() try while (true) /判断w
16、aiting队列是否有进程在等待进ready队列 while (readyLm.size()4 & waitingReadyV.size() != 0) String s = waitingReadyV.get(0).toString(); readyLm.addElement(s); wToRLbl.setForeground(Color.RED); this.sleep(500); wToRLbl.setForeground(Color.BLACK); waitingReadyV.remove(0); waitingLm.removeElement(s); /判断后备队列进程能否进就绪队列
17、 while (readyLm.size()=0; i-) s = readyLm.getElementAt(i).toString().split(,); if (Integer.parseInt(s2) = Integer.parseInt(CPUProcessPriority) maxPriorityProcess = readyLm.getElementAt(i).toString(); maxPriorityProcessId = s0; maxPriorityProcessTime = s1; maxPriorityProcessPriority = s2; index = i;
18、CPUProcessPriority = maxPriorityProcessPriority; if (!maxPriorityProcessId.equals(CPUProcessId) CPUProcess = maxPriorityProcess; CPUProcessId = maxPriorityProcessId; CPUProcessTime = maxPriorityProcessTime; CPUProcessPriority = maxPriorityProcessPriority; rToCPULbl.setForeground(Color.RED); CPUTf.se
19、tText(CPUProcessId); this.sleep(500); readyLm.removeElement(CPUProcess); rToCPULbl.setForeground(Color.BLACK); this.sleep(1000); /在CPU上运行一秒 /时间片到了,CPU上的进程转到就绪队列或完成队列 if (!CPUTf.getText().equals() CPUTf.setText(); int time = Integer.parseInt(CPUProcessTime); CPUProcessTime = String.valueOf(-time); if
20、 (Integer.parseInt(CPUProcessPriority) 1) /若优先级不为1则减1 int priority = Integer.parseInt(CPUProcessPriority); CPUProcessPriority = String.valueOf(-priority); if (Integer.parseInt(CPUProcessTime) != 0) /要求运行时间不为0则进入ready队列 CPUProcess = CPUProcessId + , + CPUProcessTime + , + CPUProcessPriority; readyLm.
21、addElement(CPUProcess); CPUToRLbl.setForeground(Color.RED); this.sleep(500); CPUTf.setText(); CPUToRLbl.setForeground(Color.BLACK); else /要求运行时间为0则转到完成队列 CPUToFLbl.setForeground(Color.RED); CPUProcessTime = String.valueOf(0); CPUProcessPriority = String.valueOf(0); CPUProcess = CPUProcessId + , + CP
22、UProcessTime + , + CPUProcessPriority; finishLm.addElement(CPUProcess); CPUProcessId = ; this.sleep(500); CPUToFLbl.setForeground(Color.BLACK); catch (Exception e) e.printStackTrace(); /* * 按钮监听器 */ class ButtonListener implements ActionListener public synchronized void actionPerformed(ActionEvent a
23、e) JButton source = (JButton)ae.getSource(); if (source = newBtn) /增加进程 String time = 1; time = needTimeCb.getSelectedItem().toString(); String priority = 1; priority = priorityCb.getSelectedItem().toString(); String pid = String.valueOf(number); String reserveItem = ; reserveItem = P + pid + , + time + , + priority; reserveLm.addElement(reserv
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中职第二学年(老年护理)实操技能阶段测试题及答案
- 2025年大学健康教育与促进(健康促进方法)试题及答案
- 2025年中职会计(税务会计)试题及答案
- 2025年高职计算机应用(数据处理技术)试题及答案
- 2025年中职电梯安装与维修保养(电梯安装技术)试题及答案
- 2025年高职养老服务应用(应用技术)试题及答案
- 2025年中职第二学年(环境监测技术)环境监测实训试题及答案
- 2025年高职(环境监测技术)化学分析实务试题及答案
- 2025年中职(物流设备基础综合实训)实操试题及答案
- 2025年大学植物生物学(植物分类)试题及答案
- 地坪漆施工方案范本
- 2025宁波市甬北粮食收储有限公司公开招聘工作人员2人笔试参考题库及答案解析
- 民政局笔试题及答案
- 二零二五版中医师承关系合同书
- 个人护理健康知识与技巧
- 《微积分与线性代数》课件
- 锅炉三大安全附件69课件讲解
- (湘美版)五年级上册书法指导练习教案
- 学习方法总结高效学习的技巧与方法
- 综合医院心身疾病诊治
- 港口安全生产管理模版
评论
0/150
提交评论