0% found this document useful (0 votes)
29 views3 pages

Shortest Job First

Shortest Job First (SJF) is a scheduling algorithm that selects the process with the shortest burst time to execute next. The code implements SJF by first sorting processes by burst time. It then calculates waiting times, turnaround times, and average waiting and turnaround times. The output displays the process details including burst time, waiting time, and turnaround time for each process.

Uploaded by

M bashir rehan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Shortest Job First

Shortest Job First (SJF) is a scheduling algorithm that selects the process with the shortest burst time to execute next. The code implements SJF by first sorting processes by burst time. It then calculates waiting times, turnaround times, and average waiting and turnaround times. The output displays the process details including burst time, waiting time, and turnaround time for each process.

Uploaded by

M bashir rehan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Shortest Job First:

Code:
#include<iostream>
using namespace std;
int main()
{
int burst_time[20],process[20],waiting_time[20],tat[20],i,j,n,total=0,pos,temp;
float wait_avg,TAT_avg;
cout<<"Enter number of process: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Burst time for process "<<i+1<<" : ";
cin>>burst_time[i];
process[i]=i+1; //Process Number
}
//Sorting
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(burst_time[j]<burst_time[pos])
pos=j; }
temp=burst_time[i];
burst_time[i]=burst_time[pos];
burst_time[pos]=temp;

temp=process[i];
process[i]=process[pos];
process[pos]=temp;
}
//First process has 0 waiting time
waiting_time[0]=0;
//calculate waiting time
for(i=1;i<n;i++)
{
waiting_time[i]=0;
for(j=0;j<i;j++)
waiting_time[i]+=burst_time[j];

total+=waiting_time[i];
}
//Calculating Average waiting time
wait_avg=(float)total/n;
total=0;
cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";
for(i=0;i<n;i++)
{
tat[i]=burst_time[i]+waiting_time[i]; //Calculating Turnaround Time
total+=tat[i];
cout<<"\n"<<process[i]<<"\t\t"<<burst_time[i]<<"\t\t"<<waiting_time[i]<<"\t\t"<<tat[i];
}
//Calculation of Average Turnaround Time
TAT_avg=(float)total/n;
cout<<"\n\nAverage Waiting Time: "<<wait_avg;
cout<<"\nAverage Turnaround Time: "<<TAT_avg;
}

Output:

You might also like