bool CheckNewDemand(string name, Resourse demand2, Resourse Available2, List <Process> processList2) { List <Process> processList = new List <Process>(); processList2.ForEach(i => processList.Add(i.Clone())); //printPro(processList,available); Resourse Available = Available2.Clone(); Resourse demand = demand2.Clone(); //cout << "进入检查状态" << endl; for (int i = 0; i < processList.Count; i++) { if (processList[i].name != name) { continue; } if (!processList[i].Demand(ref Available, demand)) //Demand 有问题 { Console.WriteLine("错误出口1"); return(false); } else { //printPro(processList, Available); List <bool> map = new List <bool>(new bool[] { false, false, false, false, false }); //map[i] = true; //cout << "进入复查状态" << endl; return(CheckStatus(Available, processList, ref map)); } } Console.WriteLine("错误出口2"); return(false); }
public bool addRequestT(int A, int B, int C, string name) { Resourse demand = new Resourse(A, B, C); Task <bool> tMain = new Task <bool>(() => CheckNewDemandT(name, demand, available, ProcessList)); tMain.Start(); Task.WaitAll(tMain); if (tMain.Result) { //this.listBox.Items.Add("预分配成功!"); StrInfoTransfEvent("预分配成功!"); for (int i = 0; i < ProcessList.Count; i++) { Process each = ProcessList[i]; if (each.name == name) { Functions.AddTo(ref each.Allocation, demand); Functions.MinTo(ref each.Need, demand); Functions.MinTo(ref available, demand); } } printPro(ProcessList, available); return(true); } else { printPro(ProcessList, available); StrInfoTransfEvent("预分配失败!"); return(false); } }
public FormOperating(AlgorithmOperator b, Resourse d, string n)//初始化各控件和属性 { InitializeComponent(); this.demand = d; this.name = n; CheckForIllegalCrossThreadCalls = false; this.myOperator = b; foreach (Control control in this.Controls) { if (control is PictureBox) { pictureBoxes.Add((PictureBox)control); } } Console.WriteLine(pictureBoxes.Count); foreach (Control control in this.groupBox1.Controls) { if (control is Label) { //listBox1.Items.Add(control.Name); avaiLables.Add((Label)control); } } this.label_avaiA.Text = myOperator.available.A.ToString(); this.label_avaiB.Text = myOperator.available.B.ToString(); this.label_avaiC.Text = myOperator.available.C.ToString(); init_draw(); //listBox1.CheckForIllegalCrossThreadCalls = false; }
/* * -----------------------------------------------------------------------上方是异步绘制的区域---------------------------------------------------------------------------------------------------------------------------------------; */ public bool addRequest(int A, int B, int C, string name) { Resourse demand = new Resourse(A, B, C); if (CheckNewDemand(name, demand, available, ProcessList)) { Console.WriteLine("分配成功!"); for (int i = 0; i < ProcessList.Count; i++) { Process each = ProcessList[i]; if (each.name == name) { Functions.AddTo(ref each.Allocation, demand); Functions.MinTo(ref each.Need, demand); Functions.MinTo(ref available, demand); } } printPro(ProcessList, available); return(true); } else { printPro(ProcessList, available); Console.WriteLine("分配失败!"); return(false); } }
public static bool AsB(Resourse a, Resourse b) { if (a.A <= b.A && a.B <= b.B && a.C <= b.C) { return(true); } return(false); }
public Process(string name, int a, int b, int c) { this.name = name; Max.A = a; Max.B = b; Max.C = c; Need = Max.Clone(); }
public AlgorithmOperator(Dictionary <string, int> dic) { available = new Resourse(dic["SA"], dic["SB"], dic["SC"]); for (int i = 0; i < 5; i++) { Process process = new Process("P" + i, dic[i + "A"], dic[i + "B"], dic[i + "C"]); ProcessList.Add(process); } }
public void printPro(List <Process> processList, Resourse Available) { foreach (var each in processList) { Console.WriteLine("name:" + each.name); Console.Write("MAX:"); printRes(each.Max); Console.Write(" Allocation:"); printRes(each.Allocation); Console.Write("Need:"); printRes(each.Need); Console.WriteLine(); } Console.WriteLine("Available:" + Available.A + "," + Available.B + "," + Available.C); }
public void drawSingleBox(int index, Resourse demand, Resourse historyHave, Resourse Max, Resourse available, int flag)//按照运行给出的数据动态展示算法运行到的当前进程的变化情况 { // float angleA = Allocation.A * 360 / (Max.A == 0 ? 1 : Max.A); //listBox1.Items.Add("myDrawPie被调用"); Console.WriteLine(pictureBoxes[index].AccessibilityObject); Graphics g = this.pictureBoxes[index].CreateGraphics(); Rectangle rectangle1 = new Rectangle(10, 10, 100, 100); Rectangle rectangle2 = new Rectangle(150, 10, 100, 100); Rectangle rectangle3 = new Rectangle(290, 10, 100, 100); float angleA = historyHave.A * 360 / (Max.A == 0 ? 1 : Max.A); float angleB = historyHave.B * 360 / (Max.B == 0 ? 1 : Max.B); float angleC = historyHave.C * 360 / (Max.C == 0 ? 1 : Max.C); Resourse after_add = new Resourse(historyHave.A + demand.A > Max.A ? Max.A : historyHave.A + demand.A, historyHave.B + demand.B > Max.B ? Max.B : historyHave.B + demand.B, historyHave.C + demand.C > Max.C ? Max.C : historyHave.C + demand.C); g.FillRectangle(new SolidBrush(Color.White), rectangle1.X, 115, 100, 20); g.DrawString(after_add.A.ToString() + "/" + Max.A.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle1.X + 30, 115); g.FillRectangle(new SolidBrush(Color.White), rectangle2.X, 115, 100, 20); g.DrawString(after_add.B.ToString() + "/" + Max.B.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle2.X + 30, 115); g.FillRectangle(new SolidBrush(Color.White), rectangle3.X, 115, 100, 20); g.DrawString(after_add.C.ToString() + "/" + Max.C.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle3.X + 30, 115); MyDrawParam param1 = new MyDrawParam(g); param1.positionRectangle = new Rectangle(10, 10, 100, 100); param1.angleBegin = angleA - 90; param1.angleIncrease = demand.A * 360 / (Max.A == 0 ? 1 : Max.A); param1.interval = DataBus.FormOperatingActiveInterval; MyDrawParam param2 = new MyDrawParam(g); param2.positionRectangle = new Rectangle(150, 10, 100, 100); param2.angleBegin = angleB - 90; param2.angleIncrease = demand.B * 360 / (Max.B == 0 ? 1 : Max.B); param2.interval = DataBus.FormOperatingActiveInterval; MyDrawParam param3 = new MyDrawParam(g); param3.positionRectangle = new Rectangle(290, 10, 100, 100); param3.angleBegin = angleC - 90; param3.angleIncrease = demand.C * 360 / (Max.C == 0 ? 1 : Max.C); param3.interval = DataBus.FormOperatingActiveInterval; if (flag == 1) { MyAnimation.SyncPreDrawPie(param1); MyAnimation.SyncPreDrawPie(param2); MyAnimation.SyncPreDrawPie(param3); } else { MyAnimation.SyncActiveDrawPie(param1); MyAnimation.SyncActiveDrawPie(param2); MyAnimation.SyncActiveDrawPie(param3); } }
public void refresh()//立刻更新所有的显示值和绘制情况 { historyReset(); SolidBrush fix_brush = new SolidBrush(DataBus.fixColor); SolidBrush change_brush = new SolidBrush(DataBus.mainColor); SolidBrush warn_brush = new SolidBrush(DataBus.backupColor); Rectangle rectangle1 = new Rectangle(10, 10, 100, 100); Rectangle rectangle2 = new Rectangle(150, 10, 100, 100); Rectangle rectangle3 = new Rectangle(290, 10, 100, 100); for (int i = 0; i < 5; i++) { Resourse Max = myOperator.ProcessList[i].Max; Resourse Allocation = myOperator.ProcessList[i].Allocation; float angleA = Allocation.A * 360 / (Max.A == 0 ? 1 : Max.A); float angleB = Allocation.B * 360 / (Max.B == 0 ? 1 : Max.B); float angleC = Allocation.C * 360 / (Max.C == 0 ? 1 : Max.C); //listBox1.Items.Add(angleA + "," + angleB + "," + angleC); //Graphics g = pictureBoxes[4 - i].CreateGraphics(); Bitmap img = new Bitmap(pictureBoxes[i].Width, pictureBoxes[i].Height); pictureBoxes[i].Image = img; Graphics g = Graphics.FromImage(img); g.Clear(Color.White); g.FillPie(fix_brush, rectangle1, 0, 360); g.FillPie(change_brush, rectangle1, -90, angleA); if (Max.A == 0) { g.FillPie(change_brush, rectangle1, 0, 360); } g.DrawString(Allocation.A.ToString() + "/" + Max.A.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle1.X + 30, 115); g.FillPie(fix_brush, rectangle2, 0, 360); g.FillPie(change_brush, rectangle2, -90, angleB); if (Max.B == 0) { g.FillPie(change_brush, rectangle2, 0, 360); } g.DrawString(Allocation.B.ToString() + "/" + Max.B.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle2.X + 30, 115); // -90+history.A, angleA - history.A g.FillPie(fix_brush, rectangle3, 0, 360); g.FillPie(change_brush, rectangle3, -90, angleC); if (Max.C == 0) { g.FillPie(change_brush, rectangle3, 0, 360); } g.DrawString(Allocation.C.ToString() + "/" + Max.C.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle3.X + 30, 115); } barReset(); }
private void update_draw(object sender, EventArgs e)//动态的绘制更新 { myOperator.printPro(myOperator.ProcessList, myOperator.available); Rectangle rectangle1 = new Rectangle(10, 10, 100, 100); Rectangle rectangle2 = new Rectangle(150, 10, 100, 100); Rectangle rectangle3 = new Rectangle(290, 10, 100, 100); for (int i = 0; i < 5; i++) { AngleCollection history = historys[i]; Resourse Max=myOperator.ProcessList[i].Max; Resourse Allocation = myOperator.ProcessList[i].Allocation; float angleA = Allocation.A * 360 / (Max.A == 0 ? 1 : Max.A); float angleB = Allocation.B * 360 / (Max.B == 0 ? 1 : Max.B); float angleC = Allocation.C * 360 / (Max.C == 0 ? 1 : Max.C); Graphics g=pictureBoxes[i].CreateGraphics(); MyDrawParam param1 = new MyDrawParam(g); param1.positionRectangle = rectangle1; param1.angleBegin = -90 + history.A; param1.angleIncrease = angleA - history.A; param1.interval = DataBus.FormMainActiveInterval; MyAnimation.AsyncActiveDrawPie(param1); g.FillRectangle(new SolidBrush(Color.White), rectangle1.X, 115, 100, 20); g.DrawString(Allocation.A.ToString() + "/" + Max.A.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle1.X + 30, 115); MyDrawParam param2 = new MyDrawParam(g); param2.positionRectangle = rectangle2; param2.angleBegin = -90 + history.B; param2.angleIncrease = angleB - history.B; param2.interval = DataBus.FormMainActiveInterval; MyAnimation.AsyncActiveDrawPie(param2); g.FillRectangle(new SolidBrush(Color.White), rectangle2.X, 115, 100, 20); g.DrawString(Allocation.B.ToString() + "/" + Max.B.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle2.X + 30, 115); MyDrawParam param3 = new MyDrawParam(g); param3.positionRectangle = rectangle2; param3.angleBegin = -90 + history.B; param3.angleIncrease = angleB - history.B; param3.interval = DataBus.FormMainActiveInterval; MyAnimation.AsyncActiveDrawPie(param3); //activeDrawPie(g, change_brush, warn_brush, rectangle3, -90+history.C, angleC - history.C,100); g.FillRectangle(new SolidBrush(Color.White), rectangle3.X, 115, 100, 20); g.DrawString(Allocation.C.ToString() + "/" + Max.C.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle3.X + 30, 115); history.A = angleA; history.B = angleB; history.C = angleC; } }
void Withdraw(Process process, List <bool> map, Resourse a, int index)//分配尝试失败时恢复现场 { SolidBrush fix_brush = new SolidBrush(DataBus.fixColor); SolidBrush change_brush = new SolidBrush(DataBus.mainColor); SolidBrush warn_brush = new SolidBrush(DataBus.backupColor); Rectangle rectangle1 = new Rectangle(10, 10, 100, 100); Rectangle rectangle2 = new Rectangle(150, 10, 100, 100); Rectangle rectangle3 = new Rectangle(290, 10, 100, 100); Resourse Max = process.Max; Resourse Allocation = process.Allocation; float angleA = Allocation.A * 360 / (Max.A == 0 ? 1 : Max.A); float angleB = Allocation.B * 360 / (Max.B == 0 ? 1 : Max.B); float angleC = Allocation.C * 360 / (Max.C == 0 ? 1 : Max.C); PictureBox pictureBox = pictureBoxes[index]; //Graphics g = pictureBoxes[4 - i].CreateGraphics(); Bitmap img = new Bitmap(pictureBox.Width, pictureBox.Height); pictureBox.Image = img; Graphics g = Graphics.FromImage(img); g.Clear(Color.White); //Graphics g = pictureBox.CreateGraphics(); g.FillPie(fix_brush, rectangle1, 0, 360); g.FillPie(change_brush, rectangle1, -90, angleA); if (Max.A == 0) { g.FillPie(change_brush, rectangle1, 0, 360); } g.DrawString(Allocation.A.ToString() + "/" + Max.A.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle1.X + 30, 115); g.FillPie(fix_brush, rectangle2, 0, 360); g.FillPie(change_brush, rectangle2, -90, angleB); if (Max.B == 0) { g.FillPie(change_brush, rectangle2, 0, 360); } g.DrawString(Allocation.B.ToString() + "/" + Max.B.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle2.X + 30, 115); g.FillPie(fix_brush, rectangle3, 0, 360); g.FillPie(change_brush, rectangle3, -90, angleC); if (Max.C == 0) { g.FillPie(change_brush, rectangle3, 0, 360); } g.DrawString(Allocation.C.ToString() + "/" + Max.C.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle3.X + 30, 115); setAvailableLabel(a); drawFlag(index, map[index]); }
bool CheckStatus(Resourse Available2, List <Process> processList2, ref List <bool> map) { List <Process> processList = new List <Process>(); processList2.ForEach(i => processList.Add(i.Clone())); Resourse Available = Available2.Clone(); //cout << "复查被调用"<<endl; //Console.WriteLine("map状态:" + map[0] + map[1] + map[2] + map[3] + map[4]); int count = 0; foreach (var each in map) { if (each == false) { break; } count++; } if (count == 5) { return(true); } for (int i = 0; i < processList.Count; i++) { if (map[i] == true) { continue; } if (Functions.AsB(processList[i].Need, Available)) { if (processList[i].Demand(ref Available, processList[i].Need)) { map[i] = true; Functions.AddTo(ref Available, processList[i].Max); //这一步应当还原了available if (CheckStatus(Available, processList, ref map)) { return(true); } } } } Console.WriteLine("错误出口3"); return(false); }
public bool Demand(ref Resourse Available, Resourse demand) { //成功了才变,没成功不变 if (Functions.AsB(demand, Available) && Functions.AsB(demand, this.Need)) { //Console.WriteLine("1时的Max" + this.Max.A+ this.Max.B + this.Max.C ); Functions.MinTo(ref Available, demand); //Console.WriteLine("2时的Max" + this.Max.A + this.Max.B + this.Max.C); Functions.AddTo(ref this.Allocation, demand); //Console.WriteLine("3时的Max" + this.Max.A + this.Max.B + this.Max.C); Functions.MinTo(ref this.Need, demand); //Console.WriteLine("4时的Max" + this.Max.A + this.Max.B + this.Max.C); return(true); } else { return(false); } }
public void init_draw()//绘制的初始化 { SolidBrush fix_brush = new SolidBrush(DataBus.fixColor); SolidBrush change_brush = new SolidBrush(DataBus.mainColor); SolidBrush warn_brush = new SolidBrush(DataBus.backupColor); Rectangle rectangle1 = new Rectangle(10, 10, 100, 100); Rectangle rectangle2 = new Rectangle(150, 10, 100, 100); Rectangle rectangle3 = new Rectangle(290, 10, 100, 100); for (int i = 0; i < 5; i++) { Resourse Max = myOperator.ProcessList[i].Max; Resourse Allocation = myOperator.ProcessList[i].Allocation; //Graphics g = pictureBoxes[4 - i].CreateGraphics(); Bitmap img = new Bitmap(pictureBoxes[i].Width, pictureBoxes[i].Height); pictureBoxes[i].Image = img; Graphics g = Graphics.FromImage(img); g.Clear(Color.White); //Graphics g = pictureBox.CreateGraphics(); g.FillPie(fix_brush, rectangle1, 0, 360); if (Max.A == 0) { g.FillPie(change_brush, rectangle1, 0, 360); } g.DrawString(Allocation.A.ToString() + "/" + Max.A.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle1.X + 30, 115); g.FillPie(fix_brush, rectangle2, 0, 360); if (Max.B == 0) { g.FillPie(change_brush, rectangle2, 0, 360); } g.DrawString(Allocation.B.ToString() + "/" + Max.B.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle2.X + 30, 115); g.FillPie(fix_brush, rectangle3, 0, 360); if (Max.C == 0) { g.FillPie(change_brush, rectangle3, 0, 360); } g.DrawString(Allocation.C.ToString() + "/" + Max.C.ToString(), new Font("等线", 14), new SolidBrush(Color.Black), rectangle3.X + 30, 115); } }
bool CheckNewDemandT(string name, Resourse demand2, Resourse Available2, List <Process> processList2) { List <Process> processList = new List <Process>(); processList2.ForEach(i => processList.Add(i.Clone())); //printPro(processList,available); Resourse Available = Available2.Clone(); Resourse demand = demand2.Clone(); //遍历所有进程 for (int i = 0; i < processList.Count; i++) { if (processList[i].name != name) { continue; } StrInfoTransfEvent("正在预分配中"); DrawInfoTransfEvent(i, demand, processList[i].Allocation, processList[i].Max, this.available, 0); if (!processList[i].Demand(ref Available, demand)) //找到了发出申请的进程 { StrInfoTransfEvent("超出需要"); WarnInfoTransfEvent(i, "数量错误,超出需要"); return(false); } else { //printPro(processList, Available); StrInfoTransfEvent("初步检测通过,开始进行预分配安全性检测"); Thread.Sleep(500); List <bool> map = new List <bool>(new bool[] { false, false, false, false, false }); //map[i] = true; //cout << "进入复查状态" << endl; return(CheckStatusT(Available, processList, ref map)); } } Console.WriteLine("错误出口2"); return(false); }
void setAvailableLabel(Resourse Available)//更新空闲资源的显示标签 { avaiLables[0].Text = Available.A < 0 ? "0" : Available.A.ToString(); avaiLables[1].Text = Available.B < 0 ? "0" : Available.B.ToString(); avaiLables[2].Text = Available.C < 0 ? "0" : Available.C.ToString(); }
public Process(string name, Resourse max) { this.name = name; Max = max; Need = Max.Clone(); }
public AlgorithmOperator() { init(ProcessList); available = new Resourse(12, 5, 9); printPro(ProcessList, available); }
//用于创建深拷贝副本,主要在传参时使用 public Resourse Clone() { Resourse r = new Resourse(this.A, this.B, this.C); return(r); }
void printRes(Resourse r) { Console.Write(r.A + "," + r.B + "," + r.C + ";"); }
bool CheckStatusT(Resourse Available2, List <Process> processList2, ref List <bool> map) { List <Process> processList = new List <Process>(); processList2.ForEach(i => processList.Add(i.Clone())); Resourse Available = Available2.Clone(); //cout << "复查被调用"<<endl; //Console.WriteLine("map状态:" + map[0] + map[1] + map[2] + map[3] + map[4]); int count = 0; for (int i = 0; i < map.Count; i++) { if (map[i] == true) { count++; } } //drawAll(map); if (count == 5) { StrInfoTransfEvent("所有的进程所需资源均被满足"); return(true); } for (int i = 0; i < processList.Count; i++) { if (map[i] == true) { continue; } DrawInfoTransfEvent(i, Available, processList[i].Allocation, processList[i].Max, this.available, 1); //listBox.Items.Add("到达节点1"); if (Functions.AsB(processList[i].Need, Available)) //这个地方好像有点重复? { if (processList[i].Demand(ref Available, processList[i].Need)) { map[i] = true; StrInfoTransfEvent("P" + (i) + "判定成功,释放资源"); //drawFlag(pictureBoxes[i], map[i]); FlagChangeEvent(i, map[i]); Functions.AddTo(ref Available, processList[i].Max); LabelChangeEvent(Available); Thread.Sleep(1000); if (CheckStatusT(Available, processList, ref map)) { return(true); } } else { WarnInfoTransfEvent(i, "此进程暂时无法完全分配"); StrInfoTransfEvent("P" + (i) + "判定失败,继续尝试"); Thread.Sleep(1000); WithDrawTransferEvent(processList[i], map, Available, i); //drawAll(map); //drawFlag(pictureBoxes[i], map[i]); Thread.Sleep(500); } } else { WarnInfoTransfEvent(i, "此进程暂时无法完全分配"); StrInfoTransfEvent("P" + (i) + "判定失败,继续尝试"); Thread.Sleep(1000); WithDrawTransferEvent(processList[i], map, Available, i); //drawAll(map); //drawFlag(pictureBoxes[i], map[i]); Thread.Sleep(500); } } return(false); }
public static void MinTo(ref Resourse a, Resourse b) { a.A -= b.A; a.B -= b.B; a.C -= b.C; }
public static void AddTo(ref Resourse a, Resourse b) { a.A += b.A; a.B += b.B; a.C += b.C; }