处理机调度算法(C#实现)

using System;

using System.Collections.Generic;

using System.Text;

namespace OS_1

{

class PCB

{

public PCB() //构造函数,初始化为0

{

time = 0;

priority = 0;

}

public void setTime(int time) //设置time

{

this.time = time;

}

public int getTime() //获得time

{

return time;

}

public void setPriority(int priority) //设置priority

{

this.priority = priority;

}

public int getPriority() //获得priority

{

return priority;

}

public void setId(int Id) //设置Id

{

this.Id = Id;

}

public int getId() //获得Id

{

return Id;

}

int time, priority, Id; //time表示运行时间,priority表示优先级

}

class chose

{

public void order(int[] num) //冒泡算法

{

int x = 0, y = 0, z = 0;

for (x = 0; x

for (y = 0; y

if (num[y]

{

z = num[y];

num[y] = num[y + 1];

num[y + 1] = z;

}

}

public void choseJC() //选择执行的进程

{

PCB[] myPCB = new PCB[5];

for (int i = 0; i

{

myPCB[i] = new PCB();

myPCB[i].setId(i);

}

for (int i = 0; i

{

try //输入非数字时捕获异常

{

Console.Write("请设置{0}号进程的运行时间:", i + 1);

int time = Int32.Parse(Console.ReadLine());

if (time

{

Console.WriteLine("\n请输入合理数值\n");

i--;

continue;

}

myPCB[i].setTime(time);

Console.WriteLine();

Console.Write("请设置{0}号进程的优先级:", i + 1);

int priority = Int32.Parse(Console.ReadLine());

if (priority

{

Console.WriteLine("\n请输入合理数值\n");

i--;

continue;

}

myPCB[i].setPriority(priority);

Console.WriteLine();

}

catch (Exception e)

{

//Console.WriteLine(e.Message);

Console.WriteLine("\n请输入合法数值!\n");

i--;

continue;

}

}

int[] orderPriority = new int[5]; //顺序存放优先级的数组 for (int i = 0; i

{

orderPriority[i] = myPCB[i].getPriority();

}

order(orderPriority); //排序

Console.WriteLine("\n\n\n");

Dictionary orderPriorityId =

new Dictionary(); //以“字典”形式存放优先级和Id

for (int i = 0; i

{

orderPriorityId.Add(myPCB[i].getPriority(), myPCB[i].getId());

}

Queue orderPCB = new Queue(); //以Id形式顺序存放进程的队列

for (int i = 0; i

{

int Id = orderPriorityId[orderPriority[i]] //通过优先级查“字典”得到Id

orderPCB.Enqueue(Id);

}

Console.Write("初始队列为: { ");

for (int i = 0; i

{

int j = (int)orderPCB.Dequeue();

Console.Write("{0}号 ", j + 1);

orderPCB.Enqueue(j);

}

Console.WriteLine("}\n");

Console.WriteLine("按进程优先级得到的进程执行顺序为:\n");

while (true)

{

try

{

int i = (int)orderPCB.Dequeue();

int time = myPCB[i].getTime() - 1;

Console.WriteLine("运行{0}号进程,剩余运行时间{1}\n", i + 1, time);

myPCB[i].setPriority(time);

myPCB[i].setTime(myPCB[i].getTime() - 1);

if (time != 0)

{

orderPCB.Enqueue(i);

}

Console.Write("目前队列中状态为: { "

for (i = 0; i

{

int j = (int)orderPCB.Dequeue();

Console.Write("{0}号 ", j + 1);

orderPCB.Enqueue(j);

}

Console.WriteLine("}\n");

}

catch (Exception e)

{

break;

}

}

}

}

class Program

{

static void Main(string[] args)

{

chose mychose = new chose();

mychose.choseJC();

}

}

);

}

思路:使用一个类存放进程的运行时间和优先级及进程Id。并定义长度为5的类数组。由另一个进行进程的选择,由用户设定5个进程的运行时间和优先级,当输入不合理数据时提示用户重新输入。完成后将5个进程的优先级存入一个数组中,使用冒泡法从大到小进程排序。使用“字典”这个数据结构将优先级设为key,进程Id设为value。从而根据优先级查字典得到对应的进程Id。然后用队列存储进程Id并依次出队,出队进程优先级减1,运行时间减1。如果运行时间不为0则插入到队尾。直至运行为0。当全部进程时间均为0时,程序结束。

数据结构及算法:使用两个类,一个类存放进程信息,一个处理进程得到应执行的进程。使用用一个数组存放进程优先级,并采用冒泡法从大到小进程排序。使用字“字典”存放优先级和进程Id。使用队列存放进程,并以出队方式表示进程执行

using System;

using System.Collections.Generic;

using System.Text;

namespace OS_1

{

class PCB

{

public PCB() //构造函数,初始化为0

{

time = 0;

priority = 0;

}

public void setTime(int time) //设置time

{

this.time = time;

}

public int getTime() //获得time

{

return time;

}

public void setPriority(int priority) //设置priority

{

this.priority = priority;

}

public int getPriority() //获得priority

{

return priority;

}

public void setId(int Id) //设置Id

{

this.Id = Id;

}

public int getId() //获得Id

{

return Id;

}

int time, priority, Id; //time表示运行时间,priority表示优先级

}

class chose

{

public void order(int[] num) //冒泡算法

{

int x = 0, y = 0, z = 0;

for (x = 0; x

for (y = 0; y

if (num[y]

{

z = num[y];

num[y] = num[y + 1];

num[y + 1] = z;

}

}

public void choseJC() //选择执行的进程

{

PCB[] myPCB = new PCB[5];

for (int i = 0; i

{

myPCB[i] = new PCB();

myPCB[i].setId(i);

}

for (int i = 0; i

{

try //输入非数字时捕获异常

{

Console.Write("请设置{0}号进程的运行时间:", i + 1);

int time = Int32.Parse(Console.ReadLine());

if (time

{

Console.WriteLine("\n请输入合理数值\n");

i--;

continue;

}

myPCB[i].setTime(time);

Console.WriteLine();

Console.Write("请设置{0}号进程的优先级:", i + 1);

int priority = Int32.Parse(Console.ReadLine());

if (priority

{

Console.WriteLine("\n请输入合理数值\n");

i--;

continue;

}

myPCB[i].setPriority(priority);

Console.WriteLine();

}

catch (Exception e)

{

//Console.WriteLine(e.Message);

Console.WriteLine("\n请输入合法数值!\n");

i--;

continue;

}

}

int[] orderPriority = new int[5]; //顺序存放优先级的数组 for (int i = 0; i

{

orderPriority[i] = myPCB[i].getPriority();

}

order(orderPriority); //排序

Console.WriteLine("\n\n\n");

Dictionary orderPriorityId =

new Dictionary(); //以“字典”形式存放优先级和Id

for (int i = 0; i

{

orderPriorityId.Add(myPCB[i].getPriority(), myPCB[i].getId());

}

Queue orderPCB = new Queue(); //以Id形式顺序存放进程的队列

for (int i = 0; i

{

int Id = orderPriorityId[orderPriority[i]] //通过优先级查“字典”得到Id

orderPCB.Enqueue(Id);

}

Console.Write("初始队列为: { ");

for (int i = 0; i

{

int j = (int)orderPCB.Dequeue();

Console.Write("{0}号 ", j + 1);

orderPCB.Enqueue(j);

}

Console.WriteLine("}\n");

Console.WriteLine("按进程优先级得到的进程执行顺序为:\n");

while (true)

{

try

{

int i = (int)orderPCB.Dequeue();

int time = myPCB[i].getTime() - 1;

Console.WriteLine("运行{0}号进程,剩余运行时间{1}\n", i + 1, time);

myPCB[i].setPriority(time);

myPCB[i].setTime(myPCB[i].getTime() - 1);

if (time != 0)

{

orderPCB.Enqueue(i);

}

Console.Write("目前队列中状态为: { "

for (i = 0; i

{

int j = (int)orderPCB.Dequeue();

Console.Write("{0}号 ", j + 1);

orderPCB.Enqueue(j);

}

Console.WriteLine("}\n");

}

catch (Exception e)

{

break;

}

}

}

}

class Program

{

static void Main(string[] args)

{

chose mychose = new chose();

mychose.choseJC();

}

}

);

}

思路:使用一个类存放进程的运行时间和优先级及进程Id。并定义长度为5的类数组。由另一个进行进程的选择,由用户设定5个进程的运行时间和优先级,当输入不合理数据时提示用户重新输入。完成后将5个进程的优先级存入一个数组中,使用冒泡法从大到小进程排序。使用“字典”这个数据结构将优先级设为key,进程Id设为value。从而根据优先级查字典得到对应的进程Id。然后用队列存储进程Id并依次出队,出队进程优先级减1,运行时间减1。如果运行时间不为0则插入到队尾。直至运行为0。当全部进程时间均为0时,程序结束。

数据结构及算法:使用两个类,一个类存放进程信息,一个处理进程得到应执行的进程。使用用一个数组存放进程优先级,并采用冒泡法从大到小进程排序。使用字“字典”存放优先级和进程Id。使用队列存放进程,并以出队方式表示进程执行


相关内容

  • 处理机调度算法的比较
  • 处理机调度算法的比较 计算机科学学院 计算机科学与技术 2009 摘要:处理机调度基本概念.调度算法优劣的评价准则.多种处理机调度算法的介绍 引言 操作系统是处理计算机硬件的一层软件和作为计算机用户与计算机硬件的中间的协调者.操作系统的CPU调度器负责给各个任务分发CPU带宽资源.调度算法负责管理当 ...

  • 操作系统课程设计--进程调度算法
  • 计算机科学与应用系 操作系统原理 课程设计报告 题目 进程调度算法 学号 班级 姓名 专业 计算机科学与技术 指导教师 完成日期 进程调度算法 一.实验目的 通过优先权法与轮转调度算法的模拟加深对进程概念和进程调度过程的理解,掌握进程状态之间的切换,同时掌握进程调度算法的实现方法和技巧. 二.实验内 ...

  • 进程调度算法简介
  • 姓名:王思文 物电学院通信一班 学号:[**************]5 进程调度算法简介 进程调度算法就进程来分,分为三类:批处理.交互式.实时.下面将分别进行描述. 批处理系统 先到先服务 这种调度算法属于非抢占式,只有当前进程主动放弃处理器别的进程才会有机会运行.这个算法只有一个运行队列,一个 ...

  • 广工操作系统2015实验报告
  • 实 验 报 告 课程名称操作系统实验 学生学院计算机学院 专业班级计算机科学与技术 学 号 学生姓名 指导教师 孙为军 2015 年12月30日 实验一 进程调度 一.实验目的 编写并调试一个模拟的进程调度程序,以加深对进程的概念及进程调度算法的理解. 二.实验内容 1. 采用"短进程优先 ...

  • 短作业优先调度算法
  • 青岛理工大学 操作系统课程设计报告 院(系): 计算机工程学院 专业: 计算机科学与技术专业 学生姓名: 班级:__ 学号: 题目: 短作业优先调度算法的进程调度程序_ 起迄日期: ___ _ ____ 设计地点: 指 导 教 师: 2011-2012年度 第 1 学期 完成日期: 2012 年 1 ...

  • 北京邮电大学软件工程招生资料
  • 北京邮电大学 招生简章 北京邮电大学网址: http://www.bupt.edu.cn 北京邮电大学研究生招生网址:http://www.yzb.bupt.cn 单位代码:10013 通讯地址:北京市海淀区西土城路10号北京邮电大学研究生招生办公室 邮政编码:100876 研究生招生办公室地址:学 ...

  • 操作系统实习报告样本
  • 1 操作系统实习报告内容(1) 基本信息:完成人姓名、学号、报告日期(2) 实习内容(3) 实习目的(4) 实习题目(5) 设计思路和流程图(6) 主要数据结构及其说明(7) 源程序并附上注释(8) 程序运行时的初值和运行结果(9) 实习体会:实习中遇到的问题及解决过程、实习中产生的错误及原因分析、 ...

  • 磁盘移臂调度过程模拟设计-电梯算法,最短寻道时间优先
  • 课 程 设 计 题 目 学 院 专 业 班 级 姓 名 指导教师 磁盘移臂调度过程模拟设计 --电梯算法.最短寻道时间优先算法 计算机科学与技术学院 计算机科学与技术 2011 年 1 月 20 日 课程设计任务书 学生姓名: 专业班级: 计算机科学与技术班 指导教师: 工作单位: 计算机科学与技术 ...

  • Linux进程调度:CFS调度器的设计框架
  • 1. 概述 对于分时操作系统而言,表面上看起来是多个进程同时在执行,而在系统内部则进行着从一个进程到另一个进程的切换动作.这样的进程并发执行涉及到进程切换(process switch)和进程调度(process scheduling)两大问题.其中进程调度是操作系统的核心功能,它是一个非常复杂的过 ...

  • 计算机考研知识点
  • 计算机学科专业基础综合 Ⅰ考查目标 计算机学科专业基础综合考试涵盖数据机构.计算机组成原理.操作系统和计算机网络等学科专业基础课程.要求考生比较系统地掌握上述专业基础课程的概念.基本原理和方法,能够运用所学的基本原理和基本方法分析.判断和解决有关理论问题和实际问题. Ⅱ考试形式和试卷结构 一.试卷满 ...