【问题标题】:Shortest Job First for Multiple Queues多个队列的最短作业优先
【发布时间】:2019-09-03 04:23:40
【问题描述】:

目前,我有一个程序可以模拟一组流程的最短作业优先策略。但是,我的任务是使其用于多优先级队列。因此,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 应用于每个队列,然后找到一种方法来避免对低/中等优先级的饥饿

我的最终目标是:每个优先级都有多个队列的调度程序 - 每个优先级都首先执行最短的作业,以及避免对低/中等优先级的饥饿并避免长突发时间进程的长时间等待

【问题讨论】:

  • 你可能想解释你已经做了什么,你想“准确地”做什么,什么是行不通的。了解如何创建minimal reproducible example。顺便说一句,我不会使用更多列表,只需使用带有 Comparator 的排序列表,使用优先级作为将流程放在正确位置的因素。
  • @AxelH 在简报中它声明了一个“多优先级队列”,现在网上关于此的内容不多,所以我最好的猜测是每个优先级的队列。我也有一个完全基于优先级排序的版本,但仅针对整个进程列表
  • 所以你有一个要求,但没有确切的描述。所以它可能是一个简单的基于 FIFO 但有 3 个不同的队列,首先需要清空 HIGH,然后是 MEDIUM 和 LOW。
  • 几乎所有的声明都是“使用多优先级队列策略来调度进程,并使用最短作业优先策略来调度每个队列内的进程。”

标签: java eclipse process operating-system scheduling


【解决方案1】:

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

【讨论】:

  • 简要说明我应该使用多优先级队列 - 假设每个优先级有 3 个队列我的目标是处理所有 HIGH,然后如果 HIGH 则为空进程 MILD 等
  • 队列的比较器可以处理多个优先级。
【解决方案2】:

也许从 java 中查看 PriorityQueue,如果您使用比较器添加/删除值,它会自行排序

【讨论】:

  • 是的,我打算调查它,但担心它可能会影响整个代码等,目前它只有一个完整的进程列表时工作正常,但现在我已经将它变成了 3 个列表,我被卡住了
  • 您可以一次为所有进程使用一个PriorityQueue,有一个线程安全版本,只需编写一个比较器来正确排序任务,如果线程处于空闲状态,则轮询第一个项目或者如果队列为空,等待
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多