SJF Scheduling Assignment Updated
SJF Scheduling Assignment Updated
AP22110010136
DATE 19-09-2024
Answer:
Shortest Job First (SJF) is a scheduling algorithm that assigns the CPU to the process with
the smallest burst time next. This method can be either preemptive or non-preemptive. In
non-preemptive SJF, the CPU is assigned to the shortest job, and it runs until it finishes.
Preemptive SJF, also known as Shortest Remaining Time First (SRTF), allows the currently
executing process to be preempted if a new process arrives with a shorter burst time. SJF
minimizes the average waiting time compared to other algorithms. However, it can lead to
starvation if shorter processes continuously enter the queue, preventing longer ones from
executing.
Code:
#include <iostream>
#include <algorithm>
using namespace std;
struct Process {
int pid;
int bt;
int wt;
int tat;
};
proc[0].wt = 0;
for (int i = 1; i < n; i++) {
proc[i].wt = proc[i - 1].wt + proc[i - 1].bt;
}
cout << "\nAverage Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}
Sample Output:
Enter number of processes: 3
Enter burst time of process 1: 6
Enter burst time of process 2: 8
Enter burst time of process 3: 7