Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- public class Main{
- public static boolean isEveryProcessExecuted(ArrayList<Process> proc) {
- for(Process p : proc) {
- if(p.getBurstTime() != 0) return false;
- }
- return true;
- }
- // Rounds the number
- public static double round(double number, int precision) {
- return (double) Math.round(number * Math.pow(10d, precision)) / Math.pow(10d, precision);
- }
- public static void printTimes(ArrayList<Process> proc) {
- for(Process p : proc) {
- System.out.print(p.getBurstTime() + " ");
- }
- System.out.println();
- for(Process p : proc) {
- System.out.print(p.getWaitingTime() + " ");
- }
- System.out.println();
- }
- public static void generateProcesses(ArrayList<Process> proc, ArrayList<Process> proc1, ArrayList<Process> proc2, ArrayList<Process> proc3)
- {
- for (int i=0; i<100; i++)
- {
- int random1 = (int)(Math.random() * 10 + 1);
- int random2 = (int)(Math.random() * 10);
- proc.add(new Process(random1,random2));
- proc1.add(new Process(random1,random2));
- proc2.add(new Process(random1,random2));
- proc3.add(new Process(random1,random2));
- }
- Collections.sort(proc);
- Collections.sort(proc1);
- Collections.sort(proc2);
- Collections.sort(proc3);
- }
- public static void sort(ArrayList<Process> proc, int globalTime)
- {
- int n = proc.size();
- int k;
- for (int m = n; m >= 0; m--)
- {
- for (int i = 0; i < n - 1; i++)
- {
- k = i + 1;
- if (proc.get(i).getBurstTime() > proc.get(k).getBurstTime())
- {
- swap(i, k, proc);
- }
- }
- }
- for(int i = proc.size()-1; i >= 0; i--)
- {
- if(proc.get(i).getArrivalTime() > globalTime && proc.get(i).getBurstTime() != 0)
- {
- Process tmp = proc.get(i);
- proc.remove(i);
- proc.add(tmp);
- }
- }
- for(int i = proc.size()-1; i >= 0; i--)
- {
- if(proc.get(i).getBurstTime() == 0)
- {
- Process tmp = proc.get(i);
- proc.remove(i);
- proc.add(tmp);
- }
- }
- }
- public static void swap(int index1, int index2, ArrayList<Process> proc)
- {
- Process tmp = proc.get(index1);
- proc.set(index1, proc.get(index2));
- proc.set(index2, tmp);
- }
- // Algorithm FCFS
- public static void algFCFS(ArrayList<Process> proc)
- {
- int globalTime = 0; // timeline
- double waitingTimeSum = 0;
- int longestWaitingTime = 0;
- int current = 0; // index of process currently in execution
- //System.out.println("Global time | Remaining times | Waiting times"); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Iterate through the queue until every process is fully executed
- while(!isEveryProcessExecuted(proc))
- {
- // Iterate through every process
- for(int i = 0; i < proc.size(); i++)
- {
- // Consider only processes that have already arrived
- if(globalTime >= proc.get(i).getArrivalTime())
- {
- // Check if the process is currently in execution
- if(i == current)
- {
- // Check if the process was waiting
- if(proc.get(i).getWaitingTime() != 0)
- {
- proc.get(i).setStarted(); // In FCFS algorithm the process will be fully executed after it starts, so there doesn't have to be any check if the process has been partly executed before
- waitingTimeSum += proc.get(i).getWaitingTime();
- longestWaitingTime = proc.get(i).getWaitingTime(); // Because it's FCFS algorithm, every process will have longer waiting time than the previous one (except for the first)
- proc.get(i).resetWaitingTime();
- }
- // Execute the process for 1 TU
- proc.get(i).execute(1);
- // If the process isn't currently in execution, then add 1 TU to its waiting time
- }
- else if(proc.get(i).getBurstTime() != 0)
- { // but only if it hasn't been fully executed yet
- proc.get(i).addToWaitingTime(1);
- }
- }
- }
- // If the currently executed process has been fully executed in current time unit, next process in queue is set to execution
- if(proc.get(current).getBurstTime() == 0)
- {
- current++;
- }
- // Move timeline by 1 TU
- globalTime++;
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- }
- System.out.println("Algorithm FCFS");
- System.out.println("Total execution time: " + globalTime + " TU");
- System.out.println("Average waiting time: " + round(waitingTimeSum / proc.size(), 2) + " TU");
- System.out.println("Longest waiting time: " + longestWaitingTime + " TU");
- }
- // Algorithm SJF (non-preemptive)
- // sortTime - amount of time units needed to sort the queue
- public static void algSJF(ArrayList<Process> proc, int sortTime) {
- int globalTime = 0; // timeline
- double waitingTimeSum = 0;
- int longestWaitingTime = 0;
- int current = 0; // index of process currently in execution
- int earliestArrivalTime = 0; // variable needed to check if the queue needs sorting
- //System.out.println("Global time | Remaining times | Waiting times"); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Initial sorting of processes
- sort(proc, globalTime);
- // Adding sortTime to waitingTime of every non-executed process
- for(Process p : proc) {
- if(p.getBurstTime() != 0 && p.getArrivalTime() <= globalTime) {
- p.addToWaitingTime(sortTime);
- }
- }
- // Moving timeline by sortTime
- globalTime += sortTime;
- //System.out.println("Sorting..."); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Iterate through the queue until every process is fully executed
- while(!isEveryProcessExecuted(proc))
- {
- // Iterate through every process
- for(int i = 0; i < proc.size(); i++)
- {
- // Consider only processes that have already arrived
- if(globalTime >= proc.get(i).getArrivalTime())
- {
- // Check if the process is currently in execution
- if(i == current)
- {
- // Check if the process was waiting
- if(proc.get(i).getWaitingTime() != 0)
- {
- proc.get(i).setStarted(); // In non-preemptive SJF algorithm the process will be fully executed after it starts, so there doesn't have to be any check if the process has been partly executed before
- waitingTimeSum += proc.get(i).getWaitingTime();
- // Check if process's waiting time was the longest
- if(proc.get(i).getWaitingTime() > longestWaitingTime)
- {
- longestWaitingTime = proc.get(i).getWaitingTime();
- }
- proc.get(i).resetWaitingTime();
- }
- // Execute the process for 1 TU
- proc.get(i).execute(1);
- // If the process isn't currently in execution, then add 1 TU to its waiting time
- }
- else if(proc.get(i).getBurstTime() != 0)
- { // but only if it hasn't been fully executed yet
- proc.get(i).addToWaitingTime(1);
- }
- }
- }
- // Moving timeline by 1 TU
- globalTime++;
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // If the currently executed process has been fully executed in current time unit, determine next process to be set to execution
- if(proc.get(current).getBurstTime() == 0)
- {
- boolean sorted = false; // variable needed for later
- // Iterate through every process
- for(Process p : proc)
- {
- // Check if 1. process has already arrived, 2. process isn't fully executed, 3. the youngest process is older than the process currently being iterated through
- if(p.getArrivalTime() <= globalTime && p.getBurstTime() != 0 && p.getArrivalTime() > earliestArrivalTime)
- {
- // If such a process has been found, the queue needs to be sorted
- sort(proc, globalTime);
- // Adding sortTime to waitingTime of every non-executed process
- for(Process r : proc)
- {
- if(r.getBurstTime() != 0 && r.getArrivalTime() <= globalTime)
- {
- r.addToWaitingTime(sortTime);
- }
- }
- // Moving timeline by sortTime
- globalTime += sortTime;
- earliestArrivalTime = globalTime;
- current = 0; // Because the queue is sorted by remaining time ascending, next process to execute is the first one in the queue
- sorted = true;
- //System.out.println("Sorting..."); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Since the queue has been sorted, there is no need to iterate through rest of the queue
- break;
- }
- }
- // If no new processes arrived, move to the next process in queue
- if(!sorted)
- {
- current++;
- }
- }
- }
- System.out.println("Algorithm SJF (non-preemptive)");
- System.out.println("Total execution time: " + globalTime + " TU");
- System.out.println("Average waiting time: " + round(waitingTimeSum / proc.size(), 2) + " TU");
- System.out.println("Longest waiting time: " + longestWaitingTime + " TU");
- }
- // Algorithm SJF (preemptive)
- // sortTime - amount of time units needed to sort the queue
- public static void algPSJF(ArrayList<Process> proc, int sortTime) {
- int globalTime = 0; // timeline
- double waitingTimeSum = 0; // sum of every waiting time in the queue
- int longestWaitingTime = 0; // longest waiting time in general
- int executeStarts = 0; // variable counting the amount of changes of process executing
- int current = 0; // index of process currently in execution
- int earliestArrivalTime = 0; // variable needed to check if the queue needs sorting
- //System.out.println("Global time | Remaining times | Waiting times"); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Initial sorting of processes
- sort(proc, globalTime);
- // Adding sortTime to waitingTime of every non-executed process
- for(Process p : proc) {
- if(p.getBurstTime() != 0 && p.getArrivalTime() <= globalTime) {
- p.addToWaitingTime(sortTime);
- }
- }
- // Moving timeline by sortTime
- globalTime += sortTime;
- //System.out.println("Sorting..."); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Iterate through the queue until every process is fully executed
- while(!isEveryProcessExecuted(proc))
- {
- // Iterate through every process
- for(int i = 0; i < proc.size(); i++)
- {
- // Consider only processes that have already arrived
- if(globalTime >= proc.get(i).getArrivalTime())
- {
- // Check if the process is currently in execution
- if(i == current)
- {
- // Check if the process was waiting
- if(proc.get(i).getWaitingTime() != 0)
- {
- // Check if the process will start being executed in general
- if(!proc.get(i).wasStarted())
- {
- proc.get(i).setStarted();
- // Check if process's waiting-for-start time was the longest
- }
- waitingTimeSum += proc.get(i).getWaitingTime();
- // Check if process's waiting time was the longest
- if(proc.get(i).getWaitingTime() > longestWaitingTime)
- {
- longestWaitingTime = proc.get(i).getWaitingTime();
- }
- proc.get(i).resetWaitingTime();
- executeStarts++;
- }
- // Execute the process for 1 TU
- proc.get(i).execute(1);
- // If the process isn't currently in execution, then add 1 TU to its waiting time
- }
- else if(proc.get(i).getBurstTime() != 0)
- { // but only if it hasn't been fully executed yet
- proc.get(i).addToWaitingTime(1);
- }
- }
- }
- // Moving timeline by 1 TU
- globalTime++;
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // In preemptive SJF, the queue has to be checked if it needs sorting in every time unit
- boolean sorted = false; // variable needed for later
- // Iterate through every process
- for(Process p : proc)
- {
- // Check if 1. process has already arrived, 2. process isn't fully executed, 3. the youngest process is older than the process currently being iterated through
- if(p.getArrivalTime() <= globalTime && p.getBurstTime() != 0 && p.getArrivalTime() > earliestArrivalTime)
- {
- // If such a process has been found, the queue needs to be sorted
- sort(proc, globalTime);
- // Adding sortTime to waitingTime of every non-executed process
- for(Process r : proc)
- {
- if(r.getBurstTime() != 0 && r.getArrivalTime() <= globalTime)
- {
- r.addToWaitingTime(sortTime);
- }
- }
- // Moving timeline by sortTime
- earliestArrivalTime = globalTime;
- current = 0; // Because the queue is sorted by remaining time ascending, next process to execute is the first one in the queue
- sorted = true;
- //System.out.println("Sorting..."); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // Since the queue has been sorted, there is no need to iterate through rest of the queue
- break;
- }
- }
- // If no new processes arrived and current process has been fully executed, move to the next process in queue
- if(!sorted && proc.get(current).getBurstTime() == 0)
- {
- current++;
- }
- }
- System.out.println("Algorithm SJF (preemptive)");
- System.out.println("Total execution time: " + globalTime + " TU");
- System.out.println("Average waiting time: " + round(waitingTimeSum / executeStarts, 2) + " TU");
- System.out.println("Longest waiting time: " + longestWaitingTime + " TU");
- }
- // Algorithm ROT
- // quant - length of the quant of time, which is the amount of time given to every process for execution in every queue iteration
- public static void algROT(ArrayList<Process> proc, int quant) {
- int globalTime = 0; // timeline
- double waitingTimeSum = 0; // sum of every waiting time in the queue
- int longestWaitingTime = 0; // longest waiting time in general
- int executeStarts = 0; // variable counting the amount of changes of process executing
- int current = 0; // index of process currently in execution
- int timeToAdd = 0; // variable stating how much time passed executing current process, since remaining time of the process can be less than the quant of time
- //System.out.println("Global time | Remaining times | Waiting times"); // Debug
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- // printTimes(proc); // Debug
- // Iterate through the queue until every process is fully executed
- while(!isEveryProcessExecuted(proc)) {
- // Iterate through every process
- for(int i = 0; i < proc.size(); i++) {
- // Consider only processes that have already arrived
- if(globalTime >= proc.get(i).getArrivalTime()) {
- // Check if the process is currently in execution
- if(i == current) {
- // Check if the process was waiting
- if(proc.get(i).getWaitingTime() != 0) {
- // Check if the process will start being executed in general
- if(!proc.get(i).wasStarted()) {
- proc.get(i).setStarted();
- }
- waitingTimeSum += proc.get(i).getWaitingTime();
- // Check if process's waiting time was the longest
- if(proc.get(i).getWaitingTime() > longestWaitingTime) {
- longestWaitingTime = proc.get(i).getWaitingTime();
- }
- proc.get(i).resetWaitingTime();
- executeStarts++;
- }
- // Execute the process for the quant of time
- timeToAdd = proc.get(i).execute(quant); // Since execute() method returns time of process execution, its value is assigned to variable timeToAdd
- // Since the process executed may not be first in the queue,
- // and time of process execution has to be known in advance before adding it to waitingTime of every process,
- // a separate for loop will iterate through every process to modify waitingTimes.
- // Therefore, current loop has to be broken
- break;
- }
- }
- }
- // Check if current process has been executed for at least 1 TU
- if(timeToAdd != 0) {
- // Adding timeToAdd to waitingTime of every process, except for the one which was being executed
- for(int i = 0; i < proc.size(); i++) {
- // Consider only processes which aren't yet fully executed and have already arrived
- if(i != current && proc.get(i).getBurstTime() != 0 && proc.get(i).getArrivalTime() <= globalTime) {
- proc.get(i).addToWaitingTime(timeToAdd);
- }
- }
- // Moving timeline by timeToAdd
- globalTime += timeToAdd;
- timeToAdd = 0;
- // If the process hasn't been executed, this means that no processes are currently in the queue,
- // therefore no waitingTimes will be increased, and instead timeline has to be moved by 1 TU
- } else {
- globalTime++;
- }
- //System.out.print(globalTime + " TU\t|\t"); // Debug
- //printTimes(proc); // Debug
- // If every process has been fully executed, finish the algorithm
- // (without this line, the while loop a few lines lower will be infinite when every process has been fully executed)
- if(isEveryProcessExecuted(proc)) break;
- int tmp = current;
- // Move to the next process in the queue
- current++;
- // Check if index of current process isn't outside the queue
- if(current >= proc.size()) {
- // If it is, move to the beginning of the queue
- current = 0;
- }
- // This loop cycles through the queue to assure correctness of the algorithm
- while((proc.get(current).getBurstTime() == 0 || proc.get(current).getArrivalTime() > globalTime) && current != tmp) {
- current = (current + 1) % proc.size();
- }
- }
- System.out.println("Algorithm ROT");
- System.out.println("Total execution time: " + globalTime + " TU");
- System.out.println("Average waiting time: " + round(waitingTimeSum / executeStarts, 2) + " TU");
- System.out.println("Longest waiting time: " + longestWaitingTime + " TU");
- }
- public static void main(String[] args) {
- ArrayList<Process> proc = new ArrayList<Process>();
- ArrayList<Process> proc1 = new ArrayList<Process>();
- ArrayList<Process> proc2 = new ArrayList<Process>();
- ArrayList<Process> proc3 = new ArrayList<Process>();
- generateProcesses(proc, proc1, proc2, proc3);
- algFCFS(proc);
- System.out.println();
- algSJF(proc1,0);
- System.out.println();
- algPSJF(proc2,0);
- System.out.println();
- algROT(proc3,2);
- /*
- ArrayList<Process> proc = new ArrayList<Process>();
- proc.add(new Process(4,0));
- proc.add(new Process(2,0));
- proc.add(new Process(11,0));
- proc.add(new Process(3,0));
- proc.add(new Process(8,0));
- proc.add(new Process(2,11));
- proc.add(new Process(5,11));
- proc.add(new Process(3,11));
- proc.add(new Process(15,27));
- proc.add(new Process(2,30));
- ArrayList<Process> proc1 = new ArrayList<Process>();
- proc1.add(new Process(4,0));
- proc1.add(new Process(2,0));
- proc1.add(new Process(11,0));
- proc1.add(new Process(3,0));
- proc1.add(new Process(8,0));
- proc1.add(new Process(2,11));
- proc1.add(new Process(5,11));
- proc1.add(new Process(3,11));
- proc1.add(new Process(15,27));
- proc1.add(new Process(2,30));
- ArrayList<Process> proc2 = new ArrayList<Process>();
- proc2.add(new Process(4,0));
- proc2.add(new Process(2,0));
- proc2.add(new Process(11,0));
- proc2.add(new Process(3,0));
- proc2.add(new Process(8,0));
- proc2.add(new Process(2,11));
- proc2.add(new Process(5,11));
- proc2.add(new Process(3,11));
- proc2.add(new Process(15,27));
- proc2.add(new Process(2,30));
- ArrayList<Process> proc3 = new ArrayList<Process>();
- proc3.add(new Process(4,0));
- proc3.add(new Process(2,0));
- proc3.add(new Process(11,0));
- proc3.add(new Process(3,0));
- proc3.add(new Process(8,0));
- proc3.add(new Process(2,11));
- proc3.add(new Process(5,11));
- proc3.add(new Process(3,11));
- proc3.add(new Process(15,27));
- proc3.add(new Process(2,30));
- printTimes(proc);
- algFCFS(proc);
- System.out.println();
- algSJF(proc1,0);
- System.out.println();
- algPSJF(proc2,0);
- System.out.println();
- algROT(proc3,2);
- */
- /*
- System.out.println(proc.get(0).getArrivalTime());
- System.out.println(proc.get(1).getArrivalTime());
- System.out.println(proc1.get(0).getArrivalTime());
- System.out.println(proc1.get(1).getArrivalTime());
- //System.out.println(proc.get(2).getArrivalTime());
- //System.out.println(proc.get(3).getArrivalTime());
- //System.out.println(proc.get(4).getArrivalTime());
- System.out.println(proc.get(0).getBurstTime());
- System.out.println(proc.get(1).getBurstTime());
- System.out.println(proc1.get(0).getBurstTime());
- System.out.println(proc1.get(1).getBurstTime());
- //System.out.println(proc.get(2).getBurstTime());
- //System.out.println(proc.get(3).getBurstTime());
- //System.out.println(proc.get(4).getBurstTime());
- */
- /* ArrayList<Process> proc1 = new ArrayList<Process>();
- proc1.add(new Process(5, 0));
- proc1.add(new Process(2, 0));
- proc1.add(new Process(7, 0));
- proc1.add(new Process(3, 0));
- proc1.add(new Process(10, 0));
- proc1.add(new Process(6, 11));
- proc1.add(new Process(5, 11));
- proc1.add(new Process(8, 11));
- proc1.add(new Process(4, 27));
- proc1.add(new Process(4, 27));
- ArrayList<Process> proc2 = new ArrayList<Process>();
- proc2.add(new Process(6, 11));
- proc2.add(new Process(4, 27));
- proc2.add(new Process(5, 0));
- proc2.add(new Process(2, 0));
- proc2.add(new Process(7, 0));
- proc2.add(new Process(5, 11));
- proc2.add(new Process(3, 0));
- proc2.add(new Process(8, 11));
- proc2.add(new Process(10, 0));
- proc2.add(new Process(4, 27));
- ArrayList<Process> proc3 = new ArrayList<Process>();
- proc3.add(new Process(6, 11));
- proc3.add(new Process(4, 27));
- proc3.add(new Process(5, 0));
- proc3.add(new Process(2, 0));
- proc3.add(new Process(7, 0));
- proc3.add(new Process(5, 11));
- proc3.add(new Process(3, 0));
- proc3.add(new Process(8, 11));
- proc3.add(new Process(10, 0));
- proc3.add(new Process(4, 27));
- ArrayList<Process> proc4 = new ArrayList<Process>();
- proc4.add(new Process(5, 0));
- proc4.add(new Process(2, 0));
- proc4.add(new Process(7, 0));
- proc4.add(new Process(3, 0));
- proc4.add(new Process(10, 0));
- proc4.add(new Process(6, 11));
- proc4.add(new Process(5, 11));
- proc4.add(new Process(8, 11));
- proc4.add(new Process(4, 27));
- proc4.add(new Process(4, 27));
- algFCFS(proc1);
- System.out.println();
- algSJF(proc2, 0);
- System.out.println();
- algPSJF(proc3, 0);
- System.out.println();
- algROT(proc4, 2);
- */
- /*
- for (int i=0; i<proc.size(); i++)
- {
- System.out.println(proc.get(i).getArrivalTime());
- }
- System.out.println();
- for (int i=0; i<proc.size(); i++)
- {
- System.out.println(proc.get(i).getBurstTime());
- }
- System.out.println();
- for (int i=0; i<proc1.size(); i++)
- {
- System.out.println(proc1.get(i).getArrivalTime());
- System.out.println(proc1.get(i).getBurstTime());
- }
- System.out.println();
- for (int i=0; i<proc2.size(); i++)
- {
- System.out.println(proc2.get(i).getArrivalTime());
- System.out.println(proc2.get(i).getBurstTime());
- }
- System.out.println();
- for (int i=0; i<proc3.size(); i++)
- {
- System.out.println(proc3.get(i).getArrivalTime());
- System.out.println(proc3.get(i).getBurstTime());
- }
- algFCFS(proc);
- System.out.println();
- algSJF(proc1,0);
- System.out.println();
- algPSJF(proc2,0);
- System.out.println();
- algROT(proc3,2);
- */
- /*
- ArrayList<Process> proc = new ArrayList<Process>();
- proc.add(new Process(9,8));
- proc.add(new Process(8,16));
- proc.add(new Process(10,11));
- proc.add(new Process(3,2));
- proc.add(new Process(7,11));
- ArrayList<Process> proc1 = new ArrayList<Process>();
- proc1.add(new Process(9,8));
- proc1.add(new Process(8,16));
- proc1.add(new Process(10,11));
- proc1.add(new Process(3,2));
- proc1.add(new Process(7,11));
- ArrayList<Process> proc2 = new ArrayList<Process>();
- proc2.add(new Process(9,8));
- proc2.add(new Process(8,16));
- proc2.add(new Process(10,11));
- proc2.add(new Process(3,2));
- proc2.add(new Process(7,11));
- ArrayList<Process> proc3 = new ArrayList<Process>();
- proc3.add(new Process(9,8));
- proc3.add(new Process(8,16));
- proc3.add(new Process(10,11));
- proc3.add(new Process(3,2));
- proc3.add(new Process(7,11));
- algFCFS(proc);
- System.out.println();
- algSJF(proc1,0);
- System.out.println();
- algPSJF(proc2,0);
- System.out.println();
- algROT(proc3,2);
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement