Skip to content

Commit c8252ea

Browse files
Add Preemptive & Non-Preemptive Scheduling Algorithm
1 parent 3eb521b commit c8252ea

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import java.util.*;
2+
3+
class SchedulingAlgorithms
4+
{
5+
6+
static class Process
7+
{
8+
int pid;
9+
int priority;
10+
int arrivalTime;
11+
int burstTime;
12+
int remainingTime;
13+
int completionTime;
14+
int waitingTime;
15+
int turnaroundTime;
16+
int responseTime;
17+
boolean completed;
18+
19+
Process(int pid, int priority, int arrivalTime, int burstTime)
20+
{
21+
this.pid = pid;
22+
this.priority = priority;
23+
this.arrivalTime = arrivalTime;
24+
this.burstTime = burstTime;
25+
this.remainingTime = burstTime;
26+
this.completed = false;
27+
}
28+
}
29+
30+
public static void main(String[] args)
31+
{
32+
Scanner sc = new Scanner(System.in);
33+
34+
System.out.print("Enter number of processes: ");
35+
int n = sc.nextInt();
36+
37+
Process[] processes = new Process[n];
38+
for (int i = 0; i < n; i++)
39+
{
40+
System.out.println("Process " + (i + 1));
41+
System.out.print("Enter arrival time: ");
42+
int at = sc.nextInt();
43+
System.out.print("Enter burst time: ");
44+
int bt = sc.nextInt();
45+
System.out.print("Enter priority (integer): ");
46+
int priority = sc.nextInt();
47+
processes[i] = new Process(i + 1, priority, at, bt);
48+
}
49+
50+
System.out.println("Choose the scheduling algorithm:");
51+
System.out.println("1. Non-Preemptive");
52+
System.out.println("2. Preemptive");
53+
System.out.print("Enter your choice (1 or 2): ");
54+
int choice = sc.nextInt();
55+
56+
switch (choice)
57+
{
58+
case 1:
59+
nonPreemptive(processes);
60+
break;
61+
case 2:
62+
preemptive(processes);
63+
break;
64+
default:
65+
System.out.println("Invalid choice.");
66+
return;
67+
}
68+
}
69+
70+
private static void nonPreemptive(Process[] processes)
71+
{
72+
int n = processes.length;
73+
int completed = 0;
74+
int currentTime = 0;
75+
boolean[] isCompleted = new boolean[n];
76+
77+
while (completed != n)
78+
{
79+
int idx = -1;
80+
int minBurst = Integer.MAX_VALUE;
81+
for (int i = 0; i < n; i++)
82+
{
83+
if ((processes[i].arrivalTime <= currentTime) && !isCompleted[i])
84+
{
85+
if (processes[i].burstTime < minBurst)
86+
{
87+
minBurst = processes[i].burstTime;
88+
idx = i;
89+
}
90+
if (processes[i].burstTime == minBurst)
91+
{
92+
if (processes[i].priority < processes[idx].priority)
93+
{
94+
idx = i;
95+
}
96+
}
97+
}
98+
}
99+
100+
if (idx != -1)
101+
{
102+
processes[idx].waitingTime = currentTime - processes[idx].arrivalTime;
103+
currentTime += processes[idx].burstTime;
104+
processes[idx].completionTime = currentTime;
105+
processes[idx].turnaroundTime = processes[idx].completionTime - processes[idx].arrivalTime;
106+
processes[idx].responseTime = processes[idx].waitingTime;
107+
isCompleted[idx] = true;
108+
completed++;
109+
}
110+
else
111+
{
112+
currentTime++;
113+
}
114+
}
115+
116+
printResults(processes, "Non-Preemptive SJF (Shortest Job First) Scheduling");
117+
}
118+
119+
private static void preemptive(Process[] processes)
120+
{
121+
int n = processes.length;
122+
int completed = 0;
123+
int currentTime = 0;
124+
int minRemainingTime;
125+
int shortest = -1;
126+
boolean check = false;
127+
128+
int[] waitingTime = new int[n];
129+
int[] turnaroundTime = new int[n];
130+
int[] completionTime = new int[n];
131+
int[] responseTime = new int[n];
132+
133+
int[] remainingTime = new int[n];
134+
for (int i = 0; i < n; i++)
135+
{
136+
remainingTime[i] = processes[i].burstTime;
137+
}
138+
139+
while (completed != n)
140+
{
141+
minRemainingTime = Integer.MAX_VALUE;
142+
shortest = -1;
143+
check = false;
144+
145+
for (int i = 0; i < n; i++)
146+
{
147+
if ((processes[i].arrivalTime <= currentTime) &&
148+
(remainingTime[i] < minRemainingTime) && ( remainingTime[i] > 0))
149+
{
150+
minRemainingTime = remainingTime[i];
151+
shortest = i;
152+
check = true;
153+
}
154+
}
155+
156+
if (!check)
157+
{
158+
currentTime++;
159+
continue;
160+
}
161+
162+
if (responseTime[shortest] == 0)
163+
{
164+
responseTime[shortest] = currentTime - processes[shortest].arrivalTime;
165+
}
166+
167+
remainingTime[shortest]--;
168+
currentTime++;
169+
170+
if (remainingTime[shortest] == 0)
171+
{
172+
completed++;
173+
completionTime[shortest] = currentTime;
174+
turnaroundTime[shortest] = completionTime[shortest] - processes[shortest].arrivalTime;
175+
waitingTime[shortest] = turnaroundTime[shortest] - processes[shortest].burstTime;
176+
if (waitingTime[shortest] < 0)
177+
{
178+
waitingTime[shortest] = 0;
179+
}
180+
}
181+
}
182+
183+
for (int i = 0; i < n; i++)
184+
{
185+
processes[i].completionTime = completionTime[i];
186+
processes[i].turnaroundTime = turnaroundTime[i];
187+
processes[i].waitingTime = waitingTime[i];
188+
processes[i].responseTime = responseTime[i];
189+
}
190+
191+
printResults(processes, "Preemptive SRTF (Shortest Remaining Time First) Scheduling");
192+
}
193+
194+
private static void printResults(Process[] processes, String title)
195+
{
196+
System.out.println("\n" + title);
197+
System.out.printf("%-10s%-10s%-15s%-15s%-17s%-15s%-17s%-15s\n",
198+
"Process", "Priority", "Arrival Time", "Burst Time",
199+
"Completion Time", "Waiting Time", "Turnaround Time", "Response Time\n");
200+
201+
double totalWT = 0, totalTAT = 0;
202+
203+
Arrays.sort(processes, (a, b) -> a.pid - b.pid);
204+
205+
for (Process p : processes)
206+
{
207+
totalWT += p.waitingTime;
208+
totalTAT += p.turnaroundTime;
209+
System.out.printf("%-10d%-10d%-15d%-15d%-17d%-15d%-17d%-15d\n",
210+
p.pid, p.priority, p.arrivalTime, p.burstTime,
211+
p.completionTime, p.waitingTime, p.turnaroundTime, p.responseTime);
212+
}
213+
214+
System.out.printf("Average Waiting Time: %.2f\n", totalWT / processes.length);
215+
System.out.printf("Average Turnaround Time: %.2f\n", totalTAT / processes.length);
216+
}
217+
}

0 commit comments

Comments
 (0)