-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRR.java
More file actions
90 lines (77 loc) · 2.1 KB
/
RR.java
File metadata and controls
90 lines (77 loc) · 2.1 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
/** -----------------------------------------------------------------------
RR.java
@author William Clift
Operating Systems
Ursinus College
Project 2 - Scheduling Schemes
14 April 2020
Compile Instructions:
Compile: javac RR.java
------------------------------------------------------------------- **/
import java.util.*;
public class RR extends Scheme{
public String scheme = "RR";
public String fileName;
public CircularLL toSchedule;
public CircularLL incoming;
public int time_quantum;
public CircularLL processed;
/**
* Round Robin Algorithm
* @param toSchedule the Circular Linked List of PCBs
* @param time_quantum the amount of time alloted per process.
*/
public RR(CircularLL incoming, String scheme, int time_quantum){
super(incoming, scheme);
this.toSchedule = new CircularLL();
this.incoming = incoming;
this.time_quantum = time_quantum;
this.processed = new CircularLL();
}
/**
* Runs the Round-Robin Algorithm
*
*/
public void run(){
System.out.println("============================================================");
boolean done = false; // Sentinal Value
while(!done){
if(incoming.getSize() > 0){
checkArrival();
}
PCB current = toSchedule.head;
if(toSchedule.getSize()>0){ // If there are processes left
if(current.time_remaining > this.time_quantum){
PCB next = toSchedule.work();
cpuProcess(next, this.time_quantum, toSchedule, incoming); // Run the next Process in line
}else{
PCB next = toSchedule.pop();
PCB result = cpuProcess(next, next.time_remaining, toSchedule, incoming);
processed.push(result);
}
} else{
cpuTick();
}
if(incoming.getSize() < 1 && toSchedule.getSize() < 1){
done = true;
}
}
printEndMetrics(processed);
}
/**
* Check if any processes have arrived.
*
*/
private void checkArrival(){
PCB current = incoming.head;
for(int i = 0; i < incoming.getSize(); i++){
current = incoming.head;
if(current.arrival_time == cpuTime){
PCB in = incoming.pop();
toSchedule.push(in);
}else{
incoming.work();
}
}
}
}