【C#可视化|VS】操作系统简单轮转法程序模拟,基于Lsit操作
目录
实验课题:
实验代码(C#部分):
实验数据样例选取:
实验预估流程展示:
实验思路:
数据封装及申明:
界面初始化及清楚方块界面:
画面描绘(不变):
画面描绘(可变+核心):
主函数代码:
实验总代码(全):
实验效果:
实验课题:
简单轮转调度:进程就绪队列按各进程进入的先后顺序排列。进程每次所需处理机的轮转式按其重要程度记入进程控制块中的轮转时间片数记录项。进程执行时,每运行一个时间片,进程还需要的时间片数减2,运行进程占用处理机的时间片数加2,然后比较占用CPU的时间片数是否与该进程的轮转时间片数相等,若相等则说明已达到轮转时间,应将现运行的进程排列就绪队列的末尾,调度队列上的首进程运行,且改变它们的进程状态,直至所有进程完成各自的时间片。
实验代码(C#部分):
实验数据样例选取:
如下为进程进度(以时间片N为2):(不想看的可以直达最后哦)
| 进程名 | 到达进程时间 | 运行时间 |
| A | 0 | 4 |
| B | 1 | 3 |
| C | 2 | 5 |
| D | 3 | 2 |
| E | 4 | 4 |
实验的效果是将根据从队列的头一个划分为一段时间片,再根据简单轮转....(无需考虑权重,从第一个转向最后一个,不累赘,都懂~打字太麻烦)。
实验预估流程展示:
| 调度1 | 调度2 | 调度3 | 调度4 | 调度5 | 调度6 | 调度7 | 调度8 | 调度9 | 调度10 | |
| A | 2 | 2 | 2 | 2 | 2 | 4=4 | ||||
| B | 0 | 2 | 2 | 2 | 2 | 2 | 3=3 | |||
| C | 0 | 0 | 2 | 2 | 2 | 2 | 2 | 4 | 4 | 5=5 |
| D | 0 | 0 | 0 | 2=2 | ||||||
| E | 0 | 0 | 0 | 0 | 2 | 2 | 2 | 2 | 4=4 |
实验思路:
C#原用List封装“A,B,C,D,E”多项元组,然后进程中再额外申明一个List来实现表达。
数据封装及申明:
申明E_nodeState的状态(等待,跑,完成)
enum E_nodeState{wait,//等待runn,//跑finish,//完成时}
申明结点(名称,状态,需要运行时间,已运行时间,到达时间,有无进入进程,有无完成)
class LinkNode{public string linkname;public E_nodeState state; //状态public int ntime; //总需要运行时间public int rtime; //已运行时间public int arrivetime;//到达时间public bool isInserted;//记录有无进入进程public bool isCompleted;//记录有无运行完成public LinkNode(string linkname,int ntime)//结点初始化{this.linkname = linkname;this.state = E_nodeState.wait;this.ntime = ntime;this.rtime = 0;this.isInserted = false;this.isCompleted = false;}public LinkNode(string linkname, int ntime, int arrivetime) : this(linkname,ntime)//结点初始化{this.arrivetime = arrivetime;}public void GetRunTime(int value)//外部可得,后来想想可以放置为属性{this.rtime = value;}public void DisPlaying()//结点展示{Console.WriteLine(linkname + " " + state + " " + arrivetime+" "+ntime + " "+rtime);}private void ChooseColor(E_nodeState state){switch (state){case E_nodeState.wait:Console.ForegroundColor = ConsoleColor.Blue;break;case E_nodeState.runn:Console.ForegroundColor = ConsoleColor.Yellow;break;case E_nodeState.finish:Console.ForegroundColor = ConsoleColor.Magenta;break;}}public void DrawT(int width_index1,int _height)//结点绘制方块{Console.ForegroundColor = ConsoleColor.White;Console.SetCursorPosition(width_index1, _height - 12);Console.Write(this.linkname);for(int i = 0; i < this.rtime; i++){Console.SetCursorPosition(width_index1, _height - 13-i);//Console.Write(" ");ChooseColor(this.state);Console.Write("■");}}}
界面初始化及清楚方块界面:
static void ConsoleInit(int _width, int _height)//基础设置{//光标隐藏Console.CursorVisible = false;//舞台大小Console.SetWindowSize(_width, _height);Console.SetBufferSize(_width, _height);
}static void ClearT(int _width, int _height)
{for (int i = 1; i < _height - 11; i++){Console.SetCursorPosition(2, i);Console.Write(" ");}
}
画面描绘(不变):
绘制墙体基础信息:
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < _width; i += 2)//行绘制
{Console.SetCursorPosition(i, 0);Console.Write("■");Console.SetCursorPosition(i, _height - 10);Console.Write("■");Console.SetCursorPosition(i, _height - 1);Console.Write("■");
}
for (int i = 0; i < _height; i += 1)//列绘制
{Console.SetCursorPosition(0, i);Console.Write("■");Console.SetCursorPosition(_width - 2, i);Console.Write("■");
}
绘制基础文字信息:
int width_index = (_width - 4) / LInkList.Count - LInkList.Count / 2;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(width_index, _height - 11);
Console.Write("■"+"RUNNING" + " ");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("■" + "WAITING" + " ");Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(width_index, _height - 9);//基础信息
Console.WriteLine("Pname" + " " + "state" + " " + "artime" + " " + "ntime" + " " + "rtime");
总代码:
static void UnChangedeDraw(int _width, int _height, List LInkList, List InRunningList)//绘制墙体基础信息{Console.ForegroundColor = ConsoleColor.Red;for (int i = 0; i < _width; i += 2){Console.SetCursorPosition(i, 0);Console.Write("■");Console.SetCursorPosition(i, _height - 10);Console.Write("■");Console.SetCursorPosition(i, _height - 1);Console.Write("■");}for (int i = 0; i < _height; i += 1){Console.SetCursorPosition(0, i);Console.Write("■");Console.SetCursorPosition(_width - 2, i);Console.Write("■");}int width_index = (_width - 4) / LInkList.Count - LInkList.Count / 2;Console.ForegroundColor = ConsoleColor.Yellow;Console.SetCursorPosition(width_index, _height - 11);Console.Write("■"+"RUNNING" + " ");Console.ForegroundColor = ConsoleColor.Blue;Console.Write("■" + "WAITING" + " ");//Console.ForegroundColor = ConsoleColor.Magenta;//这里偷懒了,懒得写Finished,有想写的友人可以尝试//Console.Write("■" + "FINISHED" );Console.ForegroundColor = ConsoleColor.White;Console.SetCursorPosition(width_index, _height - 9);//基础信息Console.WriteLine("Pname" + " " + "state" + " " + "artime" + " " + "ntime" + " " + "rtime");}
画面描绘(可变+核心):
总代码:
static void ChangedeDraw(int _width, int _height, List LInkList, List InRunningList)
{int NowIndexScence = 1;//第一次回合int N = 2;//时间片段bool isStartRotate = false;int InRunningListInsert=0;int POPindex = -1;string POPlinkname;#region 第一回合for (int index = 0; index < LInkList.Count; index++){//到达时间到达0,未插入处理if (LInkList[index].arrivetime == 0&& LInkList[index].isInserted==false){LInkList[index].isInserted =true;/****************将未进的新程序放入到 InRunningList**********************/LinkNode tempNode1 = new LinkNode(LInkList[index].linkname,LInkList[index].ntime,LInkList[index].arrivetime);tempNode1.state = E_nodeState.runn;InRunningList.Add(tempNode1);InRunningListInsert++;}//每个回合减一LInkList[index].arrivetime--;if (LInkList[index].arrivetime < 0) LInkList[index].arrivetime = 0;}OUTPutLInkList(_width, _height, InRunningList, LInkList,NowIndexScence,POPindex, POPlinkname="");//定位,正在跑的进程,回合制,弹出判断#endregionwhile (true){//回合进行时Console.ReadKey(true);//检测输入NowIndexScence++;//下一回合//**************************对于InRunningList处理(时间片N,限制边界)for (int index = 0; index < InRunningList.Count; index++)//对于InRunningList处理{//每回合读取一个时间片Nif ((InRunningList[index].state == E_nodeState.runn) && (InRunningList[index].isCompleted == false)){InRunningList[index].rtime += N;}//限制边界if (InRunningList[index].rtime >= InRunningList[index].ntime){InRunningList[index].rtime = InRunningList[index].ntime;InRunningList[index].isCompleted = true;InRunningList[index].state = E_nodeState.finish;POPindex = index;POPlinkname = InRunningList[index].linkname; } }
/*******************第一次轮转*********************************/if (InRunningListInsert == LInkList.Count) isStartRotate = true;//**************第一次轮转,uncompleted全部插入if (isStartRotate) {LinkNode tempNode1 = new LinkNode(InRunningList[InRunningList.Count-1].linkname, InRunningList[InRunningList.Count-1].ntime, InRunningList[InRunningList.Count-1].arrivetime);tempNode1.state = E_nodeState.runn;tempNode1.GetRunTime(InRunningList[InRunningList.Count - 1].rtime);InRunningList.Add(tempNode1);//扩容for (int index = InRunningList.Count-2; index>0; index--)//将第一个放置于最后,依次向上{InRunningList[index] = InRunningList[index-1];InRunningList[index].state = E_nodeState.wait;}InRunningList[0] = tempNode1;InRunningList.RemoveAt(InRunningList.Count-1);}//******************************对于LInkList处理(未进的新程序)for (int index = 0; index < LInkList.Count; index++)//对于LInkList处理{//到达时间到达0,未插入处理if (LInkList[index].arrivetime == 0 && LInkList[index].isInserted == false){LInkList[index].isInserted = true;/***********将未进的新程序放入到 InRunningList************/LinkNode tempNode1 = new LinkNode(LInkList[index].linkname, LInkList[index].ntime, LInkList[index].arrivetime);tempNode1.state = E_nodeState.runn;InRunningList.Add(tempNode1);if (InRunningList.Count <= LInkList.Count)//判断有无剩余的完全插入??{for (int i = InRunningList.Count - 1; i > 0; i--){InRunningList[i] = InRunningList[i - 1];InRunningList[i].state = E_nodeState.wait;}InRunningList[0] = tempNode1;}InRunningListInsert++;}//每个回合减一LInkList[index].arrivetime--;if (LInkList[index].arrivetime < 0) LInkList[index].arrivetime = 0;}/**********************清除IsComplete***********************/for (int index = 0; index < InRunningList.Count; index++)//对于InRunningList处理,删除{if (InRunningList[index].isCompleted == true){InRunningList.RemoveAt(index);}}OUTPutLInkList(_width, _height, InRunningList, LInkList,NowIndexScence, POPindex, POPlinkname);//定位,正在跑的进程,回合制,弹出判断POPindex = -1;}//Console.ReadKey(true);//检测输入}
附属代码:
private static void OUTPutLInkList(int _width, int _height,List InRunningList, List LInkList, int NowIndex,int POPindex, string POPlinkname)
{#region 依次画出InRunningList的数值Console.ForegroundColor = ConsoleColor.White;int width_index = (_width - 4) / LInkList.Count - LInkList.Count / 2;for (int j = 0; j < InRunningList.Count; j++){Console.SetCursorPosition(width_index, _height - 8 + j);InRunningList[j].DisPlaying();Console.SetCursorPosition(width_index, _height - 8 + j + 1);Console.Write(" ");}Console.SetCursorPosition(width_index, _height - 8 + LInkList.Count + 1);Console.Write(" ");Console.SetCursorPosition(width_index, _height - 8 + LInkList.Count + 1);Console.WriteLine("PROCESSING:" + NowIndex);if (POPindex != -1){ Console.SetCursorPosition(_width/2, _height - 8 + LInkList.Count + 1);Console.WriteLine("FINISHED P:" + POPlinkname);}#endregion//***********************************?#region 可变方块测绘int width_index1 = (_width - 4) / LInkList.Count - LInkList.Count / 2;ClearT(_width, _height);for (int i = 0; i < InRunningList.Count; i++){InRunningList[i].DrawT(width_index1, _height);width_index1 += (_width - 4) / LInkList.Count;}#endregion}
主函数代码:
static void Main(string[] args){// Console.WriteLine("Hello World!");/* LinkNode node1 = new LinkNode("A", 4, 0);LinkNode node2 = new LinkNode("B", 3, 1);LinkNode node3 = new LinkNode("C", 5, 2);LinkNode node4 = new LinkNode("D", 2, 3);LinkNode node5 = new LinkNode("E", 4, 4);*//*********************初始化********************************/
int _width = 50; int _height = 30;
ConsoleInit(_width, _height);
List LInkList =
new List { { new LinkNode("A", 4, 0) }, { new LinkNode("B", 3, 1) },{ new LinkNode("C", 5, 2) }, new LinkNode("D", 2, 3),{ new LinkNode("E", 4, 4) } };
List InRunningList = new List();//进程进行列表while (true)
{
// LInkList[1].DisPlaying();UnChangedeDraw(_width, _height, LInkList, InRunningList);
ChangedeDraw(_width, _height, LInkList, InRunningList);}
}
实验总代码(全):
using System;
using System.Collections.Generic;namespace JianDanLunZhuan
{enum E_nodeState{wait,runn,finish,}class LinkNode{public string linkname;public E_nodeState state; //状态//public int super; //优先数public int ntime; //总需要运行时间public int rtime; //已运行时间//private int rtimeMax;//public int wtime;//已等待时间public int arrivetime;//到达时间//public int isAllRepeated;//记录轮转时间// public bool isAllRepeatedFlow;//记录轮转超标public bool isInserted;//记录有无进入进程public bool isCompleted;//记录有无运行完成public LinkNode(string linkname,int ntime){this.linkname = linkname;this.state = E_nodeState.wait;//this.super = super;this.ntime = ntime;this.rtime = 0;//this.wtime = wtime;// this.isAllRepeated = 0;//this.isAllRepeatedFlow = false;this.isInserted = false;this.isCompleted = false;}public LinkNode(string linkname, int ntime, int arrivetime) : this(linkname,ntime){this.arrivetime = arrivetime;}public void GetRunTime(int value){this.rtime = value;}public void DisPlaying(){Console.WriteLine(linkname + " " + state + " " + arrivetime+" "+ntime + " "+rtime);}private void ChooseColor(E_nodeState state){switch (state){case E_nodeState.wait:Console.ForegroundColor = ConsoleColor.Blue;break;case E_nodeState.runn:Console.ForegroundColor = ConsoleColor.Yellow;break;case E_nodeState.finish:Console.ForegroundColor = ConsoleColor.Magenta;break;}}public void DrawT(int width_index1,int _height){Console.ForegroundColor = ConsoleColor.White;Console.SetCursorPosition(width_index1, _height - 12);Console.Write(this.linkname);for(int i = 0; i < this.rtime; i++){Console.SetCursorPosition(width_index1, _height - 13-i);//Console.Write(" ");ChooseColor(this.state);Console.Write("■");}}}class Program{static void Main(string[] args){// Console.WriteLine("Hello World!");/* LinkNode node1 = new LinkNode("A", 4, 0);LinkNode node2 = new LinkNode("B", 3, 1);LinkNode node3 = new LinkNode("C", 5, 2);LinkNode node4 = new LinkNode("D", 2, 3);LinkNode node5 = new LinkNode("E", 4, 4);*//*********************初始化********************************/int _width = 50; int _height = 30;ConsoleInit(_width, _height);List LInkList =new List { { new LinkNode("A", 4, 0) }, { new LinkNode("B", 3, 1) },{ new LinkNode("C", 5, 2) }, new LinkNode("D", 2, 3),{ new LinkNode("E", 4, 4) } };List InRunningList = new List();//进程进行列表while (true){// LInkList[1].DisPlaying();UnChangedeDraw(_width, _height, LInkList, InRunningList);ChangedeDraw(_width, _height, LInkList, InRunningList);}}static void ConsoleInit(int _width, int _height)//基础设置{//光标隐藏Console.CursorVisible = false;//舞台大小Console.SetWindowSize(_width, _height);Console.SetBufferSize(_width, _height);}static void ClearT(int _width, int _height){for (int i = 1; i < _height - 11; i++){Console.SetCursorPosition(2, i);Console.Write(" ");}}static void UnChangedeDraw(int _width, int _height, List LInkList, List InRunningList){Console.ForegroundColor = ConsoleColor.Red;for (int i = 0; i < _width; i += 2){Console.SetCursorPosition(i, 0);Console.Write("■");Console.SetCursorPosition(i, _height - 10);Console.Write("■");Console.SetCursorPosition(i, _height - 1);Console.Write("■");}for (int i = 0; i < _height; i += 1){Console.SetCursorPosition(0, i);Console.Write("■");Console.SetCursorPosition(_width - 2, i);Console.Write("■");}int width_index = (_width - 4) / LInkList.Count - LInkList.Count / 2;Console.ForegroundColor = ConsoleColor.Yellow;Console.SetCursorPosition(width_index, _height - 11);Console.Write("■"+"RUNNING" + " ");Console.ForegroundColor = ConsoleColor.Blue;Console.Write("■" + "WAITING" + " ");//Console.ForegroundColor = ConsoleColor.Magenta;//Console.Write("■" + "FINISHED" );Console.ForegroundColor = ConsoleColor.White;Console.SetCursorPosition(width_index, _height - 9);//基础信息Console.WriteLine("Pname" + " " + "state" + " " + "artime" + " " + "ntime" + " " + "rtime");}static void ChangedeDraw(int _width, int _height, List LInkList, List InRunningList){//bool isOver = false;//int SumIndex = 0;int NowIndexScence = 1;//第一次回合int N = 2;//时间片段bool isStartRotate = false;int InRunningListInsert=0;int POPindex = -1;string POPlinkname;#region 第一回合for (int index = 0; index < LInkList.Count; index++){//到达时间到达0,未插入处理if (LInkList[index].arrivetime == 0&& LInkList[index].isInserted==false){LInkList[index].isInserted =true;/****************将未进的新程序放入到 InRunningList**********************/LinkNode tempNode1 = new LinkNode(LInkList[index].linkname,LInkList[index].ntime,LInkList[index].arrivetime);tempNode1.state = E_nodeState.runn;InRunningList.Add(tempNode1);InRunningListInsert++;}//每个回合减一LInkList[index].arrivetime--;if (LInkList[index].arrivetime < 0) LInkList[index].arrivetime = 0;}OUTPutLInkList(_width, _height, InRunningList, LInkList,NowIndexScence,POPindex, POPlinkname="");//定位,正在跑的进程,回合制,弹出判断#endregionwhile (true){//回合进行时Console.ReadKey(true);//检测输入NowIndexScence++;//下一回合//**************************对于InRunningList处理(时间片N,限制边界)for (int index = 0; index < InRunningList.Count; index++)//对于InRunningList处理{//每回合读取一个时间片Nif ((InRunningList[index].state == E_nodeState.runn) && (InRunningList[index].isCompleted == false)){InRunningList[index].rtime += N;}//限制边界if (InRunningList[index].rtime >= InRunningList[index].ntime){InRunningList[index].rtime = InRunningList[index].ntime;InRunningList[index].isCompleted = true;InRunningList[index].state = E_nodeState.finish;POPindex = index;POPlinkname = InRunningList[index].linkname;}}/******************************第一次轮转*****************************************************/if (InRunningListInsert == LInkList.Count) isStartRotate = true;//**************第一次轮转,uncompleted全部插入// bool isAllRotate = true;//??if (isStartRotate) {LinkNode tempNode1 = new LinkNode(InRunningList[InRunningList.Count-1].linkname, InRunningList[InRunningList.Count-1].ntime, InRunningList[InRunningList.Count-1].arrivetime);tempNode1.state = E_nodeState.runn;tempNode1.GetRunTime(InRunningList[InRunningList.Count - 1].rtime);InRunningList.Add(tempNode1);//扩容for (int index = InRunningList.Count-2; index>0; index--)//将第一个放置于最后,依次向上{InRunningList[index] = InRunningList[index-1];InRunningList[index].state = E_nodeState.wait;}InRunningList[0] = tempNode1;InRunningList.RemoveAt(InRunningList.Count-1);}//******************************对于LInkList处理(未进的新程序)for (int index = 0; index < LInkList.Count; index++)//对于LInkList处理{//到达时间到达0,未插入处理if (LInkList[index].arrivetime == 0 && LInkList[index].isInserted == false){LInkList[index].isInserted = true;/****************将未进的新程序放入到 InRunningList**********************/LinkNode tempNode1 = new LinkNode(LInkList[index].linkname, LInkList[index].ntime, LInkList[index].arrivetime);tempNode1.state = E_nodeState.runn;InRunningList.Add(tempNode1);if (InRunningList.Count <= LInkList.Count)//判断有无剩余的完全插入??{for (int i = InRunningList.Count - 1; i > 0; i--){InRunningList[i] = InRunningList[i - 1];InRunningList[i].state = E_nodeState.wait;}InRunningList[0] = tempNode1;}InRunningListInsert++;}//每个回合减一LInkList[index].arrivetime--;if (LInkList[index].arrivetime < 0) LInkList[index].arrivetime = 0;}/**********************清除IsComplete***********************/for (int index = 0; index < InRunningList.Count; index++)//对于InRunningList处理,删除{if (InRunningList[index].isCompleted == true){InRunningList.RemoveAt(index);}}OUTPutLInkList(_width, _height, InRunningList, LInkList,NowIndexScence, POPindex, POPlinkname);//定位,正在跑的进程,回合制,弹出判断POPindex = -1;}//Console.ReadKey(true);//检测输入}private static void OUTPutLInkList(int _width, int _height,List InRunningList, List LInkList, int NowIndex,int POPindex, string POPlinkname){#region 依次画出InRunningList的数值Console.ForegroundColor = ConsoleColor.White;int width_index = (_width - 4) / LInkList.Count - LInkList.Count / 2;for (int j = 0; j < InRunningList.Count; j++){Console.SetCursorPosition(width_index, _height - 8 + j);InRunningList[j].DisPlaying();Console.SetCursorPosition(width_index, _height - 8 + j + 1);Console.Write(" ");}Console.SetCursorPosition(width_index, _height - 8 + LInkList.Count + 1);Console.Write(" ");Console.SetCursorPosition(width_index, _height - 8 + LInkList.Count + 1);Console.WriteLine("PROCESSING:" + NowIndex);if (POPindex != -1){ Console.SetCursorPosition(_width/2, _height - 8 + LInkList.Count + 1);Console.WriteLine("FINISHED P:" + POPlinkname);}#endregion//***********************************?#region 可变方块测绘int width_index1 = (_width - 4) / LInkList.Count - LInkList.Count / 2;ClearT(_width, _height);for (int i = 0; i < InRunningList.Count; i++){InRunningList[i].DrawT(width_index1, _height);width_index1 += (_width - 4) / LInkList.Count;}#endregion}}
}
实验效果:











本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
