Saturday 3 March 2018

Disk Scheduling Algorithms

Disk Scheduling Algorithms


This tutorial is prepared for those that need assistance in Disk Scheduling Algorithms.

INTRODUCTION

In operating systems, seek time is very important. Since all device requests are linked in queues, the seek time is increased causing the system to slow down. Disk Scheduling Algorithms are used to reduce the total seek time of any request.

PURPOSE
The purpose of this material is to provide one with help on disk scheduling algorithms. Hopefully with this, one will be able to get a stronger grasp of what disk scheduling algorithms do.



There are many Disk Scheduling Algorithms but before discussing them let’s have a quick look at some of the important terms:
Seek Time:Seek time is the time taken to locate the disk arm to a specified track where the data is to be read or write. So the disk scheduling algorithm that gives minimum average seek time is better. 

Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to rotate into a position so that it can access the read/write heads. So the disk scheduling algorithm that gives minimum rotational latency is better.
Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed of the disk and number of bytes to be transferred.
Disk Access Time: 

Disk Access Time = Seek Time + Rotational Latency + Transfer Timeos1
TYPES OF DISK SCHEDULING ALGORITHMS
Although there are other algorithms that reduce the seek time of all requests, I will only concentrate on the following disk scheduling algorithms:
  • First Come-First Serve (FCFS)
  • Shortest Seek Time First (SSTF)
  • Elevator (SCAN) 
  • Circular SCAN (C-SCAN)
  • LOOK
  • C-LOOK 
These algorithms are not hard to understand, but they can confuse someone because they are so similar. What we are striving for by using these algorithms is keeping Head Movements (# tracks) to the least amount as possible. The less the head has to move the faster the seek time will be. I will show you and explain to you why C-LOOK is the best algorithm to use in trying to establish less seek time.
Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-write head initially at the track 50 and the tail track being at 199 let us now discuss the different algorithms.

1. First Come -First Serve (FCFS)
 [DIAGRAM]
All incoming requests are placed at the end of the queue. Whatever number that is next in the queue will be the next number served. Using this algorithm doesn't provide the best results. To determine the number of head movements you would simply find the number of tracks it took to move from one request to the next. For this case it went from 50 to 95 to 180 and so on. From 50 to 95 it moved 45 tracks. If you tally up the total number of tracks you will find how many tracks it had to go through before finishing the entire request. In this example, it had a total head movement of 640 tracks. The disadvantage of this algorithm is noted by the oscillation from track 50 to track 180 and then back to track 11 to 123 then to 64. As you will soon see, this is the worse algorithm that one can use.

Advantages:
  • Every request gets a fair chance
  • No indefinite postponement
Disadvantages:
  • Does not try to optimize seek time
  • May not provide the best possible service
2. Shortest Seek Time First (SSTF) [DIAGRAM]
In this case request is serviced according to next shortest distance. Starting at 50, the next shortest distance would be 62 instead of 34 since it is only 12 tracks away from 62 and 16 tracks away from 34. The process would continue until all the process are taken care of. For example the next case would be to move from 62 to 64 instead of 34 since there are only 2 tracks between them and not 18 if it were to go the other way. Although this seems to be a better service being that it moved a total of 236 tracks, this is not an optimal one. There is a great chance that starvation would take place. The reason for this is if there were a lot of requests close to each other the other requests will never be handled since the distance will always be greater.


Advantages:
  • Average Response Time decreases
  • Throughput increases
Disadvantages:
  • Overhead to calculate seek time in advance
  • Can cause Starvation for a request if it has higher seek time as compared to incoming requests
  • High variance of response time as SSTF favours only some requests
3. Elevator (SCAN) [DIAGRAM]
This approach works like an elevator does. It scans down towards the nearest end and then when it hits the bottom it scans up servicing the requests that it didn't get going down. If a request comes in after it has been scanned it will not be serviced until the process comes back down or moves back up. This process moved a total of 230 tracks. Once again this is more optimal than the previous algorithm, but it is not the best.


Advantages:
  • High throughput
  • Low variance of response time
  • Average response time
Disadvantages:
  • Long waiting time for requests for locations just visited by disk arm.
4. Circular Scan (C-SCAN) [DIAGRAM]
Circular scanning works just like the elevator to some extent. It begins its scan toward the nearest end and works it way all the way to the end of the system. Once it hits the bottom or top it jumps to the other end and moves in the same direction. Keep in mind that the huge jump doesn't count as a head movement. The total head movement for this algorithm is only 187 track, but still this isn't the mose sufficient.

5. C-LOOK [DIAGRAM]
This is just an enhanced version of C-SCAN. In this the scanning doesn't go past the last request in the direction that it is moving. It too jumps to the other end but not all the way to the end. Just to the furthest request. C-SCAN had a total movement of 187 but this scan (C-LOOK) reduced it down to 157 tracks.


From this you were able to see a scan change from 644 total head movements to just 157. You should now have an understanding as to why your operating system truly relies on the type of algorithm it needs when it is dealing with multiple processes.

NOTE: It is important that you draw out the sequence when handling algorithms like this one. One would have a hard time trying to determine which algorithm is best by just reading the definition. There is a good chance that without the drawings there could be miscalculations.


Programs related to these Algorithms -

/*
   Implementation of DISK Scheduling Algorithm using FCFS.
   Data structure used - ARRAY.
   Implemented by -  Devansh Varshney
   
   GitHub ID      -  varshneydevansh 
*/
#include<iostream>
#include<math.h>                                                             // For abs()
using namespace std;
int main(void)
{  
   int chart[100],result[100];                                               //Data Structure used
   int head,i,n,sum=0;
   cout<<"\n\tEnter the number of processes \n\t";
   cin>>n;
   cout<<"\n\tEnter the processes number \n\t";
   for(i=0;i<n;i++)
    {cin>>chart[i]; cout<<"\t";}                                            //Gettin' processes in the chart
   cout<<"\n\tEnter the HEAD number \n\t";
   cin>>head;
   for(i=0;i<n;i++)
   {
     result[i]=abs(chart[i]-head);
     head=chart[i];
   }
   
   for(i=0;i<n;i++)
   {
     sum+=result[i];
   }
   cout<<endl;
   for(i=0;i<n;i++)
   {
     cout<<"\t"<<result[i];
   }
   
  cout<<"\n\tSUM IS:\t"<<sum<<endl;
  return 0;

} //main()
//End of FCFS Algorithm

Sister's are an invaluable gift

It has been a year since I realized that, in this highly entropic universe, my sisters are the strongest and most stable place to which I be...