📄 form1.cs
字号:
//
this.radioFCFS.Location = new System.Drawing.Point(8, 16);
this.radioFCFS.Name = "radioFCFS";
this.radioFCFS.Size = new System.Drawing.Size(128, 24);
this.radioFCFS.TabIndex = 8;
this.radioFCFS.Text = "先来先服务(FCFS)";
this.radioFCFS.CheckedChanged += new System.EventHandler(this.radioFCFS_CheckedChanged);
//
// radioSJF
//
this.radioSJF.Location = new System.Drawing.Point(192, 16);
this.radioSJF.Name = "radioSJF";
this.radioSJF.Size = new System.Drawing.Size(128, 24);
this.radioSJF.TabIndex = 9;
this.radioSJF.Text = "最短作业优先(SJF)";
this.radioSJF.CheckedChanged += new System.EventHandler(this.radioSJF_CheckedChanged);
//
// radioHRN
//
this.radioHRN.Location = new System.Drawing.Point(352, 16);
this.radioHRN.Name = "radioHRN";
this.radioHRN.Size = new System.Drawing.Size(144, 24);
this.radioHRN.TabIndex = 10;
this.radioHRN.Text = "响应比高者优先(HRN)";
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.radioFCFS,
this.radioHRN,
this.radioSJF});
this.groupBox1.Location = new System.Drawing.Point(32, 112);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(504, 48);
this.groupBox1.TabIndex = 11;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "作业调度算法";
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("华文中宋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(32, 640);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(121, 22);
this.label2.TabIndex = 12;
this.label2.Text = "平均周转时间:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("华文中宋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(288, 640);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(154, 22);
this.label3.TabIndex = 13;
this.label3.Text = "平均带权周转时间:";
//
// button5
//
this.button5.Image = ((System.Drawing.Bitmap)(resources.GetObject("button5.Image")));
this.button5.Location = new System.Drawing.Point(123, 168);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(18, 32);
this.button5.TabIndex = 14;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "引用上次的队列";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 679);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button5,
this.label3,
this.label2,
this.groupBox1,
this.label1,
this.FinishGrid,
this.WaitGrid,
this.RunGrid,
this.button4,
this.button3,
this.button2,
this.button1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "作业调度实验";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.RunGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.WaitGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.FinishGrid)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void Form1_Load(object sender, System.EventArgs e)
{
Clean();
}
private void button1_Click(object sender, System.EventArgs e)
{
button1.Enabled=false;
inputform.ShowDialog(this);
}
private void button3_Click(object sender, System.EventArgs e)
{
Clean();
}
private void Clean()
{
RunTable.Clear();
WaitTable.Clear();
FinishTable.Clear();
RunGrid.DataSource = RunTable;
WaitGrid.DataSource = WaitTable;
FinishGrid.DataSource = FinishTable;
A.NowTime=0;
inputform.num=1;
radioFCFS.Checked=true;
this.button1.Enabled=this.groupBox1.Enabled=true;
this.button2.Enabled=false;
this.button2.Text="运行";
}
private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void NowTime_Changed(object source, EventArgs e)
{
label1.Text="当前时刻:"+A.NowTime.ToString();
inputform.label5.Text="当前时刻:"+A.NowTime.ToString();
}
public void Run()
{
button1.Enabled=false;
button2.Enabled=true;
WaitQueue=inputform.queue;
BackupQueue.Clear();
for(int i=0;i<=WaitQueue.Count;i++) /*备份就绪队列已给下一次用*/
{
JCB temp =(JCB)WaitQueue.Dequeue();
BackupQueue.Enqueue(temp);
WaitQueue.Enqueue(temp);
}
if(radioFCFS.Checked)
this.FCFSsort(WaitQueue);
else if(radioSJF.Checked)
this.SJFsort(WaitQueue);
DisplayWaitTable();
}
public void DisplayRunTable()
{
RunTable.Clear();
JCB CheckJCB =new JCB();
if(WaitQueue.Count!=0)
{
CheckJCB=(JCB)WaitQueue.Peek();
if(RunJCB.name!=CheckJCB.name)
CheckJCB.StartTime=A.NowTime;
RunJCB=(JCB)WaitQueue.Dequeue();
RunJCB.AlreadyTime++;
RunJCB.NeedTime--;
RunJCB.StartTime=CheckJCB.StartTime;
RunTableRow = RunTable.NewRow();
RunTableRow["id"] =ID ;
RunTableRow["作业名"] = RunJCB.name ;
RunTableRow["开始运行时刻"] = RunJCB.StartTime;
RunTableRow["已运行的时间"]=RunJCB.AlreadyTime;
RunTableRow["还需运行的时间"]=RunJCB.NeedTime;
RunTableRow["所需资源"]=RunJCB.NeedSource;
RunTableRow["状态"]='R';
RunTable.Rows.Add(RunTableRow);
// inputform.queue.Clear();
// Set to DataGrid.DataSource property to the table.
RunGrid.DataSource = RunTable;
if(RunJCB.NeedTime==0)
{
RunJCB.FinishTime=RunJCB.StartTime+RunJCB.RunTime;
RunJCB.PeriodTime=RunJCB.FinishTime-RunJCB.ReferTime;
RunJCB.ValuePeriod=(float)((float)RunJCB.PeriodTime/(float)RunJCB.RunTime);
FinishQueue.Enqueue(RunJCB);
SumPeriodTime+=RunJCB.PeriodTime;
SumValuePeriod+=RunJCB.ValuePeriod;
}
}
}
public void DisplayWaitTable()
{
JCB tempJCB = new JCB();
WaitTable.Clear();
for(ID=1;ID<=WaitQueue.Count;ID++)
{
tempJCB=(JCB)WaitQueue.Dequeue();
WaitTableRow= WaitTable.NewRow();
WaitTableRow["id"] =ID;
WaitTableRow["作业名"] = tempJCB.name ;
WaitTableRow["提交时间"] = tempJCB.ReferTime ;
WaitTableRow["需要的运行时间"]=tempJCB.NeedTime;
WaitTableRow["所需资源"]=tempJCB.NeedSource;
WaitTableRow["状态"]=tempJCB.state;
WaitTable.Rows.Add(WaitTableRow);
WaitQueue.Enqueue(tempJCB);
}
WaitGrid.DataSource = WaitTable;
}
private void button2_Click(object sender, System.EventArgs e)
{
groupBox1.Enabled=false;
button1.Enabled=false;
if(timer1.Enabled==false)
{
button2.Text="暂停";
timer1.Enabled=true;
button3.Enabled=false;
}
else
{
button2.Text="继续";
timer1.Enabled=false;
button3.Enabled=true;
}
}
public void DisplayFinishTable()
{
JCB FinishJCB =new JCB();
FinishTable.Clear();
for(int i=1;i<=this.FinishQueue.Count;i++)
{
FinishJCB=(JCB)FinishQueue.Dequeue();
FinishTableRow = FinishTable.NewRow();
FinishTableRow["id"] =i ;
FinishTableRow["作业名"] = FinishJCB.name ;
FinishTableRow["开始运行时刻"] = FinishJCB.StartTime;
FinishTableRow["运行时间"] = FinishJCB.RunTime;
FinishTableRow["完成时刻"] = FinishJCB.FinishTime;
FinishTableRow["周转时间"] = FinishJCB.PeriodTime;
FinishTableRow["带权周转时间"] = FinishJCB.ValuePeriod;
FinishTableRow["所需资源"] = FinishJCB.NeedSource;
FinishTableRow["状态"] = 'F';
FinishTable.Rows.Add(FinishTableRow);
this.FinishQueue.Enqueue(FinishJCB);
}
FinishGrid.DataSource = FinishTable;
}
public void FCFSsort(Queue queue) //队列排序
{
if(queue.Count!=0)
{
Array array=Array.CreateInstance(typeof(JCB),queue.Count);
queue.CopyTo(array,0);
queue.Clear();
JCB ready,empty=new JCB();
empty.ReferTime=100;
for(int j=0;j<=array.Length-1;j++)
{
int max=100,now=0;
for(int i=0;i<=array.Length-1;i++)
{
ready=(JCB)array.GetValue(i);
if(max>ready.ReferTime)
{
max=ready.ReferTime;
now=i;
}
}
queue.Enqueue((JCB)array.GetValue(now));
array.SetValue(empty,now);
}
}
}
public void SJFsort(Queue queue)
{
if(queue.Count!=0)
{
Array array=Array.CreateInstance(typeof(JCB),queue.Count);
queue.CopyTo(array,0);
queue.Clear();
JCB ready,empty=new JCB();
empty.RunTime=100;
for(int j=0;j<=array.Length-1;j++)
{
int max=100,now=0;
for(int i=0;i<=array.Length-1;i++)
{
ready=(JCB)array.GetValue(i);
if(max>ready.RunTime)
{
max=ready.RunTime;
now=i;
}
}
queue.Enqueue((JCB)array.GetValue(now));
array.SetValue(empty,now);
}
}
}
private void radioFCFS_CheckedChanged(object sender, System.EventArgs e)
{
if(radioFCFS.Checked && WaitQueue!=null)
{
this.FCFSsort(WaitQueue);
this.DisplayWaitTable();
}
}
private void radioSJF_CheckedChanged(object sender, System.EventArgs e)
{
if(radioSJF.Checked && WaitQueue!=null)
{
this.SJFsort(WaitQueue);
this.DisplayWaitTable();
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
A.NowTime++;
WaitQueue=inputform.queue;
DisplayRunTable();
DisplayWaitTable();
if(RunJCB.NeedTime!=0)
{
WaitQueue.Enqueue(RunJCB);
}
if(radioFCFS.Checked==true)
{
FCFSsort(WaitQueue);
}
DisplayFinishTable();
if(WaitQueue.Count==0)
{
this.timer1.Enabled=false;
DisplayRunTable();
label2.Text="平均周转时间"+Convert.ToString(this.SumPeriodTime/A.NowTime);
label3.Text="平均带权周转时间"+Convert.ToString(this.SumValuePeriod/A.NowTime);
MessageBox.Show("作业调度已完成","Finished",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
button1.Enabled=true;
button2.Enabled=false;
button2.Text="运行";
button3.Enabled=true;
}
}
private void button5_Click(object sender, System.EventArgs e)
{
Point point = new Point(0,button5.Height);
this.contextMenu1.Show(button5,point);
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
if(BackupQueue.Count!=0)
{
for(int i=0;i<=BackupQueue.Count;i++)
{
JCB temp =(JCB)BackupQueue.Dequeue();
BackupQueue.Enqueue(temp);
WaitQueue.Enqueue(temp);
}
DisplayWaitTable();
button2.Enabled=true;
}
}
private void label1_Click(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -