优先队列流程调度程序

时间:2019-04-10 13:07:51

标签: java arraylist process operating-system scheduler

我有一个任务来创建一个使用SJF和多级优先级队列的流程调度程序,但是现在我需要改进该策略。我首先创建了最短的工作优先程序。但是现在我需要实现多级优先级队列,因此CSV文件具有一个优先级列,其编号为1,2,3,3 =低,2 =中等,1 =高。我希望能够为每个优先级创建3个单独的队列(我正在使用列表)。然后,从这里开始,我将需要创建一种避免饥饿的方法,并使低优先级的食物不再等待太久。

我创建了三个单独的队列,但是不确定从那里去哪里...

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

public class TEST
{
List<Process> processList;
private int count;
private int totalWaitingTime=0;
private int totalTurnAroundTime=0;
int j=0;
private int ganntP[];
private int ganntT[];
private float avgWatingTime=0;
private float avgTurnaroundTime=0;

// Here I want to be able to create 3 queues (made with list) for each of the priorities 
// So something like if priority = 3 add to low priority queue
List<Process> lowPriorityQueue = new ArrayList<Process>(); // PRIORITY = 3
List<Process> mediumPriorityQueue = new ArrayList<Process>(); // PRIORITY = 2
List<Process> highPriorityQueue = new ArrayList<Process>(); // PRIORITY = 1

TEST(List<Process> processList)
{
    count=processList.size();
    ganntT=new int[200];
    ganntP=new int[200];
    this.processList=new ArrayList<Process>();
    for(Process p : processList)
    {
        this.processList.add(new Process(p.getProcessId(), p.getArrivalTime(), p.getBurstTime(),p.getPriority()));
    }
    Collections.sort(this.processList, Process.BY_ARRIVAL_TIME);
    Collections.sort(this.processList, Process.BY_BURST_TIME);
}

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 SJF ");
    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 SJF "+avgWatingTime);
    System.out.println("Average TurnAround Time of SJF "+avgTurnaroundTime);
    for(int i=0;i<j;i++)
    {
        System.out.println("Time "+ganntT[i]+" Process "+ganntP[i]);    

    }
    System.out.println();
 }
}

它目前可以完美地用作SJF程序,并且您可以看到我已经创建了三个队列,但是接下来我需要做的就是让我感到困惑

0 个答案:

没有答案