private int?MinimumTime(List <OSASProcess> processes, OSASProcess process, int currentTime)
        {
            var proc = processes.Where(b => b.Id != process.Id)
                       .Where(a => a.ArrivalTime <= currentTime).ToList();

            return((proc.Count > 0) ? (int?)proc.Min(a => a.RemainingTime) : null);
        }
Esempio n. 2
0
        public void SimulateProcesses()
        {
            if (Processes.Count <= 0)
            {
                return;
            }

            // order processes by arrival time
            Processes = Processes.OrderBy(a => a.ArrivalTime).ToList();


            var currentTime = 0;

            while (Processes.Count > 0)
            {
                if (Processes[0].ArrivalTime > currentTime)
                {
                    var process = new OSASProcess();

                    process.Id            = -1;
                    process.StartTime     = currentTime;
                    process.BurstTime    += Processes[0].ArrivalTime - currentTime;
                    process.FinishTime    = process.StartTime + process.BurstTime;
                    process.RemainingTime = 0;
                    currentTime           = process.FinishTime;

                    TerminatedProcesses.Add(process);
                    continue;
                }

                Processes[0].StartTime = Math.Max(currentTime, Processes[0].ArrivalTime);

                Processes[0].IsWorking = true;

                currentTime = Processes[0].StartTime;

                currentTime += Processes[0].BurstTime;

                Processes[0].FinishTime = currentTime;

                Processes[0].RemainingTime = 0;

                Processes[0].IsWorking = false;

                Processes[0].WaitTime = Processes[0].StartTime - Processes[0].ArrivalTime;

                Processes[0].TurnAroundTime = Processes[0].FinishTime - Processes[0].ArrivalTime;

                TerminatedProcesses.Add(Processes[0]);
                Processes.Remove(Processes[0]);
            }

            CalculatedSuccessfully = true;
        }
        private int LastFinishOrArrival(List <OSASProcess> processes, OSASProcess process)
        {
            var pro = processes.Where(a => a.Id.Equals(process.Id)).ToList();

            if (pro.Count > 0)
            {
                return(pro.OrderBy(a => a.FinishTime).LastOrDefault().FinishTime);
            }
            else
            {
                return(process.ArrivalTime);
            }
        }
        public void SimulateProcesses()
        {
            if (Processes.Count <= 0)
            {
                return;
            }

            //Processes = Processes.OrderBy(a => a.ArrivalTime).ToList();

            var currentTime = 0;

            while (Processes.Count > 0)
            {
                var ArrivedProcesses = Processes.Where(a => a.ArrivalTime <= currentTime).ToList();

                if (ArrivedProcesses.Count == 0)
                {
                    var process = new OSASProcess();
                    process.Id            = -1;
                    process.StartTime     = currentTime;
                    process.BurstTime    += (Processes.FirstOrDefault().ArrivalTime - currentTime);
                    process.FinishTime    = process.StartTime + process.BurstTime;
                    process.RemainingTime = 0;
                    currentTime           = process.FinishTime;
                    TerminatedProcesses.Add(process);
                    continue;
                }

                // Order Arrived processes by priority
                ArrivedProcesses = Ascending ? ArrivedProcesses.OrderBy(a => a.Priority).ToList() :
                                   ArrivedProcesses.OrderByDescending(a => a.Priority).ToList();

                var p = ArrivedProcesses.FirstOrDefault();

                p.StartTime = currentTime;

                currentTime += p.BurstTime;

                p.FinishTime = currentTime;

                p.RemainingTime = 0;

                p.WaitTime = p.StartTime - p.ArrivalTime;

                p.TurnAroundTime = p.FinishTime - p.ArrivalTime;

                Processes.Remove(p);
                TerminatedProcesses.Add(p);
            }
        }
        void SimulateNonPreemptive()
        {
            List <OSASProcess> ArrivedProcess = new List <OSASProcess>();

            Processes = Processes.OrderBy(a => a.ArrivalTime).ToList();

            var currentTime = 0;

            while (Processes.Count > 0)
            {
                ArrivedProcess = Processes.Where(a => a.ArrivalTime <= currentTime).ToList();

                if (ArrivedProcess.Count == 0)
                {
                    var process = new OSASProcess();
                    process.Id            = -1;
                    process.StartTime     = currentTime;
                    process.BurstTime    += (Processes.FirstOrDefault().ArrivalTime - currentTime);
                    process.FinishTime    = process.StartTime + process.BurstTime;
                    process.RemainingTime = 0;
                    currentTime           = process.FinishTime;
                    TerminatedProcesses.Add(process);
                    continue;
                }

                ArrivedProcess = ArrivedProcess.OrderBy(a => a.BurstTime).ToList();

                var p = ArrivedProcess.FirstOrDefault();

                p.StartTime = currentTime;

                currentTime += p.BurstTime;

                p.FinishTime = currentTime;

                p.RemainingTime = 0;

                p.WaitTime = p.StartTime - p.ArrivalTime;

                p.TurnAroundTime = p.FinishTime - p.ArrivalTime;

                Processes.Remove(Processes.FirstOrDefault(a => a.Id.Equals(p.Id)));
                TerminatedProcesses.Add(p);
            }
            CalculatedSuccessfully = true;
        }
        public void SimulateProcesses()
        {
            if (Processes.Count <= 0)
            {
                return;
            }

            // If Time unit smaller than or equal 0
            if (TimeUnit <= 0)
            {
                // Use FSFC(First Come First Served) Algorithm
                var Fcfs = new CPU_FCFS_Algorithm(Processes);

                Fcfs.SimulateProcesses();

                TerminatedProcesses = Fcfs.TerminatedProcesses;

                // and return here
                return;
            }

            var currentTime = 0;

point:

            var PQueue = Processes.Where(a => a.ArrivalTime <= currentTime).OrderBy(a => a.ArrivalTime).ToList();

            if (PQueue.Count == 0)
            {
                var process = new OSASProcess();
                process.Id            = -1;
                process.StartTime     = currentTime;
                process.BurstTime    += (Processes.OrderBy(a => a.ArrivalTime).FirstOrDefault().ArrivalTime - currentTime);
                process.FinishTime    = process.StartTime + process.BurstTime;
                process.RemainingTime = 0;
                currentTime           = process.FinishTime;
                TerminatedProcesses.Add(process);
                goto point;
            }

            while (PQueue.Count > 0)
            {
                var p = PQueue.FirstOrDefault();

                if (p.RemainingTime > TimeUnit)
                {
                    PQueue.Remove(p);

                    p.StartTime = currentTime;

                    p.RemainingTime -= TimeUnit;

                    currentTime += TimeUnit;

                    var pro = new OSASProcess(p)
                    {
                        FinishTime = currentTime,
                        WaitTime   = p.StartTime - LastFinishOrArrival(TerminatedProcesses, p)
                    };

                    TerminatedProcesses.Add(pro);
                    Processes.Remove(p);

                    PQueue.AddRange(Processes.Where(a => a.ArrivalTime <= currentTime &&
                                                    !PQueue.Exists(b => b.Id.Equals(a.Id)) &&
                                                    a.Id != p.Id).OrderBy(a => a.ArrivalTime));

                    PQueue.Add(p);
                }
                else
                {
                    PQueue.Remove(p);

                    p.StartTime = currentTime;

                    currentTime += p.RemainingTime;

                    p.RemainingTime = 0;

                    p.WaitTime = p.StartTime - LastFinishOrArrival(TerminatedProcesses, p);

                    p.FinishTime = currentTime;

                    p.TurnAroundTime = p.FinishTime - p.ArrivalTime;

                    TerminatedProcesses.Add(p);
                    Processes.Remove(p);

                    PQueue.AddRange(Processes.Where(a => a.ArrivalTime <= currentTime &&
                                                    !PQueue.Exists(b => b.Id.Equals(a.Id)))
                                    .OrderBy(a => a.ArrivalTime));
                }

                if (PQueue.Count == 0 && Processes.Count > 0)
                {
                    var process = new OSASProcess();
                    process.Id            = -1;
                    process.StartTime     = currentTime;
                    process.BurstTime    += (Processes.OrderBy(a => a.ArrivalTime).FirstOrDefault().ArrivalTime - currentTime);
                    process.FinishTime    = process.StartTime + process.BurstTime;
                    process.RemainingTime = 0;
                    currentTime           = process.FinishTime;
                    TerminatedProcesses.Add(process);
                    PQueue.AddRange(Processes.Where(a => a.ArrivalTime <= currentTime &&
                                                    !PQueue.Exists(b => b.Id.Equals(a.Id)))
                                    .OrderBy(a => a.ArrivalTime));
                }
            }

            Processes.Clear();

            CalculatedSuccessfully = true;
        }
        void SimulatePreemptive()
        {
            List <OSASProcess> ArrivedProcess = new List <OSASProcess>();

            Processes = Processes.OrderBy(a => a.ArrivalTime).ToList();

            var currentTime = 0;

            while (Processes.Count > 0)
            {
point:
                ArrivedProcess = Processes.Where(a => a.ArrivalTime <= currentTime).ToList();

                if (ArrivedProcess.Count == 0)
                {
                    var process = new OSASProcess();
                    process.Id            = -1;
                    process.StartTime     = currentTime;
                    process.BurstTime    += (Processes.FirstOrDefault().ArrivalTime - currentTime);
                    process.FinishTime    = process.StartTime + process.BurstTime;
                    process.RemainingTime = 0;
                    currentTime           = process.FinishTime;
                    TerminatedProcesses.Add(process);
                    continue;
                }

                ArrivedProcess = ArrivedProcess.OrderBy(a => a.RemainingTime).ToList();

                foreach (var p in ArrivedProcess)
                {
                    p.StartTime = currentTime;

                    while (p.RemainingTime > 0)
                    {
                        currentTime++;
                        p.RemainingTime--;

                        var minTime = MinimumTime(Processes, p, currentTime);
                        if (minTime.HasValue)
                        {
                            if (p.RemainingTime > minTime.Value)
                            {
                                var process = new OSASProcess(p)
                                {
                                    FinishTime = currentTime,
                                    WaitTime   = p.StartTime - LastFinishOrArrival(TerminatedProcesses, p)
                                };

                                TerminatedProcesses.Add(process);
                                goto point;
                            }
                        }
                    }
                    p.FinishTime     = currentTime;
                    p.WaitTime       = p.StartTime - LastFinishOrArrival(TerminatedProcesses, p);
                    p.TurnAroundTime = p.FinishTime - p.ArrivalTime;
                    TerminatedProcesses.Add(p);

                    if (p.RemainingTime <= 0)
                    {
                        Processes.Remove(p);
                        break;
                    }
                }
            }
            CalculatedSuccessfully = true;
        }