Monday, March 26, 2012

Shortest Job First Simulation in C#


Where did I go wrong. How do I make this right


public ShortestJobFirst(int pollTime)
        {
            if (pollTime < 1)
            {
                throw new ArgumentOutOfRangeException(Resources.PollTimeGreaterThanZero);
            }

            _pollTime = pollTime;
        }

       
        public void Execute(Runner runner)
        {
            List readyQueue = new List(runner.ProcessLoad);
            while (readyQueue.Count > 0)
            {
                try
                {
                    // select the process that is eligible to run with the smallest remaining burst time
                    Process p = readyQueue.Where(x => x.ArrivalTime <= runner.Time).OrderBy(x => x.BurstTime - x.Data.UtilizedCpuTime).First();
                    runner.UtilizeCpu(p, _pollTime);
                    if (Process.IsComplete(p))
                    {
                        runner.LogProcessMetrics(p);
                        readyQueue.Remove(p);
                    }
                }
                catch (InvalidOperationException)
                {
                    runner.SkipIdleCpuTime();
                }
            }
        }

        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "Shortest Job First (Job Time = {0})", _pollTime);
        }
}

No comments:

Post a Comment