C program for shortest remaining time first scheduling algorithm.

Posted by Mangesh on June 16, 2018

program in c to implement shortest remaining time first scheduling algorithm.

Description :

If a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF). If a new process arrives with a shorter burst time than remaining of current process then schedule new process. Further reduces average waiting time and average response time.

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],b[10],x[10];
 int waiting[10],turnaround[10],completion[10];
 int i,j,smallest,count=0,time,n;
 double avg=0,tt=0,end;
 clrscr();
 printf("\nEnter the number of Processes: ");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
   printf("\nEnter arrival time of process %d : ",i+1);
   scanf("%d",&a[i]);
 }
 for(i=0;i<n;i++)
 {
   printf("\nEnter burst time of process %d : ",i+1);
   scanf("%d",&b[i]);
 }
 for(i=0;i<n;i++)
 x[i]=b[i];

  b[9]=9999;
 //printf("time => process number");
 for(time=0;count!=n;time++)
 {
   smallest=9;
  for(i=0;i<n;i++)
  {
   if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
   smallest=i;
  }
  b[smallest]--;
  //printf("\n%d => p%d",time+1,smallest);
  if(b[smallest]==0)
  {
   count++;
   end=time+1;
   completion[smallest] = end;
   waiting[smallest] = end - a[smallest] - x[smallest];
   turnaround[smallest] = end - a[smallest];
   // printf("\n %d  %d   %d",smallest,wt[smallest],ttp[smallest]);
  }
 }
 printf("pid \t burst \t arrival \twaiting \tturnaround \tcompletion");
 for(i=0;i<n;i++)
 {
   printf("\n %d \t   %d \t %d\t\t%d   \t\t%d\t\t%d",i+1,x[i],a[i],waiting[i],turnaround[i],completion[i]);
   avg = avg + waiting[i];
   tt = tt + turnaround[i];
 }
 printf("\n  %If   %If",avg,tt);
 printf("\n\nAverage waiting time = %lf\n",avg/n);
 printf("Average Turnaround time = %lf",tt/n);
 getch();
}

Output :

c program for shortest remaining time first Executed and Tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program for first come first serve scheduling.
2 C program for shortest job first scheduling algorithm.
3 C program for shortest remaining time first scheduling algorithm.
4 C program for round robin scheduling algorithm.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.