SJF Shortest Job First Process Scheduling Algorithm In C Program

Ram Pothuraju
#include< stdio.h>
#include< conio.h>
void main()
{
 int njob,pID[15],aTime[15],bTime[15],jcTime[15],taTime[15],wTime[15],i,j,temp,sTime[15];
 float totalWait,totalTA;
 clrscr();

 printf("\n Enter number of process:");
 scanf("%d",&njob);

 for(i=0;i< njob;i++)
 {
  printf("\n Enter the Arrival time & Burst time for process[%d]:",i+1);
  scanf("%d %d",&aTime[i],&bTime[i]);

  pID[i]=i+1;
 }


 for(i=0;i< njob;i++)
 {
  for(j=i+1;j< njob;j++)
  {
   if(bTime[i]>bTime[j])
   {
    temp=pID[i];
    pID[i]=pID[j];
    pID[j]=temp;

    temp=aTime[i];
    aTime[i]=aTime[j];
    aTime[j]=temp;

    temp=bTime[i];
    bTime[i]=bTime[j];
    bTime[j]=temp;
   }
  }
 }

 for(i=0;i< njob;i++)
 {
  if(i==0)
  {
   sTime[0]=aTime[i];
  }
  else
  {
   sTime[i]=jcTime[i-1];

  }

  jcTime[i]=sTime[i]+bTime[i];
  taTime[i] = jcTime[i]-aTime[i];
  wTime[i] = taTime[i] - bTime[i];

 }

 printf("\n Process ArrivalTime   BurstTime    TurnaroundTime   WaitingTime");
 printf("\n_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+");

 for(i=0;i< njob;i++)
 {
  printf("\n %d \t\t%d    \t %d",pID[i],aTime[i],bTime[i]);
  printf("\t\t %d",taTime[i]);
  printf("\t\t %d",wTime[i]);

  totalTA=totalTA+taTime[i];
  totalWait=totalWait+wTime[i];
 }
 printf("\n_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+");

 printf("\n total waiting time:%.2f",totalWait/njob);
 printf("\n total turn around time:%.2f",totalTA/njob);

getch();
}


OUTPUT


Enter number of process:5

 Enter the Arrival time & Burst time for process[1]:0 9

 Enter the Arrival time & Burst time for process[2]:1 5

 Enter the Arrival time & Burst time for process[3]:2 2

 Enter the Arrival time & Burst time for process[4]:3 6

 Enter the Arrival time & Burst time for process[5]:4 8

 Process ArrivalTime   BurstTime    TurnaroundTime   WaitingTime
_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+
 3              2        2              2               0
 2              1        5              8               3
 4              3        6             12               6
 5              4        8             19              11
 1              0        9             32              23
_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+
 total waiting time:8.60
 total turn around time:14.60Average Turn Around Time:16.200000 

Post a Comment

0Comments

Post a Comment (0)