Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. /**
  2. * Write a description of class Banker here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.util.*;
  8. import java.io.*;
  9. import java.text.*;
  10. import java.lang.Integer;
  11.  
  12. public class Banker
  13. {
  14. public static void main( String[] args )
  15. throws FileNotFoundException
  16. {
  17. // decalre and initialize variables and arrays
  18. // prompt for data file name
  19. // read data into variables and arrays
  20. Scanner keyboard = new Scanner( System.in );
  21. String filename = "";
  22. System.out.printf("\u000cFile name: ");
  23. filename = keyboard.nextLine();
  24. File fh = new File( filename );
  25. Scanner inF = new Scanner( fh );
  26. String data = "";
  27. int i=0, j=0;
  28. long start = System.currentTimeMillis();
  29.  
  30. data = inF.nextLine(); //read comment
  31. System.out.printf("%s%n",data);
  32.  
  33. final int NUMBER_OF_RESOURCES = inF.nextInt();
  34.  
  35. String[] numbers = new String[NUMBER_OF_RESOURCES];
  36. System.out.printf("%2d%n",NUMBER_OF_RESOURCES);
  37.  
  38. data = inF.nextLine();
  39. data = inF.nextLine(); //read comment
  40. System.out.printf("%s%n",data);
  41.  
  42. final int NUMBER_OF_THREADS = inF.nextInt();
  43. System.out.printf("%2d%n",NUMBER_OF_THREADS);
  44.  
  45. boolean[] finished = new boolean[NUMBER_OF_THREADS];
  46. for( i=0; i<NUMBER_OF_THREADS; i++)
  47. finished[i]= false;
  48.  
  49. data = inF.nextLine();
  50. data = inF.nextLine(); //read comment
  51. System.out.printf("%s%n",data);
  52. /* the available amount of each resource */
  53. int[] available = new int[NUMBER_OF_RESOURCES];
  54. /*the maximum demand of each thread */
  55. int[][] maximum = new int[NUMBER_OF_THREADS][NUMBER_OF_RESOURCES];
  56.  
  57. /* the amount currently allocated to each thread */
  58. int[][] allocation = new int [NUMBER_OF_THREADS][NUMBER_OF_RESOURCES];
  59.  
  60. /* the total allocated of each resource type */
  61. int[] allocationSum = new int [NUMBER_OF_RESOURCES];
  62.  
  63. /* the remaining need of each thread */
  64. int[][] need = new int [NUMBER_OF_THREADS][NUMBER_OF_RESOURCES];
  65.  
  66. /*available resources to work with */
  67. int[] work = new int [NUMBER_OF_RESOURCES];
  68. // data = inF.nextLine(); //read comment
  69. data = inF.nextLine(); //read comment
  70. System.out.printf("%s%n",data);
  71. numbers = data.split(",");
  72. for(i=0; i<numbers.length; i++)
  73. available[i] = Integer.parseInt(numbers[i]);
  74.  
  75. data = inF.nextLine(); //read comment
  76. System.out.printf("%s%n",data);
  77. // read, split and convert data for maximum required by each thread
  78. for (i=0; i<NUMBER_OF_THREADS; i++)
  79. {
  80. data = inF.nextLine();
  81. for(j=0; j<NUMBER_OF_RESOURCES; j++)
  82. {
  83.  
  84. numbers = data.split(",");
  85. System.out.printf("%2d ", Integer.parseInt(numbers[j]));
  86. maximum[i][j] = Integer.parseInt(numbers[j]);
  87. }
  88.  
  89. System.out.printf("%n");
  90. }
  91.  
  92. data = inF.nextLine(); //read comment
  93. System.out.printf("%s%n",data);
  94. // read, split, convert and print data for starting allocations for each thread
  95. for (i=0; i<NUMBER_OF_THREADS; i++)
  96. {
  97. data = inF.nextLine();
  98. for( j=0; j<NUMBER_OF_RESOURCES; j++)
  99. {
  100.  
  101. numbers = data.split(",");
  102. System.out.printf("%2d ", Integer.parseInt(numbers[j]));
  103. allocation[i][j] = Integer.parseInt(numbers[j]);
  104. }
  105. System.out.printf("%n");
  106. }
  107.  
  108. //fill and print allocationSum array
  109. System.out.printf("//sum of allocations%n");
  110. for( i=0; i<NUMBER_OF_RESOURCES; i++)
  111. {
  112. for(j=0; j<NUMBER_OF_THREADS; j++)
  113. {
  114. allocationSum[i] += allocation[j][i];
  115.  
  116. }
  117. System.out.printf("%2d ", allocationSum[i]);
  118. }
  119.  
  120. //fill and print work (currently avaiable resources)
  121. System.out.printf("%n//work array%n");
  122. for( i=0; i<NUMBER_OF_RESOURCES; i++)
  123. {
  124.  
  125. work[i] = available[i] - allocationSum[i];
  126.  
  127. System.out.printf("%2d ", work[i]);
  128. }
  129. System.out.printf("%n");
  130.  
  131. //fill and print need array (additional resources each thread may request)
  132. System.out.printf("//need array%n");
  133. for( i=0; i<NUMBER_OF_THREADS; i++)
  134. {
  135. for( j=0; j<NUMBER_OF_RESOURCES; j++)
  136. {
  137. need[i][j] = maximum[i][j] - allocation[i][j];
  138.  
  139. System.out.printf("%2d ", need[i][j]);
  140. }
  141. System.out.printf("%n");
  142. }
  143. System.out.printf("%n");
  144.  
  145. // begin checking for safe order to process threads
  146. // your code here
  147. // end of checking
  148. for(int k = 0; k < NUMBER_OF_THREADS; k++){
  149. checkSafety(work, need, allocation, finished, NUMBER_OF_THREADS, NUMBER_OF_RESOURCES);
  150. print_work(work);
  151. System.out.println();
  152. }
  153.  
  154. inF.close();
  155. long end = System.currentTimeMillis();
  156. NumberFormat formatter = new DecimalFormat("#0.00000");
  157. System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds\n");
  158. }
  159.  
  160. // check if a thread can run safely - if so, release allocated resources and set finished[thread_number] to true
  161. public static void checkSafety( int[] work, int[][] need, int[][] allocation, boolean[] finished, int NUMBER_OF_THREADS, int NUMBER_OF_RESOURCES)
  162. {
  163. for(int i = 0; i < NUMBER_OF_THREADS; i++){
  164.  
  165. if(!complete(finished)){
  166. if(threadCanRun(work, need[i], NUMBER_OF_RESOURCES)){
  167.  
  168. release_resources(work, allocation[i]);
  169. finished[i] = true;
  170. System.out.printf("Thread %d can safely run",i);
  171. print_work(work);
  172. }
  173. }
  174. }
  175. }
  176. // add allocated resources to work for a thread
  177. public static void release_resources(int[] work, int[] allocation)
  178. {
  179. for(int i = 0; i < work.length; i++){
  180. work[i] = work[i] + allocation[i];
  181. }
  182. }
  183.  
  184. // check if need <= work for a thread, if so, return true else return false
  185. public static boolean threadCanRun( int[] work, int[] need, int NUMBER_OF_RESOURCES )
  186. {
  187. boolean canRun = true;
  188. for(int i = 0; i < NUMBER_OF_RESOURCES; i++){
  189. if(need[i] <= work[i])
  190. canRun = false;
  191. }
  192. return canRun;
  193. }
  194.  
  195. // if all finished = true, then return true, else return false
  196. public static boolean complete( boolean[] finished )
  197. {
  198. boolean result = true;
  199. for(int i = 0; i < finished.length; i++){
  200. if(!finished[i])
  201. result = false;
  202. }
  203. return result;
  204. }
  205.  
  206. // print the work array
  207. public static void print_work( int[] work)
  208. {
  209. for(int i = 0; i < work.length; i++)
  210. System.out.printf("%d ",work[i]);
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement