Program to implement SSTF disk scheduling algorithm in C | Wave the world

Program to implement SSTF disk scheduling algorithm in C

/*
   SSTF Disk Scheduling Algorithm
   Created By: Pirate
*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
    scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
  for(i=1;i<n;i++)
  t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if(t[i]>t[j])
      {
        temp=t[i];
        t[i]=t[j];
        t[j]=temp;
        temp=queue[i];
        queue[i]=queue[j];
        queue[j]=temp;
      }
  }
  }
  for(i=1;i<n-1;i++)
  {
  seek=seek+abs(head-queue[i]);
  head=queue[i];
}
  printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
getch();
}





Output



















17 comments:

  1. Hi, Nice Post. you raise some really great points. Thanks for sharing.Scheduling Software

    ReplyDelete
  2. Try giving other inputs , this code won't show the proper result.

    ReplyDelete
  3. didnt get the correct output...try to check the code or u might have posted a wrong code

    ReplyDelete
    Replies
    1. Tell me your input data. I'll check.

      Delete
    2. 98
      58
      55
      39
      38
      18
      150
      160
      184

      head initial 100

      output avg seek 27.5

      Delete
    3. Please run program correctly. Output is 50.0004 ave seek time.

      Delete
  4. wrong result.... what about t[0]

    ReplyDelete
  5. here t[0] will give garbage value.

    ReplyDelete
  6. Replies
    1. this program needs one correction
      for(i=1;i<n;i++)
      instead of
      for(i=1;i<n-1;i++)



      seek=seek+abs(head-queue[i]);
      head=queue[i];

      Delete
  7. There is another correction needed:

    for(i=0;i<n;i++)
    t[i]=abs(head-queue[i]);

    instead of

    for(i=1;i<n;i++)
    t[i]=abs(head-queue[i]);

    The way it was, the first value of the queue is not going to be properly evaluated in some cases.

    ReplyDelete
  8. Of course this would give wrong ans,programmer using zero based indexing while taking input and starting from t[1] while taking abs difference .

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete

 

Pro

About