-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadingFiles.java
More file actions
182 lines (153 loc) · 6.57 KB
/
ReadingFiles.java
File metadata and controls
182 lines (153 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Written by Mostafa Mohamed Ali
* CSCE 311
*/
import java.io.*;
import java.util.*;
public class ReadingFiles {
public static void main(String[] args) throws Exception {
// Scanner to get the User absolute file path
Scanner scan = new Scanner(System.in);
// Prompting the user to enter file
System.out.println("Enter Alloc text file path: ");
String allocScan = scan.nextLine();
// Prompting the user to enter file
System.out.println("Enter Req text file path: ");
String reqScan = scan.nextLine();
// Prompting the user to enter file
System.out.println("Enter Avail text file path: ");
String availScan = scan.nextLine();
BufferedReader bufAlloc = new BufferedReader(
new FileReader(allocScan));
BufferedReader bufReq = new BufferedReader(
new FileReader(reqScan));
BufferedReader bufAvail = new BufferedReader(
new FileReader(availScan));
ArrayList<String> listAlloc = new ArrayList<>();
ArrayList<String> listReq = new ArrayList<>();
ArrayList<String> listAvail = new ArrayList<>();
String lineAlloc = bufAlloc.readLine();
String lineReq = bufReq.readLine();
String lineAvail = bufAvail.readLine();
String seq = "";
char answer = 'y';
boolean flag = true;
int q = 0;
// initialize a boolean array
Boolean[] boolArray = new Boolean[5];
// set all to false
Arrays.fill(boolArray, Boolean.FALSE);
// set user input to upper case
while (lineAlloc != null) {
listAlloc.add(lineAlloc);
lineAlloc = bufAlloc.readLine();
}
while (lineReq != null) {
listReq.add(lineReq);
lineReq = bufReq.readLine();
}
while (lineAvail != null) {
listAvail.add(lineAvail);
lineAvail = bufAvail.readLine();
}
// to handle leaks
bufAlloc.close();
bufReq.close();
bufAvail.close();
// declare arrays
int[][] alloc = new int[5][3];
int[][] req = new int[5][3];
int[][] avail = new int[1][3];
int[][] work = new int[5][3];
for (int i = 2; i < listAlloc.size(); i++) {
int k = 0;
for (int j = 0; j < listAlloc.get(i).length(); j++) {
if (listAlloc.get(i).charAt(j) != ' ') {
alloc[i - 2][k] = (int) (listAlloc.get(i).charAt(j) - 48);
k++;
}
}
}
for (int i = 2; i < listReq.size(); i++) {
int k = 0;
for (int j = 0; j < listReq.get(i).length(); j++) {
// reads chars between the tabs
if (listReq.get(i).charAt(j) != ' ') {
req[i - 2][k] = (int) (listReq.get(i).charAt(j) - 48);
k++;
}
}
}
for (int i = 1; i < listAvail.size(); i++) {
int k = 0;
for (int j = 0; j < listAvail.get(i).length(); j++) {
// reads chars between the spaces
if (listAvail.get(i).charAt(j) != ' ') {
avail[i - 1][k] = (int) (listAvail.get(i).charAt(j) - 48);
k++;
}
}
}
while (Character.toUpperCase(answer) == 'Y' && flag) {
flag = false;
for (int s = 0; s < boolArray.length; s++) {
if (boolArray[s] == false) {
flag = true;
// set user input to upper case
if (Character.toUpperCase(answer) == 'Y') {
for (int i = 0; i < alloc.length; i++)
for (int j = 0; j < alloc[i].length; j++)
work[i][j] = req[i][j];
for (int i = 0; i < req.length; i++) {
int k = 0;
for (int j = 0; j < req[i].length; j++) {
if (req[i][j] <= avail[0][j]) {
k++;
}
}
if (k == 3 && boolArray[i] == false) {
for (int j = 0; j < req[i].length; j++)
avail[0][j] = avail[0][j] + alloc[i][j]; // adds alloc to avail
boolArray[i] = true; // sets to true if the processor is passed
System.out.println(Arrays.toString(boolArray));
System.out.println(("P" + i)); // a deadlock does not exist so the sequence is shown to
// the user
// step by step
seq += "P" + String.valueOf(i) + ",";
}
for (int l = 0; l < avail.length; l++) {
for (int j = 0; j < avail[l].length; j++) {
System.out.print(avail[l][j] + " ");
}
System.out.println();
}
System.out.print("\n Press y to continue, Press n to terminate and show the sequence: ");
answer = scan.next().charAt(0);
// terminate program if answer = n
if (Character.toUpperCase(answer) == 'N') {
System.out.println(seq); // to print the sequence
System.exit(0);
}
// to terminate after all processors are true
for (int n = 0; n < boolArray.length; n++) {
if (boolArray[n] == false) {
flag = true;
}
}
System.out.println("Sequence: " + seq); // to print the sequence
}
}
}
if (!flag) {
System.exit(0);
}
}
}
// prompts the user if the system is deadlocked
for (int n = 0; n < boolArray.length; n++) {
if (boolArray[n] == false) {
System.out.println("There is a Deadlock!");
}
}
}
}