Preemptive or Non-Preemptive.: Program For Shortest Job First (Or SJF) CPU Scheduling - Set 1 (Non-Preemptive)
Preemptive or Non-Preemptive.: Program For Shortest Job First (Or SJF) CPU Scheduling - Set 1 (Non-Preemptive)
Preemptive or Non-Preemptive.: Program For Shortest Job First (Or SJF) CPU Scheduling - Set 1 (Non-Preemptive)
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process
with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can
be preemptive or non-preemptive.
Shortest Job first has the advantage of having a minimum average waiting time among
all scheduling algorithms.
It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can be solved using
the concept of ageing.
It is practically infeasible as Operating System may not know burst times and therefore may
not sort them. While it is not possible to predict execution time, several methods can be
used to estimate the execution time for a job, such as a weighted average of previous
execution times.
SJF can be used in specialized environments where accurate estimates of running time are
available.
Algorithm:
Then select that process that has minimum arrival time and minimum Burst time.
After completion of the process make a pool of processes that arrives afterward till the
completion of the previous process and select that process among the pool which is having
minimum Burst time.
How to compute below times in SJF using a program?
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Advantages of SJF:
SJF is better than the First come first serve(FCFS) algorithm as it reduces the average waiting
time.
It is suitable for the jobs running in batches, where run times are already known.
Disadvantages of SJF:
In SJF job completion time must be known earlier, but sometimes it is hard to predict.
It leads to the starvation that does not reduce average turnaround time.
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
index = i;
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
A[i][2] = 0;
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
// data.
total += A[i][3];
}
avg_tat = (float)total / n;