Question: Please use Java Part 1: CPU Scheduling Write a program in Java that implements the CPU Scheduling algorithms discussed in Chapter 6 (FCFS, Priority Scheduling,

Please use Java Part 1: CPU Scheduling

Write a program in Java that implements the CPU Scheduling algorithms discussed in Chapter 6 (FCFS, Priority Scheduling, Round Robin).

Classes provided include: Process (complete) and CPUScheduling (not complete), create a client class that will demonstrate the CPU scheduling algorithms discussed in Chapter 6.

The Process class defines a process, this is a comparable class, which will allow you to use the Collections.sort method to sort the Process by priority. This class should not be changed in any way and used as is.

The CPUScheduling class is a shell and provides some methods that could be useful. This is where you will program the scheduling algorithms. The three algorithms that you will program are the FCFS, Priority (use a random generator to generate a random priority 1 5) and Round Robin with quantum 3. You will have to also calculate the waitTime and turnAroundTime for each of the algorithms. There should be no need to define any other class variables, any other variables you should need should be local to the method that they are used.

The client class should implement the CPUScheduling. Use a ArrayList of Processes to store the processes for each scheduling algorithm. Make sure to clear the ArrayList if you plan on using the same variable for each test. Create a new CPUScheduling object for each one as well, since you will want to clear the variables in that class as well.

Included in the zip folder is an example of the output from my program to give you an idea of my expectation for output for Part 1, CPUScheduling class (not complete) and Process class. Provided classes have toString() methods to display the objects. Classes:

PROCESSES.JAVA import java.util.Random; public class Process implements Comparable { private int burstTime; private int number; private int priority; Random gen = new Random(); /* * Randomly sets burstTime inclusively between 3 and 12 * creates process with no priority * @param num - process number */ public Process(int num) { burstTime = gen.nextInt(10)+3; priority = 0; number = num; } /* * Randomly sets burstTime inclusively between 3 and 12 * creates a process with a priority from 1 to 5 * @param num - process number * @param p - priority number */ public Process(int num, int p) { burstTime = gen.nextInt(10)+3; priority = p; number = num; } public int getBurstTime() { return burstTime; } public int getPriority() { return priority; } public int getProcessNum() { return number; } public String toString() { return "Process: " + number + "\tBurst Time: " + burstTime + "\tPriority : " + priority; } public int compareTo(Process p) { if(this.getPriority() < p.getPriority()) return -1; else if(this.getPriority() > p.getPriority()) return 1; return 0; } }

CPUscheduling.java

import java.util.*; public class CPUscheduling { private List arrivals; //stores arrival times private double waitTime; private double turnAroundTime; public CPUscheduling() { arrivals = new ArrayList(); waitTime = 0; turnAroundTime = 0; } /* * Schedules processes with FCFS algorithm * @param processes - list of processes to schedule */ public void fcfsAlgorithm(List processes) { } /* * Schedules processes with Priority algorithm * @param processes - list of processes to schedule */ public void priorityAlgorithm(List processes) { } /* * Schedules processes with Round Robin algorithm with quantum q * @param processes - list of processes to schedule * @param q - quantum */ public void roundRobinAlgorithm(List processes, int q) { } public String toString() { String output = ""; for(Integer n : arrivals) output += n + "\t"; output += " Wait time : " + waitTime + " Turn around time: " + turnAroundTime; return output; } /* * Sums the times in the arrival list * used for waitTime and turnAround calculation * @param start - start index of list * @param end - end index of list */ public int sumTimes(int start, int end) { int sum = 0; for(int i = start; i < end; i++) sum += arrivals.get(i); return sum; } /* * Output the processes */ public void printProcesses(List process) { for(Process p : process) { System.out.printf(" P" + p.getProcessNum() + "\t"); } System.out.println(); } }

SAMPLE OUTPUT:

FCFS scheduling Process: 1 Burst Time: 3 Priority : 0 Process: 2 Burst Time: 6 Priority : 0 Process: 3 Burst Time: 9 Priority : 0 Process: 4 Burst Time: 8 Priority : 0 Process: 5 Burst Time: 10 Priority : 0 Process: 6 Burst Time: 6 Priority : 0 Process: 7 Burst Time: 11 Priority : 0 Process: 8 Burst Time: 9 Priority : 0 Process: 9 Burst Time: 4 Priority : 0 Process: 10 Burst Time: 6 Priority : 0 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 0 3 9 18 26 36 42 53 62 66 72 Wait time : 31.5 Turn around time: 38.7 Priority scheduling Process: 7 Burst Time: 4 Priority : 1 Process: 9 Burst Time: 9 Priority : 1 Process: 10 Burst Time: 5 Priority : 1 Process: 1 Burst Time: 3 Priority : 3 Process: 4 Burst Time: 7 Priority : 3 Process: 2 Burst Time: 7 Priority : 4 Process: 3 Burst Time: 3 Priority : 4 Process: 5 Burst Time: 11 Priority : 5 Process: 6 Burst Time: 12 Priority : 5 Process: 8 Burst Time: 6 Priority : 5 P7 P9 P10 P1 P4 P2 P3 P5 P6 P8 0 4 13 18 21 28 35 38 49 61 67 Wait time : 26.7 Turn around time: 33.4 Round Robin scheduling Process: 1 Burst Time: 6 Priority : 0 Process: 2 Burst Time: 12 Priority : 0 Process: 3 Burst Time: 11 Priority : 0 Process: 4 Burst Time: 7 Priority : 0 Process: 5 Burst Time: 3 Priority : 0 P1 P2 P3 P4 P5 P1 P2 P3 P4 P2 P3 P4 P2 P3 0 3 6 9 12 15 18 21 24 27 30 33 34 37 39 Wait time : 13.2 Turn around time: 21.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!