-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.java
161 lines (142 loc) · 4.99 KB
/
Simulator.java
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
import java.util.Scanner;
public class Simulator implements Runnable {
//This class will be used to gather and print statistics for the simulation run
//as well as being the place where the simulation is actually run
//It will also compare two or more different scheduling algorithms
public static void main(String[] args) {
Thread t = new Thread(new Simulator());
t.start();
}
//create objects of every other class.
public void run() {
Machine comp = Machine.getInstance();
QueueManager queue = new QueueManager(1);
queue.setDefaultDeviceTable();
queue.setDefaultResourceTable();
int response = 1;
while (response > 0){
System.out.println("Welcome to the Scheduling Simulator");
System.out.println();
System.out.println("Select an option, then press Enter");
System.out.println("0. Exit");
System.out.println("1. Use Default Set of Processes");
System.out.println("2. Add Process");
System.out.println("3. Run Simulator");
System.out.println("4. Change Scheduling Type");
System.out.println("5. Print Device Table");
System.out.println("6. Print Resource Table");
System.out.println("7. Reset Queues");
@SuppressWarnings("resource")
Scanner reader = new Scanner(System.in);
response=reader.nextInt();
switch (response){
case 1:
queue.emptyQueues();
// pid name pri size subtime burst dev req res req
Process p1 = new Process(1,"p1", 1, 5, 0, 6, "D-1 D-2", "R-1 R-3");
Process p2 = new Process(2,"p2", 2, 3, 3, 1, "D-4 D-2", "R-2 R-5");
Process p3 = new Process(3,"p3", 1, 10, 3, 3, "D-1 D-2", "R-1 R-3");
Process p4 = new Process(4,"p4", 3, 97, 3, 2, "D-8 D-9", "R-9 R-10");
Process p5 = new Process(5,"p5", 1, 42, 6, 9, "D-1 D-2", "R-1 R-2");
queue.addProcess(p1);
queue.addProcess(p2);
queue.addProcess(p3);
queue.addProcess(p4);
queue.addProcess(p5);
System.out.println("Done...");
break;
case 2:
int moreProcessFlag = 1;
int moreDeviceFlag = 1;
int moreResourceFlag = 1;
String name;
int size;
int pid;
int prio;
int subtime;
int bursts;
String devices = "";
String resources = "";
while (moreProcessFlag == 1){
System.out.println("Please enter a name for the process");
name = reader.next();
System.out.println("Please enter an integer pid");
pid = reader.nextInt();
System.out.println("Please enter an integer priority > 0");
prio = reader.nextInt();
System.out.println("Please enter an integer number of RAM pages required > 0");
size = reader.nextInt();
System.out.println("Please enter an integer submit time > 0");
subtime = reader.nextInt();
System.out.println("Please enter an integer number of bursts > 0");
bursts = reader.nextInt();
while (moreDeviceFlag == 1)
{
System.out.println("Please enter required device in the format \"D-x\", where x is an integer");
devices += " " + reader.next();
System.out.println("Would you like to enter more devices? 1 for yes, 0 for no");
moreDeviceFlag = reader.nextInt();
}
while (moreResourceFlag == 1)
{
System.out.println("Please enter required resources in the format \"R-x\", where x is an integer");
resources += " " + reader.next();
System.out.println("Would you like to enter more resources? 1 for yes, 0 for no");
moreResourceFlag = reader.nextInt();
}
Process pTemp = new Process (pid, name, prio, size, subtime, bursts, devices, resources);
queue.addProcess(pTemp);
//add process
System.out.println("Process Added");
System.out.println("Enter 1 to add another process, 0 to return to main menu");
moreProcessFlag = reader.nextInt();
}
break;
case 3:
//run simulation
while (!queue.isFinished()){
System.out.println(queue.toString());
queue.update();
queue.toString();
queue.cycleIncrement();
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
System.out.println(queue.toString());
double avgWaitTime = 0.0;
int numOfProcs = 0;
for (Process p : queue.getTerminateQueue()){
avgWaitTime += p.getWaitTime();
numOfProcs++;
}
avgWaitTime /= numOfProcs;
System.out.println("Average Wait Time: " + avgWaitTime);
System.out.println("Simulation Done!");
break;
case 4:
//change scheduling
System.out.println("Pick one of the following option:");
System.out.println("1. First-Come First-Serve");
System.out.println("2. Shortest Job First");
System.out.println("3. Priotiry Scheduling");
queue.setScheduler(reader.nextInt());
System.out.println("Scheduler Changed!");
break;
case 5:
System.out.println(Device.getTableInfo());
break;
case 6:
System.out.println(Resource.getTableInfo());
break;
case 7:
queue.emptyQueues();
default:
System.out.println("Goodbye!");
break;
}
}
}
}