Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. class PROIRTYWITHPREEMPTION{
  4. public static void main(String args[]) {
  5. int sumw = 0, sumt = 0, sumBurst = 0, n, i, j, k, temp, preJobNo, doJob=0;
  6. double avgWT, turnAT;
  7. Scanner sc = new Scanner(System.in);
  8. System.out.println("ENTER NUMBER OF JOBS");
  9. n = sc.nextInt();
  10. int burstTime[] = new int[n];
  11. int waitTime[] = new int[n];
  12. int priority[] = new int[n];
  13. int arrival[] = new int[n];
  14. int jobNo[] = new int[n];
  15. int finished[] = new int[n];
  16.  
  17. System.out.println("Enter burst time, Priority and Arrival time for each job :");
  18. for (i = 0; i < n; i++) {
  19. System.out.println("Enter burst time of job "+i+" : ");
  20. burstTime[i] = sc.nextInt();
  21. System.out.println("Enter priority of job "+i+" : ");
  22. priority[i] = sc.nextInt();
  23. System.out.println("Enter arrival time of job "+i+" : ");
  24. arrival[i] = sc.nextInt();
  25. jobNo[i] = i+1;
  26. sumBurst = sumBurst + burstTime[i];
  27. }
  28.  
  29. int ganntChart[] = new int[sumBurst];
  30.  
  31. /* Sorting all the job as per priority time */
  32. for (i = 0; i < n - 1; i++) {
  33. for (j = 0; j < n - 1; j++) {
  34. if (priority[j] > priority[j + 1]) {
  35. temp = priority[j];
  36. priority[j] = priority[j + 1];
  37. priority[j + 1] = temp;
  38.  
  39. temp = arrival[j];
  40. arrival[j] = arrival[j + 1];
  41. arrival[j + 1] = temp;
  42.  
  43. temp = burstTime[j];
  44. burstTime[j] = burstTime[j + 1];
  45. burstTime[j + 1] = temp;
  46.  
  47. temp = jobNo[j];
  48. jobNo[j] = jobNo[j + 1];
  49. jobNo[j + 1] = temp;
  50. }
  51. }
  52. }
  53.  
  54. /*printing the input table just for referance */
  55. System.out.println("JobNo\tBT\tpriority\tarrival");
  56. for(i=0;i<n;i++){
  57. System.out.println(jobNo[i]+"\t"+burstTime[i]+"\t"+priority[i]+"\t\t"+arrival[i]);
  58. }
  59.  
  60. /* printing out the gannt chart int form of table */
  61. doJob = 0;
  62. System.out.println("Time Unit" + "\t" + "Job Number");
  63. for (i = 0; i < sumBurst; i++) {
  64.  
  65. //get doable jobs as per arrival time
  66. for(j=0;j<n;j++)
  67. {
  68. if(arrival[j]<=i&&burstTime[j]!=0)
  69. {
  70. doJob = jobNo[j];
  71. burstTime[j]--;
  72. if(burstTime[j]==0)
  73. {
  74. finished[j] = i+1;
  75. }
  76. break;
  77. }
  78.  
  79. }
  80.  
  81. System.out.print(i+"-"+ (i+1) + "\t\t" + doJob);
  82. ganntChart[i] = doJob;
  83. System.out.println();
  84. }
  85.  
  86. /*Calculation of Average Wait time */
  87. sumw = 0;
  88. for(i=0;i<n;i++){
  89. for(j=0;j<sumBurst;j++){
  90. if(ganntChart[j]!=jobNo[i]&&j<finished[i]){
  91. sumw++;
  92. }
  93. }
  94. sumw = sumw - arrival[i];
  95. }
  96. avgWT = sumw * (1.0) / n;
  97.  
  98. /*Calculation of Average Turn Around time */
  99. j=0;
  100. while(j<n){
  101. sumt = sumt + finished[j]-arrival[j];
  102. j++ ;
  103. }
  104. turnAT = sumt * (1.0) / n;
  105.  
  106. /*printing out the results*/
  107. System.out.println();
  108. System.out.println("AVERAGE WAITING TIME =" + avgWT + "\t" + "AVERAGE TURN AROUND TIME=" + turnAT);
  109. }
  110. }
  111.  
  112.  
  113. OUTPUT:
  114.  
  115.  
  116. ENTER NUMBER OF JOBS 5
  117. Enter burst time, Priority and Arrival time for each job :
  118. Enter burst time of job 0 : 10
  119. Enter priority of job 0 : 3
  120. Enter arrival time of job 0 :0
  121. Enter burst time of job 1 : 1
  122. Enter priority of job 1 : 1
  123. Enter arrival time of job 1 :1
  124. Enter burst time of job 2 : 2
  125. Enter priority of job 2 : 3
  126. Enter arrival time of job 2 :2
  127. Enter burst time of job 3 : 1
  128. Enter priority of job 3 : 4
  129. Enter arrival time of job 3 :3
  130. Enter burst time of job 4 : 5
  131. Enter priority of job 4 : 2
  132. Enter arrival time of job 4 :4
  133. JobNo BT priority arrival
  134. 2 1 1 1
  135. 5 5 2 4
  136. 1 10 3 0
  137. 3 2 3 2
  138. 4 1 4 3
  139. Time Unit Job Number
  140. 0-1 1
  141. 1-2 2
  142. 2-3 1
  143. 3-4 1
  144. 4-5 5
  145. 5-6 5
  146. 6-7 5
  147. 7-8 5
  148. 8-9 5
  149. 9-10 1
  150. 10-11 1
  151. 11-12 1
  152. 12-13 1
  153. 13-14 1
  154. 14-15 1
  155. 15-16 1
  156. 16-17 3
  157. 17-18 3
  158. 18-19 4
  159.  
  160. AVERAGE WAITING TIME =7 AVERAGE TURN AROUND TIME=10.8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement