多个队列的最短作业优先级

问题描述 投票:-1回答:2

目前,我有一个程序,它模拟一组进程的最短作业优先策略。但是,我的任务是使其成为多优先级队列。因此,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);
        }
    }
}

正如你所看到的那样,simulate方法被注释掉了,因为它适用于SJF策略,这里我需要更改它以便能够为我创建的所有3个队列工作。

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

我的最终目标是:一个计划器,每个优先级都有多个队列 - 每个队列首先执行最短的工作,以及避免饥饿到低/轻度优先级的方法,并避免长时间突发时间进程的漫长等待时间

java eclipse process operating-system scheduling
2个回答
0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.