Exemplo n.º 1
0
        public static List <Result> Run(List <Process> jobList, List <Result> resultList)
        {
            int currentProcess = 0;
            int cpuTime        = 0;
            int cpuDone        = 0;

            int runTime = 0;

            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)
                {
                    Process frontJob = jobList.ElementAt(0);
                    if (frontJob.arriveTime == runTime)
                    {
                        readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, frontJob.priority));
                        jobList.RemoveAt(0);
                    }
                    else
                    {
                        break;
                    }
                }

                if (currentProcess == 0)
                {
                    if (readyQueue.Count != 0)
                    {
                        ReadyQueueElement rq = readyQueue.ElementAt(0);
                        resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime));
                        cpuDone        = rq.burstTime;
                        cpuTime        = 0;
                        currentProcess = rq.processID;
                        readyQueue.RemoveAt(0);
                    }
                }
                else
                {
                    if (cpuTime == cpuDone)
                    {
                        currentProcess = 0;
                        continue;
                    }
                }

                cpuTime++;
                runTime++;

                for (int i = 0; i < readyQueue.Count; i++)
                {
                    readyQueue.ElementAt(i).waitingTime++;
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0);

            return(resultList);
        }
Exemplo n.º 2
0
        public static List <Result> Run(List <Process> jobList, List <Result> resultList)
        {
            int currentProcess = 0;//현재 실행되고 있는 프로세스
            int cpuTime        = 0;
            int cpuDone        = 0;

            int runTime = 0;
            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)                                                                                   //잡 리스트  갯수가 0이 아닐 때
                {
                    Process frontJob = jobList.ElementAt(0);                                                                 //프런트잡에 잡리스트의 0번째 요소 넣기

                    if (frontJob.arriveTime == runTime)                                                                      //프런트잡의 도착시간이랑 실행시간이랑 같으면
                    {
                        readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, frontJob.priority)); //레디큐에 PID랑 Burst Time 갖다박기
                        jobList.RemoveAt(0);                                                                                 //잡리스트 0번째 삭제
                    }
                    else//프런트잡의 도착시간이랑 실행시간이랑 다르면
                    {
                        break;
                    }
                }
                int min;
                ReadyQueueElement temp;
                if (currentProcess == 0)                               //현재 실행되고 있는 프로세스가 없다면
                {
                    if (readyQueue.Count != 0)                         //이때 레디큐가 비어있지 않으면
                    {
                        for (int i = 0; i < readyQueue.Count - 1; i++) //우선순위가 높은 순서대로 레디큐의 프로세스들을 소팅
                        {
                            min = i;
                            for (int j = i + 1; j < readyQueue.Count; j++)
                            {
                                if (readyQueue.ElementAt(j).priority < readyQueue.ElementAt(min).priority)
                                {
                                    min = j;
                                }
                            }
                            temp            = readyQueue[i];
                            readyQueue[i]   = readyQueue[min];
                            readyQueue[min] = temp;
                        }
                        ReadyQueueElement rq = readyQueue.ElementAt(0);                                  //rq에 레디큐 0번째 요소 넣기
                        resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime)); //결과리스트에 rq의 PID, Run Time, Burst Time, Wating Time 넣기
                        cpuDone        = rq.burstTime;                                                   //cpuDone에 rq의 Burst Time 넣기
                        cpuTime        = 0;                                                              //CPU 시간 = 0
                        currentProcess = rq.processID;                                                   //현재 실행되고 있는 프로세스 = rq의 PID
                        readyQueue.RemoveAt(0);                                                          //레디큐의 0번째 삭제
                    }
                }
                else//현재 실행되고 있는 프로세스가 있다면
                {
                    if (cpuTime == cpuDone)//cpu시간과 == cpuDone 같으면
                    {
                        currentProcess = 0;//현재 실행되고 있는 프로세스 없도록 만들어줌
                        continue;
                    }
                }

                cpuTime++;                                                                //cpu시간 1증가
                runTime++;                                                                //런타임 1증가

                for (int i = 0; i < readyQueue.Count; i++)                                //레디큐 남은 갯수만큼
                {
                    readyQueue.ElementAt(i).waitingTime++;                                //레디큐에 있는 모든 프로세스들 waiting Time 1씩 증가
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0); //잡리스트나 레디큐나 current프로세스의 갯수가 0이 되면 탈출

            return(resultList);
        }
Exemplo n.º 3
0
        public static List <Result> Run(List <Process> jobList, List <Result> resultList, int timequantum)
        {
            int currentProcess = 0;
            int cpuTime        = 0;
            int cpuDone        = 0;

            int runTime = 0;

            int bigger_than_quantum = 0;    //burstTime이 timeQuantum보다 큰지 확인하기 위한 카운팅 변수
            ReadyQueueElement temp  = null; //ReadyQueueElement 형을 임시로 저장할 곳 선언

            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)
                {
                    Process frontJob = jobList.ElementAt(0);
                    if (frontJob.arriveTime == runTime)
                    {
                        readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, frontJob.priority));
                        jobList.RemoveAt(0);
                    }
                    else
                    {
                        break;
                    }
                }

                if (currentProcess == 0)
                {
                    if (readyQueue.Count != 0)
                    {
                        ReadyQueueElement rq = readyQueue.ElementAt(0);
                        cpuTime        = 0;
                        currentProcess = rq.processID;
                        if (rq.burstTime > timequantum)                                                     //burstTime보다 timeQuantum보다 클 때
                        {
                            bigger_than_quantum++;                                                          //burstTime이 timeQuantum보다 클 때 카운트
                            resultList.Add(new Result(rq.processID, runTime, timequantum, rq.waitingTime)); //리스트에 넣을때 burstTime을 timeQuantum으로 추가
                            cpuDone      = timequantum;                                                     //끝나는 시간을 timequantum으로 설정
                            rq.burstTime = rq.burstTime - timequantum;                                      //burstTime에서 timequantum만큼 빼줌
                            temp         = rq;                                                              //프로세스가 끝날 때 남은 부분을 레디큐에 넣어주기위해 임시로 저장
                        }
                        else//burstTime보다 timeQuantum보다 작거나 같을 때
                        {
                            resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime));
                            cpuDone = rq.burstTime;//끝나는시간은 burstTime
                        }
                        readyQueue.RemoveAt(0);
                    }
                }
                else
                {
                    if (cpuTime == cpuDone)
                    {
                        currentProcess = 0;
                        if (bigger_than_quantum > 0)                                                                 //burstTime이 timeQuantum보다 크다고 카운트 되었을 때
                        {
                            readyQueue.Add(new ReadyQueueElement(temp.processID, temp.burstTime, 0, temp.priority)); //임시로 저장해 둔 남은 프로세스부분을 레디큐에 넣어주기
                            bigger_than_quantum = 0;                                                                 //카운팅 변수 초기화
                        }
                        continue;
                    }
                }

                cpuTime++;
                runTime++;

                for (int i = 0; i < readyQueue.Count; i++)
                {
                    readyQueue.ElementAt(i).waitingTime++;
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0);

            return(resultList);
        }
        public static List <Result> Run(List <Process> jobList, List <Result> resultList)
        {
            int currentProcess = 0;
            int cpuTime        = 0;
            int cpuDone        = 0;
            int runTime        = 0;
            int highPriIndex   = 0;
            int highPriTemp;

            void getHighestPriority_rq(List <ReadyQueueElement> rq)
            {
                highPriTemp = rq.ElementAt(0).priority;
                for (int i = 0; i < rq.Count; i++)
                {
                    if (rq.ElementAt(i).priority <= highPriTemp)
                    {
                        highPriTemp  = rq.ElementAt(i).priority;
                        highPriIndex = i;
                    }
                }
            }

            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)
                {
                    Process frontJob = jobList.ElementAt(0);
                    if (frontJob.arriveTime == runTime)
                    {
                        readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, frontJob.priority, false));
                        jobList.RemoveAt(0);
                    }
                    else
                    {
                        break;
                    }
                }

                if (currentProcess == 0)
                {
                    if (readyQueue.Count != 0)
                    {
                        getHighestPriority_rq(readyQueue);
                        ReadyQueueElement rq = readyQueue.ElementAt(highPriIndex);
                        resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime));
                        cpuDone        = rq.burstTime;
                        cpuTime        = 0;
                        currentProcess = rq.processID;
                        readyQueue.RemoveAt(highPriIndex);
                    }
                }
                else
                {
                    if (cpuTime == cpuDone)
                    {
                        currentProcess = 0;
                        continue;
                    }
                }

                cpuTime++;
                runTime++;

                for (int i = 0; i < readyQueue.Count; i++)
                {
                    readyQueue.ElementAt(i).waitingTime++;
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0);

            return(resultList);
        }
        public static List <Result> Run(List <Process> jobList, List <Result> resultList)
        {
            int currentProcess        = 0;
            int cpuTime               = 0;
            int cpuDone               = 0;
            int runTime               = 0;
            int shortestBurstIndex_jl = 0;
            int shortestBurstTemp;
            int chkRqCntChng = 0;
            int cntTemp      = 0;

            //void getShortestBurst_rq(List<ReadyQueueElement> rq)
            //{
            //    shortestBurstTemp = rq.ElementAt(0).burstTime;
            //    for (int i = 0; i < rq.Count; i++)
            //    {
            //        if (rq.ElementAt(i).burstTime <= shortestBurstTemp)
            //        {
            //            shortestBurstTemp = rq.ElementAt(i).burstTime;
            //            shortestBurstIndex_rq = i;
            //        }
            //    }
            //}

            void getShortestBurst_jl(List <Process> jl)
            {
                shortestBurstTemp = jl.ElementAt(0).burstTime;
                for (int i = 0; i < jl.Count; i++)
                {
                    if (jl.ElementAt(i).burstTime <= shortestBurstTemp)
                    {
                        shortestBurstTemp     = jl.ElementAt(i).burstTime;
                        shortestBurstIndex_jl = i;
                    }
                }
            }

            bool isRqChanged(List <ReadyQueueElement> rq)
            {
                if (chkRqCntChng == rq.Count)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }

            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)
                {
                    Process frontJob = jobList.ElementAt(0);
                    if (frontJob.arriveTime == runTime)
                    {
                        readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0));
                        if (readyQueue.Count > 1)
                        {
                            readyQueue.Sort(delegate(ReadyQueueElement x, ReadyQueueElement y)
                            {
                                if (x.burstTime > y.burstTime)
                                {
                                    return(1);
                                }
                                else if (x.burstTime < y.burstTime)
                                {
                                    return(-1);
                                }
                                return(0);
                            });
                        }
                        jobList.RemoveAt(0);
                        chkRqCntChng = readyQueue.Count;
                    }
                    else
                    {
                        break;
                    }
                }

                if (currentProcess == 0)
                {
                    if (readyQueue.Count != 0)
                    {
                        ReadyQueueElement rq = readyQueue.ElementAt(0);

                        if (jobList.Count < 1)
                        {
                            resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime));
                            cpuDone        = rq.burstTime;
                            cpuTime        = 0;
                            currentProcess = rq.processID;
                            readyQueue.RemoveAt(0);
                        }
                        else
                        {
                            if (rq.burstTime <= jobList.ElementAt(0).burstTime)
                            {
                                resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime));
                                cpuDone        = rq.burstTime;
                                cpuTime        = 0;
                                currentProcess = rq.processID;
                                readyQueue.RemoveAt(0);
                            }
                            else
                            {
                                resultList.Add(new Result(rq.processID, runTime, runTime - resultList.ElementAt(resultList.Count).runTime, rq.waitingTime));
                                readyQueue.Add(new ReadyQueueElement(rq.processID, (rq.burstTime - (runTime - resultList.ElementAt(resultList.Count).runTime)), 0 - jobList.ElementAt(0).arriveTime, true));
                                cpuDone        = (rq.burstTime - (runTime - resultList.ElementAt(resultList.Count).runTime));
                                cpuTime        = 0;
                                currentProcess = rq.processID;
                                readyQueue.RemoveAt(0);
                            }
                        }
                    }
                }
                else
                {
                    if (cpuTime == cpuDone)
                    {
                        currentProcess = 0;
                        continue;
                    }
                }

                cpuTime++;
                runTime++;

                for (int i = 0; i < readyQueue.Count; i++)
                {
                    readyQueue.ElementAt(i).waitingTime++;
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0);

            return(resultList);
        }
        public static List <Result> Run(List <Process> jobList, List <Result> resultList, int timeQuantum)
        {
            int currentProcess  = 0;
            int previousprocess = 0;
            int cpuTime         = 0;
            int cpuDone         = 0;
            int runTime         = 0;
            int check           = 0;
            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)
                {
                    Process frontJob = jobList.ElementAt(0);
                    if (frontJob.arriveTime == runTime)
                    {
                        if (readyQueue.Count != 0)
                        {
                            int i;
                            for (i = 0; i < readyQueue.Count; i++)
                            {
                                if (readyQueue.ElementAt(i).responseCheck == -1)
                                {
                                    break;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            readyQueue.Insert(i, new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, 0, frontJob.arriveTime, 0, 0));
                            jobList.RemoveAt(0);
                        }
                        else
                        {
                            readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, 0, frontJob.arriveTime, 0, 0));
                            jobList.RemoveAt(0);
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (currentProcess == 0)
                {
                    if (readyQueue.Count != 0)
                    {
                        if (readyQueue.ElementAt(currentProcess).responseCheck == 0)
                        {
                            readyQueue.ElementAt(currentProcess).responseTime   = runTime - readyQueue.ElementAt(currentProcess).arriveTime;
                            readyQueue.ElementAt(currentProcess).responseCheck  = -1;
                            readyQueue.ElementAt(currentProcess).turnaroundTime = readyQueue.ElementAt(currentProcess).burstTime;
                        }
                        ReadyQueueElement rq = readyQueue.ElementAt(0);
                        if (rq.burstTime > timeQuantum)
                        {
                            resultList.Add(new Result(rq.processID, runTime, timeQuantum, rq.waitingTime, rq.responseTime, rq.turnaroundTime + rq.waitingTime));
                            rq.burstTime -= timeQuantum;
                            cpuDone       = timeQuantum;


                            readyQueue.Add(new ReadyQueueElement(rq.processID, rq.burstTime, 0, 0, rq.arriveTime, -1, rq.responseTime));
                            readyQueue.ElementAt(readyQueue.Count - 1).turnaroundTime = rq.turnaroundTime + rq.waitingTime;

                            check = 1;
                        }
                        else
                        {
                            resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime, rq.responseTime, rq.turnaroundTime + rq.waitingTime));
                            cpuDone = rq.burstTime;
                            check   = 0;
                        }
                        cpuTime         = 0;
                        currentProcess  = rq.processID;
                        previousprocess = rq.processID;
                        readyQueue.RemoveAt(0);
                    }
                }
                else
                {
                    if (cpuTime == cpuDone)
                    {
                        currentProcess = 0;
                        continue;
                    }
                }

                cpuTime++;
                runTime++;

                for (int i = 0; i < readyQueue.Count; i++)
                {
                    if (check == 1 && i == readyQueue.Count - 1)
                    {
                        break;
                    }
                    if (readyQueue.ElementAt(0).processID != previousprocess)
                    {
                        readyQueue.ElementAt(i).waitingTime++;
                    }
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0);

            return(resultList);
        }
        public static List <Result> Run(List <Process> jobList, List <Result> resultList)
        {
            int currentProcess = 0;
            int cpuTime        = 0;
            int cpuDone        = 0; //compare로 바꿀까?!바꿔라!
            int runTime        = 0;

            List <ReadyQueueElement> readyQueue = new List <ReadyQueueElement>();

            do
            {
                while (jobList.Count != 0)
                {
                    Process frontJob = jobList.ElementAt(0);
                    if (frontJob.arriveTime == cpuTime)
                    {
                        if (readyQueue.Count != 0)//runtime!=0해야할까?
                        {
                            int i;
                            for (i = 0; i < readyQueue.Count; i++)
                            {
                                if (readyQueue[i].SchedulingcpuTime > frontJob.burstTime || readyQueue[i].SchedulingcpuTime == frontJob.burstTime)
                                {
                                    continue;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            readyQueue.Insert(i, new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, 0, frontJob.arriveTime, 0, 0));
                            readyQueue[i].SchedulingcpuTime = frontJob.burstTime;
                            jobList.RemoveAt(0);
                        }

                        else
                        {
                            readyQueue.Add(new ReadyQueueElement(frontJob.processID, frontJob.burstTime, 0, 0, frontJob.arriveTime, 0, 0));
                            readyQueue[0].SchedulingcpuTime = frontJob.burstTime;
                            jobList.RemoveAt(0);
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                currentProcess = readyQueue.Count - 1;

                if ((readyQueue[currentProcess].SchedulingcpuTime-- == 0))                  //그냥 시간 다 보냈을 경우
                {
                    if (readyQueue.ElementAt(currentProcess).responseCheck == 0)
                    {
                        readyQueue.ElementAt(currentProcess).responseTime   = runTime - readyQueue.ElementAt(currentProcess).arriveTime;
                        readyQueue.ElementAt(currentProcess).responseCheck  = -1;
                        readyQueue.ElementAt(currentProcess).turnaroundTime = readyQueue.ElementAt(currentProcess).burstTime;
                    }
                    ReadyQueueElement rq = readyQueue.ElementAt(currentProcess);

                    resultList.Add(new Result(rq.processID, runTime, rq.burstTime, rq.waitingTime, rq.responseTime, rq.turnaroundTime + rq.waitingTime));
                    cpuDone = 0;
                    readyQueue.RemoveAt(currentProcess);
                    runTime = cpuTime;
                    continue;
                }
                else if ((currentProcess != cpuDone && readyQueue[currentProcess].processID != readyQueue[cpuDone].processID &&
                          (readyQueue[cpuDone].burstTime - readyQueue[cpuDone].SchedulingcpuTime != 0)))
                {    //아직 시간 남아있지만 더 적은게 들어와서 넘겨주는 경우
                    if (readyQueue.ElementAt(cpuDone).responseCheck == 0)
                    {
                        readyQueue.ElementAt(cpuDone).responseTime   = runTime - readyQueue.ElementAt(cpuDone).arriveTime;
                        readyQueue.ElementAt(cpuDone).responseCheck  = -1;
                        readyQueue.ElementAt(cpuDone).turnaroundTime = readyQueue.ElementAt(cpuDone).burstTime;
                    }
                    ReadyQueueElement rq = readyQueue.ElementAt(cpuDone);

                    resultList.Add(new Result(rq.processID, runTime, rq.burstTime - rq.SchedulingcpuTime, rq.waitingTime, rq.responseTime, rq.turnaroundTime + rq.waitingTime));
                    readyQueue[cpuDone].burstTime = readyQueue[cpuDone].SchedulingcpuTime;
                    cpuDone = currentProcess;
                    runTime = cpuTime;
                }
                else
                {
                    cpuDone = currentProcess;
                }
                cpuTime++;

                for (int i = 0; i < readyQueue.Count; i++)
                {
                    if (i == currentProcess || i == cpuDone)
                    {
                        continue;
                    }
                    else
                    {
                        readyQueue.ElementAt(i).waitingTime++;
                    }
                }
            } while (jobList.Count != 0 || readyQueue.Count != 0 || currentProcess != 0);

            return(resultList);
        }