Shortest Job First Scheduling Algorithm
Shortest job first (SJF) or shortest job straight away, is a planning strategy that chooses the holding up process with the littlest execution time to execute straight away.Shortest Job First is a non-preemptive algorithm. Shortest Job first has the benefit of having the least average holding up time among all booking algorithms. It is a Greedy Algorithm. It might cause starvation if shorter procedures continue coming. This issue can be understood by utilizing the idea of aging. It is essentially infeasible as Operating System may not realize burst time and thusly may not sort them. While it is preposterous to expect to anticipate execution time, a few strategies can be utilized to assess the execution time for a job, for example, a weighted average of past execution times. SJF can be utilized in specific situations where exact evaluations of running time are accessible.
Shortest job first Algorithm:
1: Sort every one of the procedures in expanding request as indicated by burst time. 2: Then essentially, apply FCFS.
Shortest job first program in C++
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct process
{
int id;
int burst_time;
int wait_time;
int ta_time;
};
int main()
{
int n,total_wait=0,total_ta=0,j;
cout<<"Enter the number of process : ";
cin>>n;
process p[n],temp;
cout<<"Enter Burst Time"<<endl;
for(int i=0;i<n;i++)
{
cout<<"Process "<<i+1<<" : ";
cin>>temp.burst_time;
temp.id = i+1;
j = i;
while(j>0 && temp.burst_time < p[j-1].burst_time)
{
p[j] = p[j-1];
j--;
}
p[j] = temp;
}
for(int i=0;i<n;i++)
{
p[i].wait_time = total_wait;
p[i].ta_time = p[i].wait_time + p[i].burst_time;
total_wait += p[i].burst_time;
total_ta += p[i].ta_time;
}
cout<<"\nGANTT CHART"<<endl;
for(int i=0;i<n;i++)
{
cout<<setw(p[i].burst_time + 1);
cout<<left<<p[i].id;
}
cout<<right;
cout<<endl;
for(int i=0;i<n;i++)
{
cout<<setw(p[i].burst_time + 1)<<setfill('-')<<"|";
}
cout<<endl<<0<<setfill(' ');
for(int i=1;i<n;i++)
{
cout<<setw(p[i-1].burst_time + 1)<<p[i].wait_time;
}
cout<<setw(p[n-1].burst_time + 1)<<total_wait<<endl;
cout<<"\nProcess ID\tBurst Time\tWait Time\tTurnaround Time"<<endl;
for(int i=0;i<n;i++)
{
cout<<p[i].id<<"\t\t"<<p[i].burst_time<<"\t\t"<<p[i].wait_time<<"\t\t"<<p[i].ta_time<<endl;
}
cout<<"\nTotal Wait Time : "<<total_wait;
cout<<"\nAverage Wait Time : "<<(float)total_wait/n;
cout<<"\nTotal Turnaround Time : "<<total_ta;
cout<<"\nAverage Turnaround Time : "<<(float)total_ta/n;
return 0;
}
OUTPUT
Enter the number of process: 4
Enter Burst Time
Process 1: 5
Process 2: 3
Process 3: 3
Process 4: 6
GANTT CHART
2 3 1 4
---|---|-----|------|
0 3 6 11 17
Process ID Burst Time Wait Time Turnaround Time
2 3 0 3
3 3 3 6
1 5 6 11
4 6 11 17
Total Wait Time: 17
Average Wait Time: 4.25
Total Turnaround Time: 37
Average Turnaround Time: 9.25
You must be also searching for these programming languages :
You must be also searching for these programming languages :
- First Come First Serve Scheduling Algorithm
- Selection Sort Program in C++
- Linked List Program in C++
tags: Shortest Job First, Shortest job first scheduling algorithm, shortest job first algorithm in C++.