Skip to content

Commit 8235fe9

Browse files
committed
implementazione scheduler
1 parent def3d75 commit 8235fe9

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package utils;
2+
3+
public class Process {
4+
String name;
5+
int arrivalTime;
6+
int cpuBurst;
7+
8+
public Process(String name, int arrivalTime, int cpuBurst) {
9+
this.name = name;
10+
this.arrivalTime = arrivalTime;
11+
this.cpuBurst = cpuBurst;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return String.format("%s: %d CPU bursts remaining", name, cpuBurst);
17+
}
18+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package utils;
2+
3+
import java.util.*;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public abstract class Scheduler {
7+
protected List<Process> processes;
8+
protected Queue<Process> readyQueue = new LinkedList<>();
9+
protected Process currentProcess = null;
10+
protected int currentTime = -1;
11+
12+
// Main scheduling method to be called by all subclasses
13+
public void schedule(List<Process> processes) {
14+
this.processes = processes;
15+
this.readyQueue = initializeQueue(); // Let subclasses decide the queue type
16+
17+
while (!processes.isEmpty() || !readyQueue.isEmpty() || currentProcess != null) {
18+
currentTime++;
19+
20+
// Add newly arrived processes to the ready queue
21+
Iterator<Process> processIterator = processes.iterator();
22+
while (processIterator.hasNext()) {
23+
Process p = processIterator.next();
24+
if (p.arrivalTime == currentTime) {
25+
readyQueue.add(p);
26+
System.out.println(currentTime + ": New process in queue: " + p);
27+
processIterator.remove(); // Remove from original list once added to the queue
28+
}
29+
}
30+
31+
// Select the next process to run based on the specific scheduler logic
32+
if (shouldSwitchProcess()) {
33+
if (currentProcess != null && currentProcess.cpuBurst > 0) {
34+
readyQueue.add(currentProcess); // Add the current process back if not finished
35+
}
36+
currentProcess = selectNextProcess(); // Let subclasses select the next process
37+
resetQuantumOrCounters();
38+
}
39+
40+
// Execute the selected process
41+
if (currentProcess != null) {
42+
executeBurst(currentProcess);
43+
System.out.printf("(%d) Executing %s\n", currentTime, currentProcess);
44+
} else {
45+
System.out.printf("(%d) No process is running\n", currentTime);
46+
}
47+
48+
waitOneSecond(); // Simulate the passage of time
49+
}
50+
}
51+
52+
protected abstract Process selectNextProcess();
53+
54+
protected abstract boolean shouldSwitchProcess();
55+
56+
protected void resetQuantumOrCounters() {}
57+
58+
protected void executeBurst(Process process) {
59+
if (process != null) {
60+
process.cpuBurst--;
61+
}
62+
}
63+
64+
protected Queue<Process> initializeQueue() {
65+
return new LinkedList<>();
66+
}
67+
68+
private void waitOneSecond() {
69+
try {
70+
TimeUnit.SECONDS.sleep(1);
71+
} catch (InterruptedException e) {
72+
e.printStackTrace();
73+
}
74+
}
75+
76+
public void test1() {
77+
List<Process> p = new ArrayList<>();
78+
p.add(new Process("P1", 0, 20));
79+
p.add(new Process("P2", 4, 15));
80+
p.add(new Process("P3", 8, 9));
81+
schedule(p);
82+
}
83+
84+
public void test2() {
85+
List<Process> p = new ArrayList<>();
86+
p.add(new Process("P1", 0, 17));
87+
p.add(new Process("P2", 3, 14));
88+
p.add(new Process("P3", 4, 10));
89+
p.add(new Process("P4", 9, 4));
90+
p.add(new Process("P5", 11, 12));
91+
schedule(p);
92+
}
93+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package utils;
2+
3+
import java.util.Queue;
4+
import java.util.LinkedList;
5+
6+
public class SchedulerRR extends Scheduler {
7+
8+
private final int quantum;
9+
private int timeSliceCounter = 0;
10+
11+
public SchedulerRR(int quantum) {
12+
this.quantum = quantum;
13+
}
14+
15+
@Override
16+
protected boolean shouldSwitchProcess() {
17+
return currentProcess == null || timeSliceCounter == quantum || currentProcess.cpuBurst == 0;
18+
}
19+
20+
@Override
21+
protected Process selectNextProcess() {
22+
timeSliceCounter = 0;
23+
return readyQueue.poll();
24+
}
25+
26+
@Override
27+
protected void resetQuantumOrCounters() {
28+
timeSliceCounter = 0; // Reset the counter for RR
29+
}
30+
31+
public static void main(String[] args) {
32+
Scheduler scheduler = new SchedulerRR(8);
33+
scheduler.test2();
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package utils;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.Comparator;
5+
import java.util.Queue;
6+
7+
public class SchedulerSJFPreemptive extends Scheduler {
8+
9+
@Override
10+
protected Queue<Process> initializeQueue() {
11+
// Use a priority queue to always select the process with the shortest remaining burst
12+
return new PriorityQueue<>(Comparator.comparingInt(p -> p.cpuBurst));
13+
}
14+
15+
@Override
16+
protected boolean shouldSwitchProcess() {
17+
// Switch if current process finished or there's a process with a shorter remaining time
18+
return currentProcess == null || currentProcess.cpuBurst == 0 ||
19+
(!readyQueue.isEmpty() && readyQueue.peek().cpuBurst < currentProcess.cpuBurst);
20+
}
21+
22+
@Override
23+
protected Process selectNextProcess() {
24+
return readyQueue.poll();
25+
}
26+
27+
public static void main(String[] args) {
28+
Scheduler scheduler = new SchedulerSJFPreemptive();
29+
scheduler.test1();
30+
}
31+
}

0 commit comments

Comments
 (0)