/** Driver.java - main function to drive our simulation. * Create some tasks and see how long they take to be served by the * scheduler. */ import java.util.ArrayList; import java.util.Collections; public class Driver { public static void main(String [] args) { ArrayList list = new ArrayList(); list.add(new Process("A", 0, 8)); list.add(new Process("B", 1, 4)); list.add(new Process("C", 2, 9)); list.add(new Process("D", 3, 5)); list.add(new Process("E", 50, 10)); // Fifo scheduling. // The first thing we should do is sort the processes by arrival time. // // For each process that is sorted by arrival time, execute it. // However, we also need to be sure that time has advanced far enough // because if the job hasn't yet arrived, CPU should be idle. // First job begins when it arrives [or 0 whichever is later!]. // Second job begins when it arrives, or when we are done with the first // task, whichever is later. // 3rd, 4th... just like 2nd. System.out.println("Let's try FIFO scheduling."); Collections.sort(list, new ProcessArrivalComparator()); double responseTime = 0.0; /////////////////////////////////////////////////////// // INSERT CODE HERE /////////////////////////////////////////////////////// // OUTPUT... System.out.println("Average response time = " + responseTime); // Print out all the process data. for (int i = 0; i < list.size(); ++i) System.out.println(list.get(i)); // ==================================================================== // Next, let's do "shortest job next." // This time we sort by exec time not arrival time. // But, we may have the problem that the "shortest" job has not yet // arrived! For example if process B doesn't arrive until time 9. // Then at time 8 we decide to go with process C. // // Loop: on each iteration we're obligated to execute one process. // Select the shortest job **that has arrived**. If none has arrived, // then advance time until it's the time of the earliest arrival! // Also disregard processes that are finished! // We can signify "done" by setting the time left to 0. // // If we didn't find any job that's waiting to start, advance time. // We need to sort the processes by arrival time to see who's first. // To handle possible "tie", we need to re-sort by job exec time! // But when seeing whose first, need the first one that hasn't yet // executed. System.out.println("\nLet's try Shortest Job Next scheduling."); for (int i = 0; i < list.size(); ++i) { Process p = list.get(i); p.reset(); } // UNCOMMENT the following line once you have written the comparator: // Collections.sort(list, new ProcessExecComparator()); /////////////////////////////////////////////////////// // INSERT CODE HERE /////////////////////////////////////////////////////// // OUTPUT... System.out.println("Average response time = " + responseTime); // Print out all the process data. for (int i = 0; i < list.size(); ++i) System.out.println(list.get(i)); } }