-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobinScheduler.java
More file actions
120 lines (109 loc) · 3.61 KB
/
Copy pathRoundRobinScheduler.java
File metadata and controls
120 lines (109 loc) · 3.61 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
class RoundRobinScheduler {
private class ProcessNode {
int processId;
int burstTime;
int priority;
ProcessNode next;
ProcessNode(int processId, int burstTime, int priority) {
this.processId = processId;
this.burstTime = burstTime;
this.priority = priority;
this.next = null;
}
}
private ProcessNode head = null;
private ProcessNode tail = null;
private int timeQuantum;
public RoundRobinScheduler(int timeQuantum) {
this.timeQuantum = timeQuantum;
}
public void addProcess(int processId, int burstTime, int priority) {
ProcessNode newNode = new ProcessNode(processId, burstTime, priority);
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}
public void removeProcess(int processId) {
if (head == null) return;
if (head.processId == processId) {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
tail.next = head;
}
return;
}
ProcessNode temp = head;
do {
if (temp.next.processId == processId) {
temp.next = temp.next.next;
if (temp.next == head) {
tail = temp;
}
return;
}
temp = temp.next;
} while (temp != head);
}
public void simulateRoundRobin() {
if (head == null) {
System.out.println("No processes to schedule.");
return;
}
int currentTime = 0;
ProcessNode temp = head;
do {
if (temp.burstTime > 0) {
int execTime = Math.min(temp.burstTime, timeQuantum);
System.out.println("Executing Process ID: " + temp.processId + " for " + execTime + " units");
temp.burstTime -= execTime;
currentTime += execTime;
if (temp.burstTime == 0) {
removeProcess(temp.processId);
}
}
temp = temp.next;
} while (head != null && temp != head);
}
public void displayProcesses() {
if (head == null) {
System.out.println("No processes available.");
return;
}
ProcessNode temp = head;
do {
System.out.println("Process ID: " + temp.processId + ", Burst Time: " + temp.burstTime + ", Priority: " + temp.priority);
temp = temp.next;
} while (temp != head);
}
public static void main(String[] args) {
RoundRobinScheduler scheduler = new RoundRobinScheduler(4);
scheduler.addProcess(1, 8, 2);
scheduler.addProcess(2, 4, 1);
scheduler.addProcess(3, 9, 3);
scheduler.addProcess(4, 5, 2);
System.out.println("Initial Process Queue:");
scheduler.displayProcesses();
System.out.println("Starting Round Robin Scheduling:");
scheduler.simulateRoundRobin();
}
}
//SampleOutput
//Initial Process Queue:
//Process ID: 1, Burst Time: 8, Priority: 2
//Process ID: 2, Burst Time: 4, Priority: 1
//Process ID: 3, Burst Time: 9, Priority: 3
//Process ID: 4, Burst Time: 5, Priority: 2
//Starting Round Robin Scheduling:
//Executing Process ID: 1 for 4 units
//Executing Process ID: 2 for 4 units
//Executing Process ID: 3 for 4 units
//Executing Process ID: 4 for 4 units