Problem No: 02
Name of the problem: SJF CPU Scheduling
Algorithm
Theory:
Shortest job next (SJN),
also known as the shortest job first (SJF) or shortest process next (SPN), is the scheduling policy that selects for execution the waiting process with the
smallest execution time. SJN is a non-preemptive algorithm.
Code:
#include<stdio.h>
main(){
int wt[10],tt[10],bt[10],n,i,j,temp;
float wavg,tavg;
printf(" the number of process:
");
scanf ("%d",&n);
for(i=0;i<n;i++)
{
printf("\n the burst time %d: ",i);
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
}
wt[0]=wavg=0;
tt[0]=tavg=bt[0];
for(i=1;i<n;i++){
wt[i]=wt[i-1]+bt[i-1];
tt[i]=wt[i]+bt[i];
wavg=wavg+wt[i];
tavg=tavg+tt[i];
}
printf("\n
process burst waiting
turnaround");
for(i=0;i<n;i++)
printf("\n%d %d
%d
%d",i,bt[i],wt[i],tt[i]);
printf("\n\n
avg time: %f",wavg/n);
printf("\n
turnavg time: %f",tavg/n);
}
OUTPUT:
No comments:
Post a Comment