fork download
  1. import java.util.*;
  2. class Bankers {
  3. public static void main(String[] args) {
  4. int[][] curr = new int[5][5];
  5. int[][] max_claim = new int[5][5];
  6. int[] avl = new int[5];
  7. int[] alloc = {0, 0, 0, 0, 0};
  8. int[] max_res = new int[5];
  9. int[] running = new int[5];
  10. int i, j, exec, r, p;
  11. int count = 0;
  12. boolean safe = false;
  13. Scanner sc = new Scanner(System.in);
  14. System.out.println("\nEnter the number of resources: ");
  15. r = sc.nextInt();
  16. System.out.println("\nEnter the number of processes: ");
  17. p = sc.nextInt();
  18.  
  19. for (i = 0; i < p; i++) {
  20. running[i] = 1;
  21. count++;
  22. }
  23. //Enter the total number of resources
  24. System.out.println("\nEnter total # of resources: ");
  25. for (i = 0; i < r; i++){
  26. max_res[i] = sc.nextInt();
  27. }
  28. //Enter the Resources already allocated to each process
  29. System.out.println("\nEnter Allocated Resource Table: ");
  30. for (i = 0; i < p; i++) {
  31. for (j = 0; j < r; j++){
  32. curr[i][j] = sc.nextInt();
  33. }
  34. }
  35. //Enter the total number of resources required by each process to complete
  36. System.out.println("\nEnter Maximum Claim table: ");
  37. for (i = 0; i < p; i++) {
  38. for (j = 0; j < r; j++)
  39. max_claim[i][j] = sc.nextInt();
  40. }
  41. //Printing total resources
  42. System.out.println("\nThe Claim Vector is: ");
  43. for (i = 0; i < r; i++){
  44. System.out.println(max_res[i]);
  45. }
  46. //Printing the Allocated Table
  47. System.out.println("\nThe Allocated Resource Table:\n");
  48. for (i = 0; i < p; i++) {
  49. for (j = 0; j < r; j++){
  50. System.out.println(curr[i][j]);
  51. System.out.println("\n");
  52. }
  53. }
  54.  
  55. // Printing total resources required to complete for each process
  56. System.out.println("\nThe Maximum Claim Table:\n");
  57. for (i = 0; i < p; i++) {
  58. for (j = 0; j < r; j++){
  59. System.out.println(max_claim[i][j]);
  60. System.out.println("\n");
  61. }
  62. }
  63.  
  64. for (i = 0; i < p; i++)
  65. for (j = 0; j < r; j++)
  66. alloc[j] += curr[i][j];
  67.  
  68. //Calculating the total resources allocated for all processes
  69. System.out.println("\nAllocated resources: ");
  70. for (i = 0; i < r; i++)
  71. System.out.println(alloc[i]);
  72.  
  73. for (i = 0; i < r; i++) //Calculating available resources for allocating
  74. avl[i] = max_res[i] - alloc[i];
  75.  
  76. //Printing available resources for use
  77. System.out.println("\nAvailable resources: ");
  78. for (i = 0; i < r; i++)
  79. System.out.println(avl[i]);
  80. System.out.println("\n");
  81.  
  82. while (count != 0) {
  83. safe = false;
  84. for (i = 0; i < p; i++) {
  85. if (running[i] != 0) {
  86. exec = 1;
  87. for (j = 0; j < r; j++) {
  88. if (max_claim[i][j] - curr[i][j] > avl[j]) { // Checking if required resources of process are greater than available resources
  89. exec = 0;
  90. break;
  91. }
  92. }
  93.  
  94. //If required resources for a process is less than available then it is executed
  95.  
  96. if (exec !=0) {
  97. System.out.println("\nProcess%d is executing.\n" + i + 1);
  98. running[i] = 0;
  99. count--; //Decrease count to see how many processes are remaining
  100. safe = true;
  101. for (j = 0; j < r; j++)
  102. avl[j] += curr[i][j]; //Update available resources by adding resources allocated to process in the beginning
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. if (!safe) { //If no process is executed successfully
  109. System.out.println("\nThe processes are in unsafe state.");
  110. break;
  111. }
  112.  
  113. if (safe) // If process is executed successfully
  114. System.out.println("\nThe process is in safe state.");
  115.  
  116. System.out.println("\nAvailable vector: ");
  117. for (i = 0; i < r; i++)
  118. System.out.println(avl[i]); // Print total available resources after completion of process
  119. }
  120.  
  121.  
  122.  
  123.  
  124. }
  125. }
Runtime error #stdin #stdout #stderr 0.07s 2184192KB
stdin
Standard input is empty
stdout
Enter the number of resources: 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at Bankers.main(Main.java:15)