Advantages of SJF: Machine Learning Algorithms
Advantages of SJF: Machine Learning Algorithms
on the principles of Greedy Algorithms. The key idea is to allocate the CPU to the process with the
smallest burst time so that the CPU seems to be more responsive. Burst time is the amount of time
required by a process for its execution on the CPU.
It is a Non-Preemptive CPU Scheduling algorithm. We have provided an implementation of SJF
algorithm in C++ as well.
It is difficult to predict the burst time so it is difficult to implement this algorithm. In real systems,
different approaches can be taken to predict burst time and some systems have been found to be
using Machine Learning algorithms to predict it based on previous usage of the system.
Advantages of SJF
• Maximum throughput.
Throughput means total number of processes executed per unit time. Shortest job first scheduling
algorithm selects the waiting process with the smallest execution time. Thus, in SLF, shortest jobs
are executed first making the CPU utilization maximum. So, maximum number of tasks are
completed.
• Minimum waiting and turn around time as compared with other scheduling algorithms.
Disadvantages of SJF
• It may cause starvation if shorter process keeps coming.
In case process with lower burst time appears before process with higher burst time then only the
starvation may occur, since the algorithm will always choose the process with lowest Burst Time,
the process with higher Burst Time will never be able to get the share of CPU.
• Very hard to implement as Operating System cannot calculate the burst time of every
process and will not be able to sort the processes.
Algorithm of SJF
1. Sort all the processes according to their arrival time.
2. Select the process with minimum arrival time as well as minimum burst time.
3. After completion of the process, select from the ready queue the process which has the
minimum burst time.
4. Repeat above processes untill all processes have finished their execution.
Explaning procedure as per SJF algorithm
Process Arrival time Burst time Waiting time Turn around time
P1 1 7 0 7
P2 3 3 7 10
P3 6 2 2 4
P4 7 10 14 24
P5 9 8 4 12
Since arrival time of any process is not 0, there will be no execution or allocation of CPU from time
0 to 1.
Following the algorithm further, process having the least burst time among the available processes
will be executed. Till now, we have only one process in the ready queue hence the process will be
scheduled no matter what the burst time is.
The process will be executed till 8 units of time. Till then three more processes have arrived in the
ready queue therefore, the process with the lowest burst time will be choosen. P3 will be executed
next as it has lowest burst time. So that's how the process will go on in shortest job first (SJF)
scheduling algorithm
Combined Program for SJF CPU Scheduling (with Arrival
Time)
Following is the complete C++ code:
#include <iostream>
#include <stdlib.h>
for(int i=1;i<4;i++)
// Process from the seconf are iterated to till the last process in reached.
{
temp = completion_time[i-1];
int low = burst_time[i];
for(int j=i;j<4;j++)
{
if(temp >= arrival_time[j] && low >=burst_time[j])
// Completion time of previous process is compare with the arrival time of
current process as well the burst time is compared.
{
low = burst_time[j];
value = j;
}
}
completion_time[value] = temp + burst_time[value];
turn_around_time[value] = completion_time[value] - arrival_time[value];
waiting_time[value] = turn_around_time[value] - burst_time[value];
// Value of completion time, waiting time and turn around time is calculated
using formulae.
}
}
cout<<endl<<endl;
}
int main()
{
int process[4];
int burst_time[4];
int waiting_time[4];
int completion_time[4];
int turn_around_time[4];
int arrival_time[4];
input_burst_arrival_time(burst_time,process,arrival_time);
sort_arrival_time(arrival_time,process,burst_time);
sort_burst_time(arrival_time,process,burst_time);
sjf_operations(arrival_time,waiting_time,burst_time,turn_around_time,completion_
time);
print_table(process,burst_time,waiting_time,turn_around_time,arrival_time);
return 0;
}