CPU Scheduling computing the average response time and througput in FCFS SJF MultiLevel FB
As discussed in the class, please finish the three functions:
(1) public static void FCFS(List
Complete it by computing the average response time and througput.
(2) public static void SJF_preemptive(List
Complete this function by using shortest job first algorithm in preemptive mode. You need to compute the average waiting time, average response time and throughput and print them out.
– Processes are executed in the correct order (25%)
– Correctly print out the average waiting time, average response time and throughput. (15%)
(3) public static void multiLevel_FB(List
Complete this function by using multi-level feedback algorithm. You need to compute the average waiting time, average response time and throughput and print them out.
– Processes are executed in the correct order (25%)
– Correctly print out the average waiting time, average response time and throughput. (15%)
Submission:
Feel free to modify the Process class as needed. So after you finish the code, you only need to turn in the “CPUscheduling.java” file
package demo;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Process {
int id;
Instant ready; //The time the process enter the ready queue
Instant starting_timing; //The time the process is picked up by CPU
int burst_time; //CPU execution time
Instant finishing_time; //The time that the process is finished by CPU
Process(int i, List
{
id = i;
Instant cur_time = Instant.now();
//1. Let the process wait for a random amount of time and then set the time to the ready time (between 0 and 2000)
Random rd = new Random();
int amount = rd.nextInt(2000) + 1;
Duration d = Duration.ofMillis(amount);
ready = cur_time.plus(d);
//2. Assign a burst_time for the process
burst_time = rd.nextInt(2000) + 1000; //between 1000ms and 3000ms to finish
//3. Add the current process into ready_q
ready_q.add(this);
}
}
public class CPUScheduling {
/*Finish the FCFS by computing the average waiting time, average response time and throughput and print them out*/
//Follow the algorithm of FCFS to start the processes in the ready_q
public static void FCFS(List
{
Instant global_time = Instant.now().plus(Duration.ofMillis(3000)); //Represent the time CPU start scheduling
List
/* Loop finding minimum process and remove it from the ready_q */
//Pick a process to start execution (assign “starting_timing” and “finishing_time”)
// Once a process is removed from the ready_q, you can add them to another list: finishing_q
while(ready_q.size() > 0)
{
Instant big_time = Instant.now().plus(Duration.ofMillis(10000)); //Create a bigger timestamp than all of the processes.ready time in the queue
int idx = -1; //The index that points to the process with minimum ready time
//The following for loop will find the earliest job (with minimum ready_time)
for(int i = 0; i < ready_q.size(); i++)
{
//find the minimum ready_time
if(big_time.compareTo(ready_q.get(i).ready) > 0)
{
idx = i;
big_time = ready_q.get(i).ready;
}
}
System.out.print(“The minimum one is: “);
System.out.println(ready_q.get(idx).ready);
//assign “starting_timing” and “finishing_time”
ready_q.get(idx).starting_timing = global_time;
ready_q.get(idx).finishing_time = ready_q.get(idx).starting_timing.plus(Duration.ofMillis(ready_q.get(idx).burst_time));
global_time = ready_q.get(idx).finishing_time;
//remove it from ready_q and add it to the finishing_q
finishing_q.add(ready_q.get(idx));
ready_q.remove(idx);
}
System.out.println(“Finishing Q: “);
int total_waiting = 0;
for(int i = 0; i < finishing_q.size(); i++)
{
//Print out (1) Average waiting time (2) Average response time (3) Throughput
System.out.print("[" + i + "]: start: ");
System.out.print(finishing_q.get(i).starting_timing);
System.out.print("| end: ");
System.out.println(finishing_q.get(i).finishing_time);
//Accumulate each process's waiting time = starting_time - ready
total_waiting += Duration.between(finishing_q.get(i).ready, finishing_q.get(i).starting_timing).toMillis();
}
//Compute the Average waiting time
double avg_waiting = total_waiting / finishing_q.size();
System.out.println("Total waiting: ” + total_waiting + “, avg waiting: ” + avg_waiting);
}
/*SJF (preemptive): Finish this function by computing the average waiting time, average response time and throughput and print them out*/
public static void SJF_preemptive(List
{
//global_time – represents the time CPU start performing scheduling
Instant global_time; //Assign the global_time by using the minimum ready (time) among all the processes in the ready_q
}
/*SJF (preemptive): Finish this function by computing the average waiting time, average response time and throughput and print them out*/
public static void multiLevel_FB(List
{
//global_time – represents the time CPU start performing scheduling
Instant global_time; //Assign the global_time by using the minimum ready (time) among all the processes in the ready_q
}
public static void main(String args[])
{
List
//Create 10 processes and then add them to the ready_q
for(int i = 0; i < 10; i++)
{
Process p = new Process(i, ready_q);
}
for(int i = 0; i < ready_q.size(); i++)
{
System.out.print("[" + ready_q.get(i).id + "]: ");
System.out.print(ready_q.get(i).ready);
System.out.println(" | burst: " + ready_q.get(i).burst_time);
}
//FCFS
FCFS(ready_q);
//After all the processes are finished, print out the overall wait time.
}
}