Esempio n. 1
0
        public void PutInPipelines()
        {
            for (int i = 0; i < CPU_Count; i++)
            {
                // List that will contain processes that are fit to enter the pipeline (processes that have arrived)
                List <Process> pendingProcesses = new List <Process>();

                for (int j = 0; j < ProcessList.Count; j++)
                {
                    // A process is fit to enter the pipeline if it has arrived, it isn't currently executing, it hasn't finished executing and it isn't stalled
                    if (processListCopy[j].ArrivalTime < 1 && processListCopy[j].Executing == false && processListCopy[j].FinishedExecuting == false && processListCopy[j].Stalled == false)
                    {
                        pendingProcesses.Add(processListCopy[j]);
                    }
                }

                // If there are any candidate processes for entering the pipeline
                if (pendingProcesses.Count > 0)
                {
                    // Sort the pending processes by time needed for processing (because of SJN)
                    if (pendingProcesses.Count > 1)
                    {
                        alg.SortByProcessingTime(pendingProcesses);
                    }

                    // Check if there is any pipeline empty
                    bool flag = false;
                    for (int k = 0; k < MultiCorePipeline.Count; k++)
                    {
                        if (MultiCorePipeline[k].OfType <Process>().Any <Process>() == false)
                        {
                            // If true, add the best fit pending process to the pipeline
                            pendingProcesses[0].Executing = true;
                            MultiCorePipeline[k].Add(pendingProcesses[0]);
                            flag = true;
                            break;
                        }
                    }

                    if (flag == false)
                    {
                        if (MultiCorePipeline[i].OfType <Process>().Last <Process>().ProccesingTime - MultiCorePipeline[i].OfType <Process>().Last <Process>().ExecutedTime == 0)
                        {
                            // If true, add the best fit pending process to the pipeline and also deal with the finished process
                            MultiCorePipeline[i].OfType <Process>().Last <Process>().Executing         = false;
                            MultiCorePipeline[i].OfType <Process>().Last <Process>().FinishedExecuting = true;

                            pendingProcesses[0].Executing = true;
                            MultiCorePipeline[i].Add(pendingProcesses[0]);
                        }
                        // If not, check if the best fit pending process has a smaller processing time than the one currently in the pipe
                        else if ((pendingProcesses[0].ProccesingTime - pendingProcesses[0].ExecutedTime) < (MultiCorePipeline[i].OfType <Process>().Last <Process>().ProccesingTime - MultiCorePipeline[i].OfType <Process>().Last <Process>().ExecutedTime))
                        {
                            MultiCorePipeline[i].OfType <Process>().Last <Process>().Executing = false;

                            //Not in use anymore
                            //MultiCorePipeline[i].OfType<Process>().Last<Process>().Stalled = true;

                            // If true, eliminate process from pipeline, but keep a clone of it so it can be added later on
                            Process expelledProcess = MultiCorePipeline[i].OfType <Process>().Last <Process>().Clone() as Process;
                            MultiCorePipeline[i].Remove(MultiCorePipeline[i].OfType <Process>().Last <Process>());
                            MultiCorePipeline[i].Add(expelledProcess);

                            for (int j = 0; j < ProcessList.Count; j++)
                            {
                                if (MultiCorePipeline[i].OfType <Process>().Last <Process>().ID == processListCopy[j].ID) // Copy neaparat
                                {
                                    // This line of code cost me ~1.5 hours of debugging. I'll keep it (commented) to remind myself of stupid mistakes.
                                    // processListCopy[j].ProccesingTime = ProcessList[j].ProccesingTime - MultiCorePipeline[i].OfType<Process>().Last<Process>().ExecutedTime;
                                    processListCopy[j].ProccesingTime = processListCopy[j].ProccesingTime - MultiCorePipeline[i].OfType <Process>().Last <Process>().ExecutedTime;
                                    processListCopy[j].ExecutedTime   = 0;
                                    processListCopy[j].Executing      = false;

                                    processListCopy[j].Stalled   = true;
                                    processListCopy[j].StallTime = pipelineTime + 1;
                                }
                            }

                            pendingProcesses[0].Executing = true;
                            MultiCorePipeline[i].Add(pendingProcesses[0]);
                        }
                    }
                }
                // If there are no candidate processes
                else
                {
                    for (int j = 0; j < CPU_Count; j++)
                    {
                        // Finish processes that have executed fully
                        try
                        {
                            if (MultiCorePipeline[j].OfType <Process>().Last <Process>().ProccesingTime - MultiCorePipeline[j].OfType <Process>().Last <Process>().ExecutedTime == 0)
                            {
                                MultiCorePipeline[j].OfType <Process>().Last <Process>().Executing         = false;
                                MultiCorePipeline[j].OfType <Process>().Last <Process>().FinishedExecuting = true;

                                //Not in use anymore
                                //MultiCorePipeline[j].OfType<Process>().Last<Process>().Stalled = false;
                            }
                        }
                        catch (Exception ex)
                        {
                            // handle
                        }
                    }
                }
            }
        }