Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Jevin Olano
- // jolano
- // CMPS 12B
- // May 22, 2019
- // Simulation.java - executes commands implemented within Queue.java
- import java.io.*;
- import java.util.Scanner;
- public class Simulation {
- public static Job getJob(Scanner in) {
- String[] s = in.nextLine().split(" ");
- int arrival = Integer.parseInt(s[0]);
- int duration = Integer.parseInt(s[1]);
- return new Job(arrival, duration);
- }
- static void printUsage() {
- System.out.println("Usage: Java Simulation <input file>");
- System.exit(1);
- }
- // helper method to put Jobs to be completed in shortest available queue
- static Queue getShortestQueue(Queue[] processorQueues) {
- int shortestLength = Integer.MAX_VALUE;
- Queue shortestQueue = null;
- for (int i = 1; i < processorQueues.length; i++) { // previously i <= processorQueues.length
- if (processorQueues[i].length() < shortestLength) {
- shortestLength = processorQueues[i].length();
- shortestQueue = processorQueues[i];
- }
- }
- return shortestQueue;
- }
- static void printReport(PrintWriter report, int numProcessors, Queue completedJobs) {
- // compute the total wait, maximum wait, and average wait for all Jobs
- int totalWait = 0;
- int maxWait = 0;
- int numJobs = completedJobs.length();
- while (!completedJobs.isEmpty()) {
- Job job = (Job) completedJobs.dequeue();
- int waitTime = job.getWaitTime();
- totalWait += waitTime; // find total wait time
- if (waitTime > maxWait) { // find maximum wait time
- maxWait = waitTime;
- }
- }
- float avgWait = ((float) totalWait) / numJobs; // find average wait time
- // print to report file
- report.printf("%d processor%s: totalWait=%d, maxWait=%d, averageWait=%0.2f\n", numProcessors, (numProcessors>1?"s":""),
- totalWait, maxWait, avgWait);
- }
- static void printTrace(PrintWriter trace, int time, Queue[] processorQueues, Queue completedJobs) {
- if (time > 0) {
- trace.println();
- }
- trace.println("time=" + time);
- for (int i = 0; i < processorQueues.length; i++) {
- trace.print(i + ": " + processorQueues[i].toString());
- if (i == 0 && !completedJobs.isEmpty()) {
- trace.print(" " + completedJobs.toString());
- }
- trace.println();
- }
- }
- static void printTrace(PrintWriter trace, int numProcessors) {
- trace.println("*****************************");
- trace.printf("%d processor%s:\n", numProcessors,(numProcessors>1?"s":""));
- trace.println("*****************************");
- }
- static void printReportHeader(PrintWriter report, String fileName, Job[] jobs) {
- report.println("Report file: " + fileName);
- printJobs(report, jobs);
- report.println("***********************************************************");
- }
- static void printTraceHeader(PrintWriter trace, String fileName, Job[] jobs) {
- trace.println("Trace file: " + fileName);
- printJobs(trace, jobs);
- }
- static void printJobs(PrintWriter pw, Job[] jobs) {
- pw.println(jobs.length + " jobs:");
- for (int i = 0; i < jobs.length; i++) {
- if (i > 0) {
- pw.print(" ");
- }
- pw.print(jobs[i].toString());
- }
- pw.println("\n");
- }
- public static void main(String[] args) throws IOException {
- // check # of command arguments
- if (args.length != 1) {
- printUsage();
- }
- // open files for reading and writing
- Scanner input = new Scanner(new File(args[0]));
- PrintWriter report = new PrintWriter(new FileWriter(args[0]+".rpt"));
- PrintWriter trace = new PrintWriter(new FileWriter(args[0]+".trc"));
- int numJobs = Integer.parseInt(input.nextLine()); // # of Jobs to be processed, given by first line of input file
- Job[] jobs = new Job[numJobs]; // array of all Jobs
- Queue completedJobs = new Queue(); // used to track completion of Jobs
- // "populate" jobs
- for (int i = 0; i < numJobs; i++) {
- jobs[i] = getJob(input);
- }
- // headers for report & trace go HERE ------------------------------------------------------------------------------
- printReportHeader(report, args[0]+".rpt", jobs);
- printTraceHeader(trace, args[0]+".trc", jobs);
- // run simulation with numProcessors processors for numProcessors = 1 to numProcessors = numJobs-1 -----------------
- for (int numProcessors = 1; numProcessors < numJobs; numProcessors++) {
- // declare and initialize an array of n processor Queues and any necessary storage Queues
- Queue[] processorQueues = new Queue[numProcessors+1]; // n+1 = amount of processor queues + 1 storage queue
- // allocate each queue
- for (int i = 0; i < processorQueues.length; i++) {
- processorQueues[i] = new Queue();
- }
- // enqueue all Jobs in Queue 0 (storage queue)
- for (int j = 0; j < jobs.length; j++) {
- processorQueues[0].enqueue((Job) jobs[j]);
- jobs[j].resetFinishTime();
- } // THIS WORKS FOR NOW DONT CHANGE
- // set timer to 0
- int time = 0;
- printTrace(trace, time, processorQueues, completedJobs);
- // while unprocessed jobs remain
- while (completedJobs.length() != numJobs) {
- System.out.print("test2"); // infinite loop here
- time++;
- boolean eventHappened = false;
- // determine the time of the next arrival or finish event and update time
- for (int k = 1; k < numProcessors; k++) { // used to be k <= numProcessors
- if (processorQueues[k].length() == 0) {
- continue;
- }
- Job job = (Job) processorQueues[k].peek();
- if (job.getFinish() == time) {
- processorQueues[k].dequeue();
- completedJobs.enqueue(job);
- if (processorQueues[k].length() > 0) {
- job = (Job) processorQueues[k].peek();
- job.computeFinishTime(time);
- }
- eventHappened = true;
- }
- }
- if (processorQueues[0].length() > 0) {
- Job job = (Job) processorQueues[0].peek();
- while (job.getArrival() == time) {
- processorQueues[0].dequeue();
- Queue shortestQueue = getShortestQueue(processorQueues);
- shortestQueue.enqueue(job);
- if (shortestQueue.length() == 1) {
- job.computeFinishTime(time);
- }
- eventHappened = true;
- if (processorQueues[0].length() == 0) {
- break;
- }
- job = (Job) processorQueues[0].peek();
- }
- }
- if (eventHappened) {
- printTrace(trace, numProcessors);
- printTrace(trace, time, processorQueues, completedJobs); // print the body of the trace file
- }
- }
- // print the body of the report file
- printReport(report, numProcessors, completedJobs);
- }
- // close input and output files
- input.close();
- report.close();
- trace.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement