import [Link].
*;
public class Main {
// ---------- FCFS ----------
static void FCFS() {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of processes: ");
int n = [Link]();
int bt[] = new int[n];
int at[] = new int[n];
int wt[] = new int[n];
int tat[] = new int[n];
int ft[] = new int[n];
float awt = 0, atat = 0;
[Link]("Enter Burst time for each process:");
for (int i = 0; i < n; i++) {
[Link]("Process[" + (i + 1) + "]: ");
bt[i] = [Link]();
}
[Link]("Enter Arrival time for each process:");
for (int i = 0; i < n; i++) {
[Link]("Process[" + (i + 1) + "]: ");
at[i] = [Link]();
}
ft[0] = at[0] + bt[0];
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = [Link](0, ft[i - 1] - at[i]);
ft[i] = at[i] + wt[i] + bt[i];
awt += wt[i];
}
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
atat += tat[i];
}
[Link]("\nProcess\tBurst\tWaiting\tTurnaround");
for (int i = 0; i < n; i++) {
[Link]("P" + (i + 1) + "\t" + bt[i] + "\t" +
wt[i] + "\t" + tat[i]);
}
[Link]("Average Waiting Time: %.2f\n", awt / n);
[Link]("Average Turnaround Time: %.2f\n", atat /
n);
}
// ---------- Non-Preemptive SJF ----------
static void SJF() {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of processes: ");
int n = [Link]();
int bt[] = new int[n];
int at[] = new int[n];
int wt[] = new int[n];
int tat[] = new int[n];
boolean completed[] = new boolean[n];
[Link]("Enter Burst time for each process:");
for (int i = 0; i < n; i++) {
[Link]("Process[" + (i + 1) + "]: ");
bt[i] = [Link]();
}
[Link]("Enter Arrival time for each process:");
for (int i = 0; i < n; i++) {
[Link]("Process[" + (i + 1) + "]: ");
at[i] = [Link]();
}
int currentTime = 0;
float awt = 0, atat = 0;
for (int count = 0; count < n; count++) {
int idx = -1;
int minBT = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!completed[i] && at[i] <= currentTime && bt[i] <
minBT) {
minBT = bt[i];
idx = i;
}
}
if (idx == -1) {
currentTime++;
count--;
continue;
}
wt[idx] = currentTime - at[idx];
currentTime += bt[idx];
tat[idx] = wt[idx] + bt[idx];
completed[idx] = true;
awt += wt[idx];
atat += tat[idx];
}
[Link]("\nProcess\tBurst\tWaiting\tTurnaround");
for (int i = 0; i < n; i++) {
[Link]("P" + (i + 1) + "\t" + bt[i] + "\t" +
wt[i] + "\t" + tat[i]);
}
[Link]("Average Waiting Time: %.2f\n", awt / n);
[Link]("Average Turnaround Time: %.2f\n", atat /
n);
}
// ---------- Non-Preemptive Priority ----------
static void PriorityScheduling() {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of processes: ");
int n = [Link]();
int bt[] = new int[n];
int priority[] = new int[n];
int wt[] = new int[n];
int tat[] = new int[n];
int p[] = new int[n];
[Link]("Enter Burst time and Priority (higher
number = higher priority) for each process:");
for (int i = 0; i < n; i++) {
[Link]("Process[" + (i + 1) + "] - Burst,
Priority: ");
bt[i] = [Link]();
priority[i] = [Link]();
p[i] = i + 1;
}
// Sort by priority descending
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (priority[i] < priority[j]) {
int temp = priority[i]; priority[i] =
priority[j]; priority[j] = temp;
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
wt[0] = 0;
tat[0] = bt[0];
float awt = 0, atat = tat[0];
for (int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
tat[i] = wt[i] + bt[i];
awt += wt[i];
atat += tat[i];
}
[Link]("\nProcess\tBurst\tWaiting\tTurnaround\tPriority")
;
for (int i = 0; i < n; i++) {
[Link]("P" + p[i] + "\t" + bt[i] + "\t" +
wt[i] + "\t" + tat[i] + "\t\t" + priority[i]);
}
[Link]("Average Waiting Time: %.2f\n", awt / n);
[Link]("Average Turnaround Time: %.2f\n", atat /
n);
}
// ---------- Main Menu ----------
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Process Scheduling Algorithms");
[Link]("1. FCFS");
[Link]("2. Non-preemptive SJF");
[Link]("3. Non-preemptive Priority");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1: FCFS(); break;
case 2: SJF(); break;
case 3: PriorityScheduling(); break;
default: [Link]("Invalid choice");
}
}
}
OUTPUT: