-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSRT.java
More file actions
191 lines (161 loc) · 5.59 KB
/
SRT.java
File metadata and controls
191 lines (161 loc) · 5.59 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
183
184
185
186
187
188
189
190
191
/*
* Evan Thompson, Tausif Ahmed
*/
import java.util.ArrayList;
import java.util.Collections;
public class SRT extends CPU_Algorithm {
// Array to hold preempted processes
ArrayList<Process> preempt_procs;
/**
* @param in_proc: list of processes
* @param num_cpus: # of cpus
* @effect intialize SRT object
*/
public SRT(ArrayList<Process> in_proc, int num_cpus){
copy_in_procs(in_proc);
super.NUM_PROCESSES = procs.size();
super.NUM_CPUS = num_cpus;
preempt_procs = new ArrayList<Process>();
}
/**
* @param time: current time
* @effect get next set of processes for CPUs
*/
@Override
protected void get_next_procs(int time){
// If this is the initial first processes
if(curr_procs.size() == 0) {
for(int i = 0; i < super.NUM_CPUS; i++) {
Process tmp = null;
curr_procs.add(tmp);
}
}
// Sort the preempted processes by smallest remaining time
Collections.sort(preempt_procs);
// Add preempted processes back to be run by CPU
for (int i = 0; i < curr_procs.size(); i++) {
if (preempt_procs.size() > 0 && curr_procs.get(i) == null) {
curr_procs.set(i, preempt_procs.remove(0));
curr_procs.get(i).set_wait(time);
}
}
// Sort processes by shortest burst times
Collections.sort(procs);
// Get the first n processes, fill in the null slots
for(int i = 0; i < curr_procs.size(); i++) {
if(procs.size() > 0 && curr_procs.get(i) == null) {
curr_procs.set(i, procs.remove(0));
curr_procs.get(i).set_wait(time);
}
}
}
/**
* @param context_time:list of context times
* @param time: current time
* @effect handle processes withing the CPUs
*/
private void burst_context_handle(ArrayList<Integer> context_time, int time) {
// Go through all of the processes and check if any have hit their burst
for(int i = 0; i < curr_procs.size(); i++) {
if (curr_procs.get(i) != null && procs.size() > 0) {
ArrayList<Process> tmpSort = new ArrayList<Process>(procs);
Collections.sort(tmpSort);
if (tmpSort.get(0).get_burst() < curr_procs.get(i).get_remaining_burst()) {
preempt_procs.add(curr_procs.get(i));
System.out.println("[time " + time + "ms] Preemption (preempting process ID " + curr_procs.get(i).get_pid());
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + curr_procs.get(i).get_pid() + " for process ID " + tmpSort.get(0).get_pid() + ")");
curr_procs.set(i, tmpSort.get(0));
procs.remove(tmpSort.get(0));
}
}
if(context_time.get(i) == 0 && curr_procs.get(i) != null) {
Process tmp_p = curr_procs.get(i);
if(!tmp_p.is_active()) {
tmp_p.activate_burst();
tmp_p.set_wait(time);
}
// Decrement the timer in this process
tmp_p.dec_curr_burst();
// Check if this completes the burst for this process
if(!tmp_p.is_active()) {
// Set the turnaround time for this burst
if(tmp_p.get_wait() > 0) {
tmp_p.set_turnaround(time + 1);
} else {
tmp_p.set_turnaround(time);
}
print_proc_end(tmp_p, time);
if(!tmp_p.finished()) {
// Generate its blocking time
int val = gen_num(IO_BLOCK_RANGE, IO_BLOCK_OFF);
tmp_p.set_blocked_time(val);
// Add the process to the list of blocked processes
blocked_procs.add(tmp_p);
}
// Set the spot in the current proc to null
curr_procs.set(i,null);
// Populate with the next process
get_next_procs(time);
// If the current process isn't null
if(curr_procs.get(i) != null) {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp_p.get_pid() + " for process ID " + curr_procs.get(i).get_pid() + ")");
context_time.set(i, new Integer(4));
} else {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp_p.get_pid() + " with no process to replace it)");
context_time.set(i, new Integer(2));
}
}
} else {
if(curr_procs.get(i) == null) {
get_next_procs(time);
if(curr_procs.get(i) != null) {
context_time.set(i, context_time.get(i) + 2);
if(curr_procs.get(i).is_interactive()) {
System.out.println("[time " + time + "ms] Interactive process ID " + curr_procs.get(i).get_pid() + " has taken unused cpu, " + (i+1));
} else {
System.out.println("[time " + time + "ms] CPU-Bound process ID " + curr_procs.get(i).get_pid() + " has taken unused cpu, " + (i+1));
}
}
}
}
}
}
/**
* @effect runs the algorithm
*/
@Override
public void exec() {
System.out.println("---- Executing SRT ----");
print_ready_entry();
int time = 0;
// CPU queue
curr_procs = new ArrayList<Process>();
// Blocked queue
blocked_procs = new ArrayList<Process>();
ArrayList<Process> all_procs = new ArrayList<Process>(procs);
// Load the next processes into the current ones
get_next_procs(time);
// Used to keep track of context switches
ArrayList<Integer> context_time = new ArrayList<Integer>();
// Sets up the context switch
setup_context_cpu(context_time);
// Set all of them to enter at time 0
set_start_ready();
// Start up each of the processes
for(int i = 0; i < curr_procs.size(); i++){
if (curr_procs.get(i) != null) {
curr_procs.get(i).set_wait(time);
curr_procs.get(i).activate_burst();
}
}
// Go until all the CPU-bound processes are finished (6 bursts)
while(!super.should_stop()){
time++;
dec_context_switch(context_time, time);
burst_context_handle(context_time, time);
handle_blocked_processes(time);
remove_finished_procs(time);
}
display_data(all_procs, time);
}
}