コード例 #1
0
        public override void Reset(List <Process> _processes)
        {
            List <Process> copyProcesses = new List <Process>();

            foreach (var item in _processes)
            {
                copyProcesses.Add(new Process(item));
            }
            allProcesses       = new List <Process>(copyProcesses);
            unStartedProcesses = new List <Process>(allProcesses);
            finishedProcesses  = new List <Process>();
            currentProcess     = null;
            isEnded            = false;
            foreach (var item in allProcesses)
            {
                item.remainingBurstTime = item.initialBurstTime;
                item.endStep            = 0;
            }
            foreach (var queue in queues)
            {
                queue.Reset(new List <Process>());
            }
            currentQueue = queues[0];
            counter      = 0;
        }
コード例 #2
0
        public override Process Step(int step)
        {
            this.step         = step;
            processesToRemove = new List <Process>();
            foreach (Process process in unStartedProcesses)
            {
                if (process.arrivalTime == step)
                {
                    queues[0].processQueue.Enqueue(process);
                    processesToRemove.Add(process);
                }
            }
            foreach (var process in processesToRemove)
            {
                unStartedProcesses.Remove(process);
            }
            int i = 0;

            if (currentQueue.currentProcess == null)
            {
                foreach (var q in queues)
                {
                    if (q.processQueue.Count > 0)
                    {
                        counter          = i;
                        currentQueue     = q;
                        q.currentProcess = q.processQueue.Dequeue();
                        break;
                    }
                    i++;
                }
            }
            if (currentQueue.currentProcess != null)
            {
                if (currentQueue.currentProcess.remainingBurstTime <= 0)
                {
                    currentQueue.currentProcess.endStep = step;
                    finishedProcesses.Add(currentQueue.currentProcess);
                    currentQueue.currentProcess = null;
                    i = 0;
                    foreach (var q in queues)
                    {
                        if (q.processQueue.Count > 0)
                        {
                            counter          = i;
                            currentQueue     = q;
                            q.currentProcess = q.processQueue.Dequeue();
                            q.duration       = 0;
                            break;
                        }
                        i++;
                    }
                    if (currentQueue.currentProcess == null)
                    {
                        if (finishedProcesses.Count == allProcesses.Count)
                        {
                            isEnded = true;
                        }
                        return(null);
                    }
                }
                else if (currentQueue.isQuantumUsed())
                {
                    if (currentQueue == queues[queues.Count - 1])
                    {
                        currentQueue.processQueue.Enqueue(currentQueue.currentProcess);
                        currentQueue.currentProcess = currentQueue.processQueue.Dequeue();
                        currentQueue.duration       = 0;
                    }
                    else
                    {
                        queues[++counter].processQueue.Enqueue(currentQueue.currentProcess);
                        currentQueue = queues[counter];
                        i            = 0;
                        foreach (var q in queues)
                        {
                            if (q.processQueue.Count > 0)
                            {
                                counter      = i;
                                currentQueue = q;
                                break;
                            }
                            i++;
                        }
                        currentQueue.currentProcess = currentQueue.processQueue.Dequeue();
                        currentQueue.duration       = 0;
                    }
                }
                currentQueue.currentProcess.remainingBurstTime -= 1;
                currentQueue.duration++;
            }

            return(currentQueue.currentProcess);
        }
コード例 #3
0
        public override StepResponse Step(int step)
        {
            bool isPageDemanded = false;

            this.step         = step;
            processesToRemove = new List <Process>();
            bool isDemanded = false;

            foreach (Process process in unStartedProcesses)
            {
                if (process.arrivalTime == step)
                {
                    {
                        queues[0].processQueue.Enqueue(process);
                    }
                    processesToRemove.Add(process);
                }
            }
            foreach (var process in processesToRemove)
            {
                unStartedProcesses.Remove(process);
            }
            int i = 0;

            if (currentQueue.currentProcess == null)
            {
                foreach (var q in queues)
                {
                    if (q.processQueue.Count > 0)
                    {
                        counter          = i;
                        currentQueue     = q;
                        q.currentProcess = q.processQueue.Dequeue();
                        //FORCE DEMAND
                        isPageDemanded = ForceDemandPage();
                        isDemanded     = true;
                        break;
                    }
                    i++;
                }
            }
            if (currentQueue.currentProcess != null)
            {
                if (currentQueue.currentProcess.remainingBurstTime <= 0)
                {
                    currentQueue.currentProcess.endStep = step;
                    finishedProcesses.Add(currentQueue.currentProcess);
                    currentQueue.currentProcess = null;
                    i = 0;
                    foreach (var q in queues)
                    {
                        if (q.processQueue.Count > 0)
                        {
                            counter          = i;
                            currentQueue     = q;
                            q.currentProcess = q.processQueue.Dequeue();
                            //FORCE DEMAND
                            isPageDemanded = ForceDemandPage();
                            isDemanded     = true;
                            q.duration     = 0;
                            break;
                        }
                        i++;
                    }
                    if (currentQueue.currentProcess == null)
                    {
                        if (finishedProcesses.Count == allProcesses.Count)
                        {
                            isEnded        = true;
                            isDemanded     = true;
                            isPageDemanded = true;
                        }
                        return(null);
                    }
                }
                else if (currentQueue.isQuantumUsed())
                {
                    Process lastProcess = currentQueue.currentProcess;
                    if (currentQueue == queues[queues.Count - 1])
                    {
                        currentQueue.processQueue.Enqueue(currentQueue.currentProcess);
                        currentQueue.currentProcess = currentQueue.processQueue.Dequeue();
                        currentQueue.duration       = 0;
                    }
                    else
                    {
                        queues[++counter].processQueue.Enqueue(currentQueue.currentProcess);
                        currentQueue = queues[counter];
                        i            = 0;
                        foreach (var q in queues)
                        {
                            if (q.processQueue.Count > 0)
                            {
                                counter      = i;
                                currentQueue = q;
                                break;
                            }
                            i++;
                        }

                        currentQueue.currentProcess = currentQueue.processQueue.Dequeue();
                        bool isThisProcessInNextQueueTheSameQueueAsThis = currentQueue.currentProcess == lastProcess;

                        //FORCE DEMAND
                        if (!isThisProcessInNextQueueTheSameQueueAsThis)
                        {
                            isPageDemanded = ForceDemandPage();
                            isDemanded     = true;
                        }
                        currentQueue.duration = 0;
                    }
                }
                //CHECK DEMAND
                if (!isDemanded)
                {
                    isPageDemanded = CheckAndkDemandPageIfRequired();
                }

                currentQueue.currentProcess.remainingBurstTime -= 1;
                currentQueue.duration++;
            }

            return(new StepResponse(currentQueue.currentProcess, true, this, isPageDemanded, isEnded));
        }