多个队列的最短作业优先

时间:2019-04-12 11:51:48

标签: java eclipse process operating-system scheduling

当前,我有一个程序,该程序可以模拟一组流程的最短作业优先策略。但是,我的任务是使其成为多优先级队列。因此,针对HIGH,MILD,LOW的队列:HIGH = 1,MILD = 2,LOW =3。原始代码运行良好,现在我不得不尝试为3个单独的流程队列实施它。我设法将正确的进程添加到每个队列中,但据我所知。 问题是,如何将SJF策略应用于每个队列

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TEST
{
    List<Process> processList;
    List<Process> highPriorityQueue;
    List<Process> mildPriorityQueue;
    List<Process> lowPriorityQueue;
    List<Process> finalProcessList;
    private int count;
    private int timeQuantum;
    int j=0;
    private int ganntP[];
    private int ganntT[];

    private int totalWaitingTime=0;
    private int totalTurnAroundTime=0;

    private float avgWatingTime=0;
    private float avgTurnaroundTime=0;
    TEST(List<Process> processList)
    {
        count=processList.size();
        this.timeQuantum=timeQuantum;
        this.processList=new ArrayList<Process>();
        this.highPriorityQueue=new ArrayList<Process>();
        this.mildPriorityQueue=new ArrayList<Process>();
        this.lowPriorityQueue=new ArrayList<Process>();
        this.finalProcessList = new ArrayList<Process>();
        ganntT=new int[200];
        ganntP=new int[200];        

        for(Process p : processList)
        {
            if(p.getPriority()==1)
                this.highPriorityQueue.add(new Process(p.getProcessId(), p.getArrivalTime(), p.getBurstTime(),p.getPriority()));
            else if(p.getPriority()==2)
                this.mildPriorityQueue.add(new Process(p.getProcessId(), p.getArrivalTime(), p.getBurstTime(),p.getPriority()));
            else if(p.getPriority()==3)
                this.lowPriorityQueue.add(new Process(p.getProcessId(), p.getArrivalTime(), p.getBurstTime(),p.getPriority()));
        }


        Collections.sort(highPriorityQueue, Process.BY_ARRIVAL_TIME);
        Collections.sort(highPriorityQueue, Process.BY_BURST_TIME);
        Collections.sort(mildPriorityQueue, Process.BY_ARRIVAL_TIME);
        Collections.sort(mildPriorityQueue, Process.BY_BURST_TIME);
        Collections.sort(lowPriorityQueue, Process.BY_ARRIVAL_TIME);
        Collections.sort(lowPriorityQueue, Process.BY_BURST_TIME);

        // CREATE NEW QUEUE WITH COMBINED PRIORITIES IN ORDER
        // SEE WHAT PROBLEM IS
        // CHECK SJF WHY NOT WORKING
        //finalProcessList.addAll(highPriorityQueue);
        //finalProcessList.addAll(mildPriorityQueue);
        //finalProcessList.addAll(lowPriorityQueue);

    }

    /*public void simulate()
    {
        int currentTime=0;
        int remainingProcess=count; 
        while (remainingProcess > 0)
        {
            int min=1000;
            int index=-1;
            Process current = null;

            for (int i = 0; i < count; i++) 
            {
                current = processList.get(i);

                if (current.getRemainingTime() > 0 && current.getBurstTime()<min &&current.getArrivalTime()<=currentTime )
                {   
                    index=i;
                    min=current.getBurstTime();
                }   
            }
            if(index==-1)
            {   currentTime++;
                continue;
            }
            current = processList.get(index);
            if (current.getStartTime()==-1) 
            {
                current.setStartTime(currentTime);
            }

            ganntP[j]=current.getProcessId();
            ganntT[j]=currentTime;
            j++;

            current.setRemainingTime(0);
            current.setEndTime(currentTime +current.getBurstTime());
            currentTime+=current.getBurstTime();
            remainingProcess--;


        }
        for(int i=0;i<count;i++)
        {   
            Process current=processList.get(i);
            current.setWaitingTime(current.getEndTime()-current.getBurstTime()-current.getArrivalTime());
            current.setTurnaroundTime(current.getEndTime()  - current.getArrivalTime());

            totalWaitingTime+=current.getWaitingTime();
            totalTurnAroundTime+=current.getTurnaroundTime();
        }
        avgWatingTime=(float)totalWaitingTime/count;
        avgTurnaroundTime=(float)totalTurnAroundTime/count;

    }*/

    public void printResult()
    {
        Collections.sort(this.processList, Process.BY_PROCESSID);
        System.out.println("Simulation result of TEST ");
        System.out.println("ProcessID | ArrivalTime | BurstTime | Priority | StartTime | EndTime| WaitingTime | TurnAroundTime");
        System.out.println("PId ArrivalT BurstT Priority  StartT   EndT  WaitingT TurnAroundT");
        for(Process p : processList)
        {
            System.out.println(p);  

        }
        System.out.println("Average Waiting Time of Multilevel "+avgWatingTime);
        System.out.println("Average TurnAround Time of Multilevel "+avgTurnaroundTime);

        System.out.println("HIGH PRIORITY");
        System.out.println("PId ArrivalT BurstT Priority  StartT   EndT  WaitingT TurnAroundT");
        for(Process p : highPriorityQueue)
        {
            System.out.println(p);
        }
        System.out.println("MILD PRIORITY");
        System.out.println("PId ArrivalT BurstT Priority  StartT   EndT  WaitingT TurnAroundT");
        for(Process p : mildPriorityQueue)
        {
            System.out.println(p);
        }
        System.out.println("LOW PRIORITY");
        System.out.println("PId ArrivalT BurstT Priority  StartT   EndT  WaitingT TurnAroundT");
        for(Process p : lowPriorityQueue)
        {
            System.out.println(p);
        }
        for(int i=0;i<j;i++)
        {
            System.out.println("Time "+ganntT[i]+" Process "+ganntP[i]);    

        }
        System.out.println();
        System.out.println("LIST COMBINE TEST");
        System.out.println("PId ArrivalT BurstT Priority  StartT   EndT  WaitingT TurnAroundT");
        for(Process p : finalProcessList)
        {
            System.out.println(p);
        }
    }
}

您将看到由于它是针对SJF策略而被注释掉的,因此在这里我需要对其进行更改以使其能够适用于已创建的所有3个队列。

编辑: 在我的简要说明中,我说我需要对多优先级队列使用最短作业优先策略-所以我的目标是将sjf应用于每个队列,然后找到一种避免对低/低优先级饥饿的方法

我的最终目标是:一个为每个优先级安排多个队列的scheudler-每个调度程序首先执行最短的作业,并且是一种避免饿死低优先级/轻度优先级的方法,并且避免了长突发时间过程的漫长等待时间

2 个答案:

答案 0 :(得分:0)

也许您可以从Java中研究PriorityQueue,如果您使用比较器添加/删除值,它会自动排序

答案 1 :(得分:0)

我建议您使用java.util.PriorityQueue并为Comparator类定义Process(或让它实现Comparable)。然后,您只需将所有进程添加到其中,然后队列对其进行排序。