Quantcast
Channel: Active questions tagged operating-system+scheduling - Stack Overflow
Viewing all articles
Browse latest Browse all 211

Shortest Job First for Multiple Queues

$
0
0

Currently, I have a program which simulates a shortest job first strategy for a set of processes. However, my task is to make it for multi-priority level queues. So a queue for HIGH, MILD, LOW: HIGH = 1, MILD = 2, LOW = 3. The original code works fine and now I am stuck with trying to implement it for the 3 separate process queues. I have managed to add the correct processes into each queue but there's as far as I got to. The problem is, how do I apply the SJF strategy to each queue

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);        }    }}

As you'll see the simulate method is commented out due to it being for the SJF strategy, here is where I need to change it to be able to work for all 3 queues I've created.

EDIT:In my brief it states I need to use shortest job first strategy for multi priority level queues - so my goal is to apply sjf to each queue, then find a way to avoid starvation to low/mild priorities

My final goal being: A scheudler which has multiple queues for each priority - each implementing shortest job first and a way to avoid starvation to low/mild priorities and avoid long wait time for long burst time processes


Viewing all articles
Browse latest Browse all 211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>