- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathEDFScheduling.java
99 lines (80 loc) · 2.98 KB
/
EDFScheduling.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
packagecom.thealgorithms.scheduling;
importjava.util.ArrayList;
importjava.util.Comparator;
importjava.util.List;
/**
* The Earliest Deadline First (EDF) Scheduling class implements a dynamic scheduling algorithm.
* It assigns the CPU to processes with the earliest deadlines, ensuring that deadlines are met if possible.
* This scheduling algorithm is ideal for real-time systems where meeting deadlines is critical.
*/
publicfinalclassEDFScheduling {
privateEDFScheduling() {
}
privateList<Process> processes;
/**
* Constructs an EDFScheduling object with a list of processes.
*
* @param processes List of processes to be scheduled.
*/
publicEDFScheduling(finalList<Process> processes) {
this.processes = processes;
}
/**
* Schedules the processes using Earliest Deadline First (EDF) scheduling.
* Processes are sorted by their deadlines, and the method simulates their execution.
* It calculates the waiting time and turnaround time for each process.
*
* @return List of processes after they have been executed in order of earliest deadline first.
*/
publicList<Process> scheduleProcesses() {
processes.sort(Comparator.comparingInt(Process::getDeadline));
intcurrentTime = 0;
List<Process> executedProcesses = newArrayList<>();
for (Processprocess : processes) {
process.setWaitingTime(currentTime);
currentTime += process.getBurstTime();
process.setTurnAroundTime(process.getWaitingTime() + process.getBurstTime());
if (currentTime > process.getDeadline()) {
System.out.println("Warning: Process " + process.getProcessId() + " missed its deadline.");
}
executedProcesses.add(process);
}
returnexecutedProcesses;
}
/**
* The Process class represents a process with an ID, burst time, deadline, waiting time, and turnaround time.
*/
publicstaticclassProcess {
privateStringprocessId;
privateintburstTime;
privateintdeadline;
privateintwaitingTime;
privateintturnAroundTime;
publicProcess(StringprocessId, intburstTime, intdeadline) {
this.processId = processId;
this.burstTime = burstTime;
this.deadline = deadline;
}
publicStringgetProcessId() {
returnprocessId;
}
publicintgetBurstTime() {
returnburstTime;
}
publicintgetDeadline() {
returndeadline;
}
publicintgetWaitingTime() {
returnwaitingTime;
}
publicvoidsetWaitingTime(intwaitingTime) {
this.waitingTime = waitingTime;
}
publicintgetTurnAroundTime() {
returnturnAroundTime;
}
publicvoidsetTurnAroundTime(intturnAroundTime) {
this.turnAroundTime = turnAroundTime;
}
}
}