Question

Write a Java program that will simulate SRTnext scheduling algorithms. For the algorithm, the program should compute waiting
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please comment below if you find any difficulty in undestanding

Please Upvote, If you are okay with the answer

Answer :

import java.util.List;


public class ShortestRemainingTime extends AllocationStrategy {
  
    public ShortestRemainingTime(List;Job; jobs) {
        super(jobs);
    }
  
    public void run() {
      
    }
  
    public void run(List<Job; jobList) {
      
        float avgTurnArroundTime = 0;
        float avgWaitigTime = 0;
        int n;
        n = jobList.size();
        int p[] = new int[n];
        int at[] = new int[n];
        int bt[] = new int[n];
        int bt2[] = new int[n];
        int wt[] = new int[n];
        int tat[] = new int[n];
        int count = 0;
        double tempWT=0;
        for (Job job:jobList) {
            p[count] = count;
            at[count] = job.getArrivalTime();
            bt[count] = job.getCpuTime();
            bt2[count] = bt[count];
            count++;
        }
        int tbt = 0;
        for (int i = 0; i < n; i++) {
            tbt = tbt + bt[i];
        }
        int time[] = new int[tbt];
        int k = 0;
        int q2 = 0;
      
        System.out.println("============================================ ");
        System.out.println("Process ID | Turnaround time | Waiting time ");
        System.out.println("============================================ ");
        int counter=1;
        for (int i = 0; i < tbt; i++) {
          
            int q = Min(bt, at, tbt, i, n);
          
            if (q != q2) {
                time[k++] = i;
              
                wt[q] = i;
                tat[q] = i + bt[q];
                counter++;
                tempWT+=wt[q];
            }
            avgWaitigTime+= wt[q];
            avgTurnArroundTime+=tat[q];
            bt[q] = bt[q] - 1;
            q2 = q;
          
            System.out.println( (p[q]+1) +"\t|"+tat[q]+"\t|\t"+wt[q]);
            System.out.println("----------------------------------------");
          
        }
        time[k] = tbt;
        System.out.println();
        System.out.print("0\t");
      
        for (int i = 0; i <= k; i++) {
            System.out.print(time[i] + "\t");
        }
        System.out.println("\n============================================ ");
        System.out.println("Avg WT||TAT::"+tempWT/counter+"|"+avgTurnArroundTime/counter);
        System.out.println("============================================ ");
      
    }
  
    public int Min(int b[], int a[], int tbt, int r, int n) {
        int j = 0;
        int min = tbt;
        for (int i = n - 1; i ;= 0; i--) {
            if (b[i] < min && b[i] ; 0 && r ;= a[i]) {
                min = b[i];
                j = i;
            }
        }
        return j;
    }
  
}

AllocationStrategy.java


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;

/* implement this class for all three strategies */

public abstract class AllocationStrategy {
    protected List<Job> Jobs;
    protected ArrayList<Job> Queue;
  
    public AllocationStrategy(List<Job> jobs) {
        super();
        Jobs = jobs;
    }
  
    public abstract void run();
    // update current job by 1 tick
    // check if the job queue might need to be changed.
    // check for jobs to add to the queue
}

Job.java

public class Job {
    private int id, submitTime, CPUTime, CPUTimeLeft;
  
    private int startTime = 0, endTime = 0;
  
  
    public int ProcessCompletionTime;
    public int processArrivalTime;
    public int waitingTime;
    public int turnAroundTime;
    private JobFinishEvent evt;
  
    private int arrivalTime,cpuTime,processId;
  
    public Job(int id, int submitTime, int CPUTime, JobFinishEvent evt) {
        super();
        this.id = id;
        this.submitTime = submitTime;
        this.CPUTime = CPUTime;
        this.CPUTimeLeft = CPUTime;
        this.evt = evt;
    }
  
    public Job(int processId, int arrivalTime, int cpuTime) {
      
        this.processId = processId;
        this.arrivalTime = arrivalTime;
        this.cpuTime = cpuTime;
      
    }
  
    public void start(int sysTime) {
        startTime = sysTime;
    }
  
    public void tick(int sysTime) {
        CPUTimeLeft --;
        if (CPUTimeLeft <= 0){
            endTime = sysTime;
            evt.onFinish(this);
        }
      
    }
  
    public int getId() {
        return id;
    }
  
    public void setId(int id) {
        this.id = id;
    }
  
    public int getSubmitTime() {
        return submitTime;
    }
  
    public void setSubmitTime(int submitTime) {
        this.submitTime = submitTime;
    }
  
    public int getCPUTime() {
        return CPUTime;
    }
  
    public void setCPUTime(int cPUTime) {
        CPUTime = cPUTime;
    }
  
    public int getCPUTimeLeft() {
        return CPUTimeLeft;
    }
  
    public void setCPUTimeLeft(int cPUTimeLeft) {
        CPUTimeLeft = cPUTimeLeft;
    }
  
    public int getStartTime() {
        return startTime;
    }
  
    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }
  
    public int getEndTime() {
        return endTime;
    }
  
    public void setEndTime(int endTime) {
        this.endTime = endTime;
    }
  
    public int getArrivalTime() {
        return arrivalTime;
    }
  
    public void setArrivalTime(int arrivalTime) {
        this.arrivalTime = arrivalTime;
    }
  
    public int getCpuTime() {
        return cpuTime;
    }
  
    public void setCpuTime(int cpuTime) {
        this.cpuTime = cpuTime;
    }
  
    public int getProcessId() {
        return processId;
    }
  
    public void setProcessId(int processId) {
        this.processId = processId;
    }
  
  
}


JobFinishEvent.java


public interface JobFinishEvent {
    public void onFinish(Job j);
}

Question1.java


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Question1 {
  
    public static void main(String[] args) {
        // Process command line arguments
        // read the file
      
      
        Scanner sc = new Scanner(System.in);
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
      
      
        String filename ;
        String allocationStrategy;
        int quantum=20;
      
        /*filename = args[0];
        allocationStrategy = args[1];*/
      
        filename = "testing.txt";
        allocationStrategy = "FCFS";
      
      
        //filename = sc.nextLine();
      
        if(args.length==3){
            quantum = new Integer(args[2]);
        }
      
        BufferedReader br = null;
      
        try {
          
            String sCurrentLine;
          
            br = new BufferedReader(new FileReader("C://Users/Arnav/Desktop/"+filename));
            //System.out.println("processId arrivalTime cpuTime");
          
            List<Job> jobList = new ArrayList<Job>();
            while ((sCurrentLine = br.readLine()) != null) {
              
                String a[] = sCurrentLine.split(",");
                int processId = new Integer(a[0]);
                int arrivalTime = new Integer(a[1]);
                int cpuTime = new Integer(a[2]);
              
                Job job = new Job(processId,arrivalTime,cpuTime);
              
                jobList.add(job);
              
                //System.out.println(processId+" "+ arrivalTime+" " + cpuTime);
            }
          
            if("FCFS".equalsIgnoreCase(allocationStrategy)){
              
                FirstComeFirstServed firstComeFirstServed = new FirstComeFirstServed(jobList);
                firstComeFirstServed.run(jobList);
              
                }else if("SRT".equalsIgnoreCase(allocationStrategy)){
              
                ShortestRemainingTime shortestRemainingTime = new ShortestRemainingTime(jobList);
                shortestRemainingTime.run(jobList);
              
                }else if("RR".equalsIgnoreCase(allocationStrategy)){
              
                RoundRobin roundRobin = new RoundRobin();
                roundRobin.run(jobList,quantum);
              
            }
          
            } catch (IOException e) {
            e.printStackTrace();
            } finally {
            try {
                if (br != null)br.close();
                } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
      
        JobFinishEvent callback = new JobFinishEvent() {
            @Override
            public void onFinish(Job j) {
                // this will be called when a job is finished.
            }
        };
      
      
        /*// example job addition:
        ArrayList jobs = new ArrayList();
        jobs.add(new Job(1, 0, 2, callback));
        jobs.add(new Job(2, 1, 3, callback));
        ShortestRemainingTime srt = new ShortestRemainingTime(jobs);
        srt.run();
        */
    }
  
}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that will simulate SRTnext scheduling algorithms. For the algorithm, the pro...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Java that will simulate FCFS (First Come First Serve) considering context switching...

    Write a program in Java that will simulate FCFS (First Come First Serve) considering context switching is 0. The program should compute waiting time and turnaround time of every job as well as the average waiting time and average turnaround time. Then change the context switching time to 0.4 milliseconds. Arrival time CPU Burst (in milliseconds Process 0) P2 P3 4 100 P6

  • Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:

    Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:Processburst TimePriorityP1103P211P323P414P552For each of the scheduling algorithms, FCFS, Shortest-Job-First (SJF, non-preemptive), Priority (smaller priority number implies higher scheduling priority), and RR (quantum = 1) do the following.Draw a Gantt chart to show how these processes would be scheduled.Give the turnaround time (total time from the first arrival into ready state until CPU-burst is completed) of each process.Give the waiting time (total time spent in the Ready state) of each process.Give...

  • Cpu scheduling

    Consider the following set of processes, with the length of the CPU burst times given in milliseconds:a. Draw four Gantt charts illustrating the execution of the processes using FCFS, Preemptive SJF, a non-preemptive priority, and a RR (quantum=2) scheduling. (30 pts)Note: for the RR consider that the arriving time is 0 for all processesb. What is the average waiting time of each process for of the above scheduling algorithms? (10 pts)P1 8 2 0 P2 5 36P3 1 1 8...

  • 4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1...

    4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1 Priority P2 P3 P4 P5 For each of the following CPU scheduling algorithms, determine the turnaround and average waiting time for each of the process :- a. Shortest remaining Time First b. Shortest Job First C. Priority Scheduling (Both pre-emptive and non-preemptive) d. Round Robin (quantum is 1 ms)

  • QUESTION 1 Consider the following set of processes, with the length of the CPU burst time...

    QUESTION 1 Consider the following set of processes, with the length of the CPU burst time given in milliseconds: Process            Burst Time      Priority P1                    7                      5 P2                    2                      4 P3                    11                    3 P4                    9                      1 P5                    5                      3 The processes are assumed to have arrived in the order P1,P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 2). b. What...

  • Consider the following set of processes, with the length of the CPU burst given in milliseconds:

    Consider the following set of processes, with the length of the CPU burst given in milliseconds:ProcessBurst TimePriorityP1 54P231P312P472P543The processes are assumed to have arrived in the order P1 , P2 , P3 , P4 , P5 , all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a larger priority number implies a higher priority), and RR (quantum = 2). b. What is the turnaround time of each...

  • Compute turnaround time with preemptive priority scheduling. Also compute the average turnaround time. Show the Gantt...

    Compute turnaround time with preemptive priority scheduling. Also compute the average turnaround time. Show the Gantt chart and other details. Process CPU Burst time Arrival time Priority P1 25 5 5 P2 10 10 3 P3 15 15 4 P4 20 20 2 P5 30 25 1

  • Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:...

    Consider the following set of processes, with the length of the CPU-burst time given in milliseconds: Process            Burst Time      Priority P1                    10                    3 P2                    1                     1 P3                    2                     3 P4                    1                     4 P5                    5                     2 The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF (SPN), a...

  • Implement a program for SJF(non-preemptive) scheduling. Given n processes with their burst times and Arrival Time,...

    Implement a program for SJF(non-preemptive) scheduling. Given n processes with their burst times and Arrival Time, the task is to find average waiting time and average turn around time and completion time using SJF scheduling algorithm. Language in C • Completion Time: Time at which process completes its execution. • Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time • Waiting Time: Time Difference between turn around time and...

  • Please answer the following question in C++ language Consider the following set of processes, with the...

    Please answer the following question in C++ language Consider the following set of processes, with the length of the CPU burst time given in milliseconds: Process            Burst Time      Priority P1. 7 5 P2 2 4 P3 11 3 P4 9 1 P5 5 3 The processes are assumed to have arrived in the order P1,P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT