⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os.txt

📁 操作系统,作业的调度,自己编的
💻 TXT
字号:
using System;
using System.IO;


namespace zuoye
{
	[Serializable]
	public class Job
	{
		public string Name;
		public DateTime EnterTime;
		public TimeSpan EstimatedRunningTime;
		public DateTime CompleteTime;
		public bool Scheduled = false;
		public Job(string jobName, DateTime enterTime, TimeSpan ert)
		{
			this.Name = jobName;
			this.EnterTime = enterTime;
			this.EstimatedRunningTime = ert;
		}

		public Job() { }

		public override string ToString()
		{
			return EnterTime.ToString();
		}

	}

	/// <summary>
	/// 调度程序的基类, 抽象类
	/// </summary>
	[Serializable]
	public abstract class JobScheduler
	{
		public Job[] Jobs;

		/// <summary>
		/// 对一个作业集合进行调度, 计算其平均周转时间, 平均加权调度时间以及每个作业的完成时刻
		/// </summary>
		/// <param name="jobs"></param>
		public abstract void Schedule();

		/// <summary>
		/// 平均周转时间
		/// </summary>
		public TimeSpan AverageWorkingTime;

		/// <summary>
		/// 平均加权周转时间
		/// </summary>
		public double AverageWeightedWorkingTime;

		/// <summary>
		/// 获得没有调度的作业中最早进入调度程序的作业
		/// </summary>
		/// <param name="jobs"></param>
		/// <returns></returns>
		protected Job GetFirstComeJob()
		{
			Job min = null;
			for (int i = 0; i < this.Jobs.Length; i++)
			{
				if (!this.Jobs[i].Scheduled)
				{
					if (min == null)
					{
						min = this.Jobs[i];
					}
					else if (this.Jobs[i].EnterTime < min.EnterTime)
					{
						min = this.Jobs[i];
					}
				}
			}
			return min;
		}

		protected void CalculateTime()
		{
			TimeSpan workingTime = TimeSpan.Zero;
			double weightedWorkingTime = 0;
			for (int i = 0; i < this.Jobs.Length; i++)
			{
				TimeSpan w = Jobs[i].CompleteTime - Jobs[i].EnterTime;
				workingTime += w;
				weightedWorkingTime += w.TotalMilliseconds / Jobs[i].EstimatedRunningTime.TotalMilliseconds;
			}
			this.AverageWorkingTime = TimeSpan.FromMilliseconds(workingTime.TotalMilliseconds / Jobs.Length);
			this.AverageWeightedWorkingTime = weightedWorkingTime / Jobs.Length;
		}
	}

	/// <summary>
	/// First Coming First Service 调度程序
	/// </summary>
	[Serializable]
	public class FCFSScheduler : JobScheduler
	{
		public FCFSScheduler(Job[] jobs)
		{
			this.Jobs = jobs;
		}

		public override void Schedule()
		{
			DateTime current = GetFirstComeJob().EnterTime;
			Job j = null;
			while ((j = GetFirstComeJob()) != null)
			{
				j.Scheduled = true;
				if (j.EnterTime < current) // current 时刻前已经在等待, 作业立刻开始运行
				{
					current += j.EstimatedRunningTime;
				} 
				else // 作业在 current 时刻后才开始运行
				{
					current = j.EnterTime + j.EstimatedRunningTime;
				}
				j.CompleteTime = current;
			}
			CalculateTime();
		}
	}

	/// <summary>
	/// Shortest Job First 调度程序
	/// </summary>
	[Serializable]
	public class SJFScheduler : JobScheduler
	{
		public SJFScheduler(Job[] jobs)
		{
			this.Jobs = jobs;
		}

		public override void Schedule()
		{
			Job first = GetFirstComeJob();
			DateTime current = first.EnterTime + first.EstimatedRunningTime;
			first.CompleteTime = current;
			first.Scheduled = true;
			Job j = null;
			while ((j = GetShortestEnteredJob(current)) != null)
			{
				j.Scheduled = true;
				if (j.EnterTime < current)
				{
					current += j.EstimatedRunningTime;
				}
				else
				{
					current = j.EnterTime + j.EstimatedRunningTime;
				}
				j.CompleteTime = current;
			}
			CalculateTime();
		}

		/// <summary>
		/// 得到最短运行时间而且在now时刻以及进入调度程序的未被调度的作业
		/// </summary>
		/// <param name="now"></param>
		/// <returns></returns>
		public Job GetShortestEnteredJob(DateTime now)
		{
			Job r = null;
			for (int i = 0; i < Jobs.Length; i++)
			{
				Job j = Jobs[i];
				if (!j.Scheduled && j.EnterTime <= now)
				{
					if (r == null)
					{
						r = j;
					}
					else if (j.EstimatedRunningTime < r.EstimatedRunningTime)
					{
						r = j;
					}
				}
			}
			if (r == null) // now 时刻没有等待中的作业, 则返回下一个最先进入等待队列的作业
			{
				r = GetFirstComeJob();
			}
			return r;
		}
	}

	/// <summary>
	/// 主程序
	/// </summary>
	public class Program
	{
		[STAThread]
		static void Main(string[] args)
		{
			Job[] jobs = new Job[4];
			jobs[0] = new Job("1", GetTime(8, 0), TimeSpan.FromMinutes(120));
			jobs[1] = new Job("2", GetTime(8, 50), TimeSpan.FromMinutes(50));
			jobs[2] = new Job("3", GetTime(9, 0), TimeSpan.FromMinutes(10));
			jobs[3] = new Job("4", GetTime(9, 50), TimeSpan.FromMinutes(20));

			SJFScheduler s = new SJFScheduler(jobs);
			s.Schedule();
			
			for (int i = 0; i < s.Jobs.Length; i++)
			{
				Console.WriteLine("作业"+s.Jobs[i].Name + "进入时间: " + s.Jobs[i].EnterTime);
			}
			Console.WriteLine("\n");
			Console.WriteLine("SJF作业调度:");
			for (int i = 0; i < s.Jobs.Length; i++)
			{
				Console.WriteLine("作业"+s.Jobs[i].Name + "完成时间: " + s.Jobs[i].CompleteTime);
			}
			Console.WriteLine("SJF平均周转时间:"+s.AverageWorkingTime.TotalMinutes);
			Console.WriteLine("SJF带权平均周转时间:"+s.AverageWeightedWorkingTime);

			for (int i = 0; i < jobs.Length; i++)
				jobs[i].Scheduled = false;
			FCFSScheduler f = new FCFSScheduler(jobs);
			f.Schedule();
			Console.WriteLine("\n");
			Console.WriteLine("FCFS作业调度:");
			for (int i = 0; i < f.Jobs.Length; i++)
			{
				Console.WriteLine("作业"+f.Jobs[i].Name + "完成时间: " + f.Jobs[i].CompleteTime);
			}
			Console.WriteLine("FCFS平均周转时间:"+f.AverageWorkingTime.TotalMinutes);
			Console.WriteLine("FCFS带权平均周转时间:"+f.AverageWeightedWorkingTime);
		}
		static DateTime GetTime(int hour, int minute)
		{
			return new DateTime(2007, 5, 16, hour, minute, 0);
		}
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -