Shortest Job First Program

Shortest Job First Program:

The Shortest Job First (SJF) scheduling algorithm is a non-preemptive scheduling algorithm used in computer operating systems to prioritize processes based on their burst time. In SJF, the scheduler selects the process with the shortest burst time from the ready queue for execution. This approach minimizes the average waiting time and turnaround time, leading to efficient resource utilization. The SJF algorithm is beneficial in scenarios where the burst times of processes are known in advance or can be accurately predicted. However, it may lead to starvation for long processes if short processes keep arriving continuously. Despite this limitation, SJF remains a popular scheduling algorithm due to its simplicity and effectiveness in reducing waiting times for processes.

#include<stdio.h>
void main()
{
int x,b[10], a[10],n,c1[10],sum=0,w[10],i,t,tt[10],index,s,bt[10],c2=0;
float avgw=0,avgt=0;
printf("Enter no of process\n");
scanf("%d",&n);
printf("Enter Burst Time\n");
for(x=0;x<=n-1;x++)
{
scanf("%d",&b[x]);
}
printf("Enter Arrival Time\n");
for(x=0;x<=n-1;x++)
{
scanf("%d",&a[x]);
}
for(x=0;x<=n-1;x++)
{
sum+=b[x];
c1[x]=0;  
bt[x]=b[x];
}
s=32767;
for(t=0;t<n;t++)
     {
  for(i=0;i<=a[i];i++)
 
      if(b[i]<s && b[i]>0 && a[i]<=t)
 
s=b[i];
index=i;
}
}
//b[index]=b[index]-1;
c1[index]=c1[index]+b[index];
//s=b[index];
if(c2==0)
w[index]=c1[index]-b[index]-a[index];
else
w[index]=c2-b[index]-a[index];
c2=c2+b[index];
b[index]=0;
     if (b[index]==0)
    {
s=32765;  b[index]=s;

    }
    }
printf("Waiting Time\n");
for(x=0;x<=n-1;x++)
{
printf("%d\n",w[x]);
tt[x]=bt[x]+w[x];
avgw+=w[x];
}
printf("Turn Around Time\n");
for(x=0;x<=n-1;x++)
{
printf("%d\n",tt[x]);
avgt+=tt[x];
}
printf("Avg Waiting Time = %f\n",avgw/n);
printf("Avg Turn Around Time = %f\n",avgt/n);
}

Post a Comment

0 Comments