forked from bourque/round_robin_processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
163 lines (139 loc) · 4.02 KB
/
Driver.java
File metadata and controls
163 lines (139 loc) · 4.02 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
/* Matthew Bourque
* COSC 519
* Semester Project
*
* This program is the main driver for the round-robin process scheduling
* project.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Driver {
protected static Scanner userInput = null;
protected static int MAXIMUM_PID;
protected static List<Process> readyQueue;
protected static double timeQuantum = 0.0;
private static final int MINIMUM_PID = 1;
private static final int MINIMUM_BURST_TIME = 1;
private static final int MAXIMUM_BURST_TIME = 10;
public static void main(String[] args) {
// Parse agruments
askUserForInputs(Boolean.TRUE);
readyQueue = new ArrayList<Process>();
// Add the process in the readyQueue
initProcess();
// Ask User to set the time quantum value
askUserForInputs(Boolean.FALSE);
// Schedule the processes
Scheduler scheduler = new Scheduler();
scheduler.roundRobin(readyQueue, timeQuantum);
}
private static void askUserForInputs(boolean isProcessValueToBeSet) {
if (isProcessValueToBeSet) {
System.out
.println("Please enter the number of Process you want to simulate");
newScanner();
evaluateUserInput(Boolean.TRUE);
closeScanner();
} else {
System.out
.println("Please enter the time Quantum you want to assign");
newScanner();
evaluateUserInput(Boolean.FALSE);
closeScanner();
}
}
private static void closeScanner() {
userInput.close();
}
private static void newScanner() {
userInput = new Scanner(System.in);
}
private static void evaluateUserInput(boolean isProcessNumberSelected) {
boolean accurateInputsReceived = Boolean.FALSE;
while (!accurateInputsReceived) {
if (isProcessNumberSelected) {
if (userInput.hasNextInt()) {
MAXIMUM_PID = userInput.nextInt();
accurateInputsReceived = Boolean.TRUE;
if (MAXIMUM_PID < 0) {
System.out
.println("Please enter the process number atleast more than 5");
accurateInputsReceived = Boolean.FALSE;
}
} else {
System.out
.println("Please enter a Positive Integer for the number of Process you want to simulate");
}
} else {
if (userInput.hasNextDouble()) {
timeQuantum = userInput.nextDouble();
accurateInputsReceived = Boolean.TRUE;
} else {
System.out
.println("Please enter the Time Quantum value again, for e.g. 2.0 or 4.0");
}
}
}
}
private static void initProcess() {
/*
* Instantiate the Ready Queue
*/
int counter = MAXIMUM_PID;
while (counter > -1) {
int newRandom = getRandomNumber(Boolean.TRUE);
Process process = instantiateProcess(newRandom);
readyQueue.add(process);
}
}
private static Process instantiateProcess(int newRandom) {
Process p = new Process();
p.pid = String.valueOf(newRandom);
p.burstTime = getRandomNumber(Boolean.FALSE);
return p;
}
private static int getRandomNumber(boolean generateForPID) {
Random random = new Random();
if (generateForPID) {
return random.nextInt(MAXIMUM_PID - MINIMUM_PID) + MINIMUM_PID;
} else {
return random.nextInt(MINIMUM_BURST_TIME - MAXIMUM_BURST_TIME)
+ MINIMUM_BURST_TIME;
}
}
public static Object[] parseArgs(String[] args) {
/*
* Parse command line arguments. Return arguments in a list of objects.
*/
Object[] arguments = new Object[2];
try {
arguments[0] = args[0];
arguments[1] = args[1];
} catch (Exception e) {
System.out.println("Missing or invalid command line argument.");
System.exit(0);
}
return arguments;
}
public static List<String> readProcesses(String processFile) {
/*
* Return a list of processes to execute.
*/
List<String> processList = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(
processFile));
String line = null;
while ((line = reader.readLine()) != null) {
processList.add(line);
}
} catch (Exception e) {
System.out.printf("Error reading in file", processFile);
}
return processList;
}
}