Professional Documents
Culture Documents
Out of all the available processes, CPU is assigned to the process having the smallest burst time.
3-The preemptive mode of Shortest Job First is called the Shortest Remaining Time First(SRTF).
int main()
int n,temp,tt=0,min,d,i,j;
float atat=0,awt=0,stat=0,swt=0;
cout<<"enter no of process"<<endl;
cin>>n;
int a[n],b[n],e[n],tat[n],wt[n];
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cin>>b[i];
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(b[i]>b[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
min=a[0];
for(i=0;i<n;i++)
if(min>a[i])
min=a[i];
d=i;
tt=min;
e[d]=tt+b[d];
tt=e[d];
for(i=0;i<n;i++)
if(a[i]!=min)
e[i]=b[i]+tt;
tt=e[i];
for(i=0;i<n;i++)
tat[i]=e[i]-a[i];
stat=stat+tat[i];
wt[i]=tat[i]-b[i];
swt=swt+wt[i];
atat=stat/n;
awt=swt/n;
for(i=0;i<n;i++)
}
Output:
enter no of process
P1 3 1 3 4
P2 4 2 3 5
P3 2 3 7 10
P4 1 4 11 15
P5 0 6 0 6
awt=4.8 atat=8
It is simple, easy to implement, and starvation-free as all processes get fair share of CPU.
It is preemptive as processes are assigned CPU only for a fixed slice of time at most.
// processes
// burst times.
int rem_bt[n];
rem_bt[i] = bt[i];
while (1)
t += quantum;
// by quantum
rem_bt[i] -= quantum;
else
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
if (done == true)
break;
// bt[i] + wt[i]
int quantum)
<< " Waiting time " << " Turn around time\n";
// Calculate total waiting time and total turn
// around time
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
// Driver code
int main()
// process id's
// Time quantum
int quantum = 2;
return 0;
}
Output:
Processes Burst time Waiting time Turn around time
1 10 13 23
2 5 10 15
3 8 13 21
Average waiting time = 12
Average turn around time = 19.6667
The Comparison between Shortest Job First (SJF) and Round-Robin (RR)
scheduling algorithm are as follows:
Shortest Job First (SJF) Round-Robin (RR)
Shortest Job First (SJF) executes the processes Round-Robin (RR) executes the processes based
based upon their burst time i.e. in ascending order upon the time quantum defined i.e. each process is
of their burst times. executed for a fixed amount of time.
The average waiting time for given set of processes The average waiting time for given set of
is minimum. processes is quite small and depends on the time
quantum.
The real difficulty with SJF is knowing the length of It is quite easy to implement RR.
the next CPU request or burst.
A long process may never get executed and the Each process is executed and every user feels that
system may keep executing the short processes. his work is being done as the CPU gives equal
amount of time to each process.
In case of SJF, elapsed time should be recorded, In case of RR, if the time quantum is very small
results in more overhead on the processor. then context switch takes place again and again
after very short intervals of time which leads to
overhead.