Question: Task 2: Developing a Scheduling Algorithm in Java In this task, you are going to develop a simple program in Java that implements a random
Task 2: Developing a Scheduling Algorithm in Java In this task, you are going to develop a simple program in Java that implements a random scheduling algorithm, simulates the execution of processes, and reports the results. See the provided RandomScheduler.java file attached below.
Your specific tasks for this part of the assignment include the following:
- Implement a program in Java that satisfies functional requirements listed below (also see the additional requirements for your code below this list):
- The scheduling algorithm must start at time moment (tick) 0 and work in a loop (with a step of 1 tick) until all of the processes are completed. The total number of processes is limited to a specific number. The processes do not arrive at predetermined time moments, however instead, at each time moment one or several new processes may arrive with a given probability. Maximum number of new processes arriving at a given time movement is also limited with a separate parameter, though.
- The total burst time for each new process is determined randomly (using a uniform distribution) between the given minimum and maximum burst time.
- The next process to be assigned to the CPU is selected by the scheduling algorithm randomly (using a uniform distribution) among the currently available (and yet not completed) processes.
- The scheduling algorithm should support two behaviors: non-preemptiv and preemptive (using the provided time quantum value).
- The data about the total waiting time for each process should be stored. After each simulation is complete, the information about each process (ID, burst time, arrival time, and total waiting time) should be printed. Additionally, the complete execution time of the simulation and the average process waiting time should be printed.
- The implementation should run several simulations, with both non-preemptive and preemptive versions of the scheduling algorithm. See RandomScheduler.java for further details.
- Run the implementation and capture the results in your report (several screenshots + text output for all simulation results).
- Discuss the results in your report, including the following points: Do the simulation results for the non-preemptive and preemptive versions of the scheduling algorithm differ with any observable patterns? Would the observable behavior for non-preemptive vs preemptive versions be different, if the RNG seed was different? What if the number of simulations was increased to, e.g., 10000?
- What are the advantages and disadvantages of such a random scheduling algorithm compared to the First Come First Served (FCFS) algorithm?
PROGRAM TEMPLATE
import java.util.Random;
/* * File: RandomScheduling.java * Course: 20HT - Operating Systems - 1DV512 * Author: *State your name and LNU student ID here! (e.g., ab222cd)* * Date: November 2020 */
// TODO: put this source code file into a new Java package with meaningful name (e.g., dv512.YourStudentID)!
// You can implement additional fields and methods in code below, but // you are not allowed to rename or remove any of it!
// Additionally, please remember that you are not allowed to use any third-party libraries
public class RandomScheduling { public static class ScheduledProcess { int processId; int burstTime; int arrivalMoment; // The total time the process has waited since its arrival int totalWaitingTime; // The total CPU time the process has used so far // (when equal to burstTime -> the process is complete!) int allocatedCpuTime;
public ScheduledProcess(int processId, int burstTime, int arrivalMoment) { this.processId = processId; this.burstTime = burstTime; this.arrivalMoment = arrivalMoment; } // ... add further fields and methods, if necessary } // Random number generator that must be used for the simulation Random rng;
// ... add further fields and methods, if necessary public RandomScheduling(long rngSeed) { this.rng = new Random(rngSeed); } public void reset() { // TODO - remove any information from the previous simulation, if necessary } public void runNewSimulation(final boolean isPreemptive, final int timeQuantum, final int numProcesses, final int minBurstTime, final int maxBurstTime, final int maxArrivalsPerTick, final double probArrival) { reset(); // TODO: // 1. Run a simulation as a loop, with one iteration considered as one unit of time (tick) // 2. The simulation should involve the provided number of processes "numProcesses" // 3. The simulation loop should finish after the all of the processes have completed // 4. On each tick, a new process might arrive with the given probability (chance) // 5. However, the max number of new processes per tick is limited // by the given value "maxArrivalsPerTick" // 6. The burst time of the new process is chosen randomly in the interval // between the values "minBurstTime" and "maxBurstTime" (inclusive)
// 7. When the CPU is idle and no process is running, the scheduler // should pick one of the available processes *at random* and start its execution // 8. If the preemptive version of the scheduling algorithm is invoked, then it should // allow up to "timeQuantum" time units (loop iterations) to the process, // and then preempt the process (pause its execution and return it to the queue) // If necessary, add additional fields (and methods) to this class to // accomodate your solution
// Note: for all of the random number generation purposes in this method, // use "this.rng" ! } public void printResults() { // TODO: // 1. For each process, print its ID, burst time, arrival time, and total waiting time // 2. Afterwards, print the complete execution time of the simulation // and the average process waiting time } public static void main(String args[]) { // TODO: replace the seed value below with your birth date, e.g., "20001001" final long rngSeed = 00000000; // Do not modify the code below instead, complete the implementation // of other methods! RandomScheduling scheduler = new RandomScheduling(rngSeed); final int numSimulations = 5; final int numProcesses = 10; final int minBurstTime = 2; final int maxBurstTime = 10; final int maxArrivalsPerTick = 2; final double probArrival = 0.75; final int timeQuantum = 2;
boolean[] preemptionOptions = {false, true};
for (boolean isPreemptive: preemptionOptions) {
for (int i = 0; i < numSimulations; i++) { System.out.println("Running " + ((isPreemptive) ? "preemptive" : "non-preemptive") + " simulation #" + i);
scheduler.runNewSimulation( isPreemptive, timeQuantum, numProcesses, minBurstTime, maxBurstTime, maxArrivalsPerTick, probArrival);
System.out.println("Simulation results:" + " " + "----------------------"); scheduler.printResults();
System.out.println(" "); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
