Question

In: Computer Science

IN JAVA Implement SRT scheduling algorithms based on following actions: The simulation maintains the current time,...

IN JAVA

Implement SRT scheduling algorithms based on following actions:

The simulation maintains the current time, t, which is initialized to 0 and is incremented after each simulation step. Each simulation step then consists of the following actions:

repeat until Rᵢ == 0 for all n processes /* repeat until all processes have terminated */
while no process is active, increment t /* if no process is ready to run, just advance t */
choose active processes pᵢ to run next
according to scheduling algorithm /* Ex: FIFO, SJF, SRT */
decrement Rᵢ /* pᵢ has accumulated 1 CPU time unit */
if Rᵢ == 0 /* process i has terminated */
set active flag of pᵢ = 0 /* process i is excluded from further consideration */
TTᵢ = t - Aᵢ /* the turnaround time of process i is the time
since arrival, TTᵢ, until the current time t */
compute the average turnaround time,
ATT, by averaging all values TTᵢ

Solutions

Expert Solution

import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class SRTF {
private static class Process {
private final int PID;
private final int submitTime;
private int burst;
private int completeTime;
private boolean dispatched;
  
public Process(int PID, int submitTime, int burst) {
this.PID = PID;
this.submitTime = submitTime;
this.burst = burst;
this.completeTime = -1;
this.dispatched = false;
}
  
public int getPID() {
return this.PID;
}
  
public int getSubmissionTime() {
return this.submitTime;
}
  
public int getBurst() {
return this.burst;
}
  
public void reduceBurst(int q) {
burst -= (int) Math.min(burst, q);
}
  
public void setCompleteTime(int time) {
this.completeTime = time;
}
  
public int getCompleteTime() {
return this.completeTime;
}
  
public boolean isCompleted() {
return this.completeTime != -1;
}
  
public void setDispatched() {
this.dispatched = true;
}
  
public boolean isDispatched() {
return this.dispatched;
}
}
  
private static boolean checkCompleted(Process[] parr) {
for(Process p : parr) {
if(!p.isCompleted()) {
return false;
}
}
return true;
}
  
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(System.out);
PrintWriter out = new PrintWriter(osw);
  
Process[] parr = new Process[5];
parr[0] = new Process(0, 1, 3);
parr[1] = new Process(1, 2, 1);
parr[2] = new Process(2, 2, 5);
parr[3] = new Process(3, 3, 4);
parr[4] = new Process(4, 4, 2);
  
int clock = 1;
int quantum = 1;
  
while(!checkCompleted(parr)) {
// Detect shortest burst process which
// has not completed
Process minBurstProc = null;
int minBurst = Integer.MAX_VALUE;
  
for(Process p : parr) {
if(!p.isCompleted() && p.getSubmissionTime() <= clock && p.getBurst() < minBurst) {
minBurst = p.getBurst();
minBurstProc = p;
}
}
  
clock += quantum;
  
if(minBurstProc != null) {
minBurstProc.reduceBurst(quantum);
  
if(minBurstProc.getBurst() == 0) {
minBurstProc.setCompleteTime(clock);
}
}
}
  
out.println("PID\tCompletion Time");
  
for(Process p : parr) {
out.print(p.getPID());
out.print("\t");
out.print(p.getCompleteTime());
out.println(" units");
}
  
out.close();
}
}


Related Solutions

IN JAVA Implement SJF scheduling algorithms based on following actions: The simulation maintains the current time,...
IN JAVA Implement SJF scheduling algorithms based on following actions: The simulation maintains the current time, t, which is initialized to 0 and is incremented after each simulation step. Each simulation step then consists of the following actions: repeat until Rᵢ == 0 for all n processes /* repeat until all processes have terminated */ while no process is active, increment t /* if no process is ready to run, just advance t */ choose active processes pᵢ to run...
(Python or C++) We are going to implement the following scheduling algorithms that we discussed in...
(Python or C++) We are going to implement the following scheduling algorithms that we discussed in class: 1. First-Come First-Served (FCFS) 2. Shortest Remaining Time First (SRTF) 3. Highest Response Ratio Next (HRRN) 4. Round Robin, with different quantum values (RR) We are interested to compute the following metrics, for each experiment: _ The average turnaround time _ The total throughput (number of processes done per unit time) _ The CPU utilization _ The average number of processes in the...
in C++ We are going to implement the following scheduling algorithms 1. First-Come First-Served (FCFS) 2....
in C++ We are going to implement the following scheduling algorithms 1. First-Come First-Served (FCFS) 2. Shortest Remaining Time First (SRTF) 3. Highest Response Ratio Next (HRRN) 4. Round Robin, with di_erent quantum values (RR) We are interested to compute the following metrics, for each experiment: _ The average turnaround time _ The total throughput (number of processes done per unit time) _ The CPU utilization _ The average number of processes in the ready queue The simulator needs to...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
Provide and implement three completely different algorithms of different running time that will check if two...
Provide and implement three completely different algorithms of different running time that will check if two strings are anagrams.
Draw six Gantt charts that illustrate the execution of these processes using the following scheduling algorithms:
Draw six Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: 
1- Which of the following scheduling algorithms could result in starvation? Why? If any, show how...
1- Which of the following scheduling algorithms could result in starvation? Why? If any, show how can starvation problem be resolved.a. First-come, first-served (FCFS)b. Shortest job first (SJF)c. Round robin (RR)d. Priority?2- Illustrate Peterson solution to critical section problem, showing how it satisfy the conditions of mutual exclusion, progress, and bounded waiting!3- What is the meaning of the term busy waiting? What other kinds of waiting are there in an operating system? Can busy waiting be avoided altogether? Explain your...
Tasks 2 Write algorithms in pseudocode to implement a LinkedList The following operations must be performed...
Tasks 2 Write algorithms in pseudocode to implement a LinkedList The following operations must be performed on the LinkedList: *Add an element *Insert an element at a given position x. *Retrieve/Read the value of an element at position y. *Delete an element from position z. (x,y and z represent any value in the valid range of indices of the LinkedList).
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT