The following program demonstrates how a Round Robin CPU Scheduler works using a csv or txt file as an input. The user will have to input a path leading to the csv file and will also have to provide the time quantum. The csv file will contain information of the processes such as its unique process ID, burst time, and arrival time and the program will extract and read those calues as inputs. The program then executes the algorithm leading to it ouputing certain metrics such as CPU utilization, Throughput, average waiting time, and average turnaround time. It also tells the user the total amount of time it took for all proccesses to be completed for the given time quantum.
- Round Robin CPU Scheduling: The main focus of the program. uses this cpu scheduling algorithm which runs processes at a time slice (time quantum) and keeps doing so until all process remaining time is 0. Helps deal with starvation as all processes run at that givevn time slice.
- Performance Metrics: The output of the program. After the proram is done executing, the console will print out the different information that the user can see on certain categories such as CPU utilization, throughput, average waiting time, average turnaroundtime and number of context switches.
- CSV input: What makes the program run. Using Buffered Readers, the user can input a csv file that includes the process ID, arrival time and burst time in that order and the program will seperate the values from the commas using String split
File: src/Process.java
Description:
The process class represents a single process along with all of its information such as a unique ID. The class maintains its variables private to enhance security and uses accesors and mutators instead
Attributes:
private int processID: The unique process IDprivate int burstTime: Burst time of the processarrivalTime: Arrival time of the process (alsoprivate)remainingTime: Remaining time of the process after time quantum (alsoprivate)private int waitingTime: time process is in the ready queueturnaroundTime: Time taken from arrival to completion (alsoprivate)
Methods:
- Constructor:
public Process(int id, int bt, int at)
The Constructor initializes a new process given its unique ID, a burst time and an arrival time.
- Accessors and Mutators:
getProcessID(),getBurstTime(),getArrivalTime(),getRemainingTime(),getWaitingTime(),getTurnaroundTime()setRemainingTime(int rt),setWaitingTime(int wt),setTurnaroundTime(int tat)
File: src/CPU.java
Description:
The CPU class is supposed to represent a CPU where it executes the processes that are given by using a given time quantum.
Attributes:
private int timeQuantum: Stores the time quantum that is being used
Methods:
- Constructor:
Fully loaded constructor initializes a CPU class with a given time quantum
public CPU(int tq)
- executeProcess:
public void executeProcess(Process p)
Takes a process class as a parameter and will execute if the current process's remaining time is greater than 0. The process will then get executed for the amount of time quantum and the output will depend if the time quantum is greater than the process's remaining time.
File: src/ReadyQueue.java
Description: This class represents the ready queue by using a linked list.
Attributes:
private Queue<Process> queue: Creates a new queue of processes
Methods:
- Constructor:
The constructor is used to initialize a linked list. No parameters needed.
public ReadyQueue()
- add:
Adds a process to the queue
Public void add(Process p)
- poll:
Retrieves the first element currently in the queue
public Process poll()
- isEmpty:
Checks if queue is empty
public boolean isEmpty()
File: src/WaitingQueue.java
Description: This class represents the ready queue by using a linked list.
Attributes:
private Queue<Process> q: Creates a new queue of processes
Methods:
- Constructor:
The constructor is used to initialize a linked list. No parameters needed.
public WaitingQueue()
- add:
Adds a process to the queue
Public void add(Process p)
- poll:
Retrieves the first element currently in the queue
public Process poll()
- isEmpty:
Checks if queue is empty
public boolean isEmpty()
- moveToReady:
public void moveToReady(ReadyQueue rq, int currentTime)
Method is used to move a process from the waiting queue to the ready queue. Executes if the current queue is not empty by calling isEmpty(). The method polls the proccess in the waiting queue and adds to the ready queue if the current time is less than or equal to the current time. If the conditions are not met, it gets added back to the waiting queue.
File: src/Main.java
Description:
The Main class is responsible for the round robin algorithm itself. It contains the many methods of taking the user's input and printing out the performance metrics after the program has been executed. Uses a scanner for the input as well as File readers and Buffer readers to read the csv file that is inputed.
Methods:
- roundRobin:
public static void roundRobin(Process[] processes, int timeQuantum)
The method that is the algorithm itself. instantializes a new ReadyQueue, WaitingQueue, and a CPU. The method tracks the current time, index of the process array, the amount of processes completed as well as the number of context switches. Using many if, if-else, and while statements the method determines if the process is transferred into the ready queue as well as updating the final metrics after each process. The method finishes off by calling printmetrics to finalize the program and print the performance metrics.
- printMetrics:
private static void printMetrics(Process[] pArray, int cp, int cs)
This methods prints out the final performance metrics such as CPU utilization, throughput, etc. The method creates two double variables that track the total waiting and total turnaround time. It updates the value for every process in the array of processes using a for loop. The method also calculates the average waiting and turnaround time as well as the CPU utilization by dividing the total time by the number of completed processes. The method finishes by printing out all the final values rounded to the second decimal place.
- main:
The main method of the program. It takes in two inputs using a scanner that is created in the method. It first asks the user for the file path of the csv file. Once that is given, the method creates an array of processes and uses buffered readers to read the given file and seperate it using
line.split(",")and assigns the values to a process ID, arrival time, and burst time in that order. Then the program asks the user to input a time quantum and finally, it calls theroundRobinmethod with the processes array, and the given time quantum as its parameters.
The program has been executed five times, each run with a different time quantum provided by the user. The file that has been used is the same thorughout each of the five tries. The performance metrics have been captured as screenshots and saved in the RoundRobin Output/ folder in the directory. Each screenshot has been named according to the time quantum that was used during the run. Starts with TimeQuantum_75 and ends with TimeQuantum_300.
After trying and capturing the output of the program on each of the five trials, CPU utilizations tended to peak at around the middle of the bunch, especially whent the time quantum was at 200ms. It hovered around 56% as the rest were at 55%. The program used milliseconds as units of time instead of seconds, possibly impacting some of the results. As for the throughput, it also follows the same pattern as CPU utilization as it also peaks at 200 ms time quantum with a value of 0.64 processes per second. Waiting time and Turnaround time both have a strange pattern where the smallest three time quantums started to decrease the values and once the time quantum was set to 250 ms, the values increased and started to decrease when time quantum increased to 300 ms.
- Youtube Video: Round Robin Algorithm
