Search Tutorials

Saturday 1 June 2013

C code for Shortest Remaining Time Scheduling

Shortest Remaining Time (SRT) Scheduling:
The shortest remaining time (SRT) policy is a preemptive version of shortest process next(SPN). In this case, the scheduler always chooses the process that has the shortest expected remaining processing time. When a new process joins the ready queue, it may in fact have a shorter remaining time than the currently running process. Accordingly, the scheduler may preempt the current process when a new process becomes ready. As with SPN, the scheduler must have an estimate of processing time to perform the selection function, and there is a risk of starvation of longer processes.

/* C program to implement Shortest Remaining Time Scheduling */

# include <stdio.h>
# include <conio.h>
void main()
{
 int i,p[20],a[20],b[20],br[20],tat[20],gantt[100][2],n1,n2,n3,c,d,e=0,f=0,g,time=0,t1,t2,t3,t4,t5,t6;
 float v1=0.0,v2=0.0;
 FILE *fp;
 clrscr();
 fp=fopen("sjf.txt","r");
 while (fscanf(fp,"%d%d%d",&n1,&n2,&n3)!=EOF)
 {
   p[e]=n1;
   a[e]=n2;
   b[e]=n3;
   br[e++]=n3;
 }
 while (1)
 {
  for (i=1;i<e;i++)
   if ((br[0]==0 || time<a[0]?1:br[i]<br[0] || (br[i]==br[0] && a[i]<a[0])) && time>=a[i] && br[i]!=0)
   {
    t1=p[0];
    t2=a[0];
    t3=b[0];
    t4=br[0];
    t5=tat[0];

    p[0]=p[i];
    a[0]=a[i];
    b[0]=b[i];
    br[0]=br[i];
    tat[0]=tat[i];

    p[i]=t1;
    a[i]=t2;
    b[i]=t3;
    br[i]=t4;
    tat[i]=t5;
   }
  if (p[0]>time || br[0]==0)
  {
   if (f==0 || gantt[f-1][0]!=-1)
   {
    gantt[f][0]=-1;
    gantt[f++][1]=time;
   }
  }
  else
  if (br[0]!=0)
  {
   if (f==0 || gantt[f-1][0]!=p[0])
   {
    gantt[f][0]=p[0];
    gantt[f++][1]=time;
   }
   if (--br[0]==0)
     tat[0]=time+1;
  }
  time++;
  c=0;
  for (i=0;i<e;i++)
   if (br[i]!=0)
    c=1;
  if (c==0)
   break;
 }

 gantt[f][1]=time;
 printf ("%8s%8s%8s%8s%8s\n\n","P","A","B","W","TAT");
 for (i=0;i<e;i++)
  printf ("%8d%8d%8d%8d%8d\n",p[i],a[i],b[i],tat[i]-b[i],tat[i]);
 printf("\n\nAverage waiting time is:");
  for(i=0;i<e;i++)
    v1+=tat[i]-b[i];
printf("%f",v1/e);
printf("\n\nAverage turnaround time is:");
  for(i=0;i<e;i++)
    v2+=tat[i];
printf("%f",v2/e);
printf("\n\n\n");

 printf ("\n\n ");
 for (i=0;i<f;i++)
  printf ("--- ");
 printf ("\n|");
 for (i=0;i<f;i++)
  printf (gantt[i][0]==-1?"   |":" %d |",gantt[i][0]);
 printf ("\n ");
 for (i=0;i<f;i++)
  printf ("--- ");
 printf ("\n0");
 for (i=1;i<=f;i++)
  printf ("  %2d",gantt[i][1]);
 getch();
}




Input File:

1 0 3
2 1 3
3 2 2

//Output Of the above program:-

C code for Shortest Remaining Time Scheduling


No comments:

Post a Comment

Back to Top