Пример #1
0
        public List <Execution> StartExecution()
        {
            insertions.Sort();

            Process   runningProcess = null;
            Execution execution      = null;

            for (int t = 0; insertions.Count != 0 || !queue.isEmpty() || runningProcess != null; t++)
            {
                //Console.WriteLine("<<<<  Time #" + t + "  >>>>");

                // Add processes
                while (insertions.Count != 0 && t == insertions[0].InsertionTime)
                {
                    processes.Add(insertions[0].Process);
                    queue.add(insertions[0].Process);
                    insertions.RemoveAt(0);
                }

                // Select Process
                if (!queue.isEmpty())
                {
                    if (runningProcess == null)
                    {
                        runningProcess = queue.getFirst();
                        queue.remove();
                        execution = new Execution(runningProcess, t, t);
                    }
                    else if (preemptive)
                    {
                        bool switchProcess = false;

                        if (queue.QueueType == QueueType.FCFS)
                        {
                            switchProcess = false;
                        }
                        else if (queue.QueueType == QueueType.SJF)
                        {
                            switchProcess = (runningProcess.GetRemainingTime() > queue.getFirst().GetRemainingTime());
                        }
                        else if (queue.QueueType == QueueType.PRIORITY)
                        {
                            switchProcess = (runningProcess.Priority > queue.getFirst().Priority);
                        }
                        else if (queue.QueueType is RoundRobinQueueType)
                        {
                            switchProcess = (execution.EndTime - execution.StartTime) >= ((RoundRobinQueueType)queue.QueueType).Quantum;
                        }

                        if (switchProcess)
                        {
                            executionList.Add(execution);
                            runningProcess.AddExecution(execution);

                            queue.add(runningProcess);
                            runningProcess = queue.getFirst();
                            queue.remove();
                            execution = new Execution(runningProcess, t, t);
                        }
                    }
                }
                // split consecutive quantums for only one process case
                else if (queue.isEmpty() && queue.QueueType is RoundRobinQueueType && execution != null && (execution.EndTime - execution.StartTime) >= ((RoundRobinQueueType)queue.QueueType).Quantum)
                {
                    executionList.Add(execution);
                    runningProcess.AddExecution(execution);
                    execution = new Execution(runningProcess, t, t);
                }

                //Console.WriteLine(" -> " + runningProcess);
                //printQueue();

                // Execute unit time of a process
                if (runningProcess != null)
                {
                    runningProcess.decrementRemiainingTime();
                    execution.EndTime++;

                    // Remove process from queue and add an execution entry
                    if (runningProcess.isFinished())
                    {
                        executionList.Add(execution);
                        runningProcess.AddExecution(execution);

                        runningProcess = null;
                        execution      = null;
                    }
                }
            }
            return(executionList);
        }