Advertisement
Guest User

Untitled

a guest
May 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. // Jevin Olano
  2. // jolano
  3. // CMPS 12B
  4. // May 22, 2019
  5. // Simulation.java - executes commands implemented within Queue.java
  6.  
  7. import java.io.*;
  8. import java.util.Scanner;
  9.  
  10. public class Simulation {
  11.  
  12. public static Job getJob(Scanner in) {
  13.  
  14. String[] s = in.nextLine().split(" ");
  15. int arrival = Integer.parseInt(s[0]);
  16. int duration = Integer.parseInt(s[1]);
  17. return new Job(arrival, duration);
  18.  
  19. }
  20.  
  21. static void printUsage() {
  22. System.out.println("Usage: Java Simulation <input file>");
  23. System.exit(1);
  24. }
  25.  
  26. // helper method to put Jobs to be completed in shortest available queue
  27. static Queue getShortestQueue(Queue[] processorQueues) {
  28.  
  29. int shortestLength = Integer.MAX_VALUE;
  30. Queue shortestQueue = null;
  31.  
  32. for (int i = 1; i < processorQueues.length; i++) { // previously i <= processorQueues.length
  33. if (processorQueues[i].length() < shortestLength) {
  34. shortestLength = processorQueues[i].length();
  35. shortestQueue = processorQueues[i];
  36. }
  37. }
  38. return shortestQueue;
  39. }
  40.  
  41. static void printReport(PrintWriter report, int numProcessors, Queue completedJobs) {
  42.  
  43. // compute the total wait, maximum wait, and average wait for all Jobs
  44. int totalWait = 0;
  45. int maxWait = 0;
  46. int numJobs = completedJobs.length();
  47.  
  48. while (!completedJobs.isEmpty()) {
  49. Job job = (Job) completedJobs.dequeue();
  50. int waitTime = job.getWaitTime();
  51. totalWait += waitTime; // find total wait time
  52. if (waitTime > maxWait) { // find maximum wait time
  53. maxWait = waitTime;
  54. }
  55. }
  56.  
  57. float avgWait = ((float) totalWait) / numJobs; // find average wait time
  58.  
  59. // print to report file
  60. report.printf("%d processor%s: totalWait=%d, maxWait=%d, averageWait=%0.2f\n", numProcessors, (numProcessors>1?"s":""),
  61. totalWait, maxWait, avgWait);
  62. }
  63.  
  64. static void printTrace(PrintWriter trace, int time, Queue[] processorQueues, Queue completedJobs) {
  65. if (time > 0) {
  66. trace.println();
  67. }
  68. trace.println("time=" + time);
  69. for (int i = 0; i < processorQueues.length; i++) {
  70. trace.print(i + ": " + processorQueues[i].toString());
  71. if (i == 0 && !completedJobs.isEmpty()) {
  72. trace.print(" " + completedJobs.toString());
  73. }
  74. trace.println();
  75. }
  76. }
  77.  
  78. static void printTrace(PrintWriter trace, int numProcessors) {
  79. trace.println("*****************************");
  80. trace.printf("%d processor%s:\n", numProcessors,(numProcessors>1?"s":""));
  81. trace.println("*****************************");
  82. }
  83.  
  84. static void printReportHeader(PrintWriter report, String fileName, Job[] jobs) {
  85. report.println("Report file: " + fileName);
  86. printJobs(report, jobs);
  87. report.println("***********************************************************");
  88. }
  89.  
  90. static void printTraceHeader(PrintWriter trace, String fileName, Job[] jobs) {
  91. trace.println("Trace file: " + fileName);
  92. printJobs(trace, jobs);
  93. }
  94.  
  95. static void printJobs(PrintWriter pw, Job[] jobs) {
  96. pw.println(jobs.length + " jobs:");
  97. for (int i = 0; i < jobs.length; i++) {
  98. if (i > 0) {
  99. pw.print(" ");
  100. }
  101. pw.print(jobs[i].toString());
  102. }
  103. pw.println("\n");
  104. }
  105.  
  106. public static void main(String[] args) throws IOException {
  107.  
  108. // check # of command arguments
  109. if (args.length != 1) {
  110. printUsage();
  111. }
  112.  
  113. // open files for reading and writing
  114. Scanner input = new Scanner(new File(args[0]));
  115. PrintWriter report = new PrintWriter(new FileWriter(args[0]+".rpt"));
  116. PrintWriter trace = new PrintWriter(new FileWriter(args[0]+".trc"));
  117.  
  118. int numJobs = Integer.parseInt(input.nextLine()); // # of Jobs to be processed, given by first line of input file
  119. Job[] jobs = new Job[numJobs]; // array of all Jobs
  120. Queue completedJobs = new Queue(); // used to track completion of Jobs
  121.  
  122. // "populate" jobs
  123. for (int i = 0; i < numJobs; i++) {
  124. jobs[i] = getJob(input);
  125. }
  126.  
  127. // headers for report & trace go HERE ------------------------------------------------------------------------------
  128.  
  129. printReportHeader(report, args[0]+".rpt", jobs);
  130. printTraceHeader(trace, args[0]+".trc", jobs);
  131.  
  132. // run simulation with numProcessors processors for numProcessors = 1 to numProcessors = numJobs-1 -----------------
  133. for (int numProcessors = 1; numProcessors < numJobs; numProcessors++) {
  134.  
  135. // declare and initialize an array of n processor Queues and any necessary storage Queues
  136. Queue[] processorQueues = new Queue[numProcessors+1]; // n+1 = amount of processor queues + 1 storage queue
  137.  
  138. // allocate each queue
  139. for (int i = 0; i < processorQueues.length; i++) {
  140. processorQueues[i] = new Queue();
  141. }
  142.  
  143. // enqueue all Jobs in Queue 0 (storage queue)
  144. for (int j = 0; j < jobs.length; j++) {
  145. processorQueues[0].enqueue((Job) jobs[j]);
  146. jobs[j].resetFinishTime();
  147. } // THIS WORKS FOR NOW DONT CHANGE
  148.  
  149. // set timer to 0
  150. int time = 0;
  151. printTrace(trace, time, processorQueues, completedJobs);
  152.  
  153. // while unprocessed jobs remain
  154. while (completedJobs.length() != numJobs) {
  155. System.out.print("test2"); // infinite loop here
  156. time++;
  157. boolean eventHappened = false;
  158.  
  159. // determine the time of the next arrival or finish event and update time
  160. for (int k = 1; k < numProcessors; k++) { // used to be k <= numProcessors
  161. if (processorQueues[k].length() == 0) {
  162. continue;
  163. }
  164. Job job = (Job) processorQueues[k].peek();
  165. if (job.getFinish() == time) {
  166. processorQueues[k].dequeue();
  167. completedJobs.enqueue(job);
  168. if (processorQueues[k].length() > 0) {
  169. job = (Job) processorQueues[k].peek();
  170. job.computeFinishTime(time);
  171. }
  172. eventHappened = true;
  173. }
  174. }
  175.  
  176. if (processorQueues[0].length() > 0) {
  177. Job job = (Job) processorQueues[0].peek();
  178. while (job.getArrival() == time) {
  179. processorQueues[0].dequeue();
  180. Queue shortestQueue = getShortestQueue(processorQueues);
  181. shortestQueue.enqueue(job);
  182. if (shortestQueue.length() == 1) {
  183. job.computeFinishTime(time);
  184. }
  185. eventHappened = true;
  186. if (processorQueues[0].length() == 0) {
  187. break;
  188. }
  189. job = (Job) processorQueues[0].peek();
  190. }
  191. }
  192.  
  193. if (eventHappened) {
  194. printTrace(trace, numProcessors);
  195. printTrace(trace, time, processorQueues, completedJobs); // print the body of the trace file
  196. }
  197. }
  198.  
  199. // print the body of the report file
  200. printReport(report, numProcessors, completedJobs);
  201.  
  202. }
  203.  
  204. // close input and output files
  205. input.close();
  206. report.close();
  207. trace.close();
  208.  
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement