Osexp 8 C
Osexp 8 C
Experiment No : 8 Date :
Disk Scheduling Algorithm
Aim : c) To implement SCAN disk scheduling algorithm.
Theory:
SCAN (Elevator Algorithm) The SCAN algorithm moves the disk head in one direction until it
reaches the end of the disk or the end of the request list in that direction. After reaching the
end, it reverses direction and services requests on its way back. This bidirectional movement
pattern is why SCAN is often called the "Elevator algorithm."
SCAN ensures that every request is served in a predictable manner, making it more efficient
than SSTF and FCFS. However, if requests are concentrated at one end of the disk, SCAN can
lead to increased response time for requests on the opposite end.
Advantages:
• Reduced variance in response times, making it more predictable.
• Fair to requests in both directions.
Disadvantages:
• Longer seek time than C-SCAN if requests are frequently clustered.
• Delays requests on the opposite end when moving in one direction.
Example:
Starting with the disk head at track 50 and requests at [10, 40, 60, 90], if SCAN moves left to
right:
• 50 → 60 (seek time = 10)
• 60 → 90 (seek time = 30)
• End of disk, then reverse
• 90 → 40 (seek time = 50)
• 40 → 10 (seek time = 30)
Total seek time = 10 + 30 + 50 + 30 = 120
Algorithm:
1. Start with the disk head’s initial position and direction.
2. Move the disk head in one direction, servicing requests until the end of the disk (or last
request in that direction).
3. Reverse direction and continue servicing requests back to the other end.
4. Repeat the cycle until all requests are served.
Code:
#include <bits/stdc++.h>
#define N 50
Aaditya M. Salgaonkar 22B-CO-001 Batch A|
// left = true
// right = false
// find the index of the first request larger than or equal to the head position
int i = 0;
while (i < totalRequests && requests[i] < headPosition)
i++;
int left = i - 1;
int right = i;
// Move in the chosen direction first (left for decreasing, right for increasing)
while (direction ? left >= 0 : right < totalRequests)
{
if (direction) // Move left
{
seekTime += abs(currentPosition - requests[left]);
currentPosition = requests[left];
cout << currentPosition << " -> ";
left--;
}
else // Move right
{
seekTime += abs(currentPosition - requests[right]);
currentPosition = requests[right];
cout << currentPosition << " -> ";
right++;
}
}
{
while (right < totalRequests)
{
seekTime += abs(currentPosition - requests[right]);
currentPosition = requests[right];
cout << currentPosition << " -> ";
right++;
}
}
else // Switch to left if initially moving right
{
while (left >= 0)
{
seekTime += abs(currentPosition - requests[left]);
currentPosition = requests[left];
cout << currentPosition << " -> ";
left--;
}
}
int main()
{
int totalRequests, headPosition, requests[N], cylinders;
cout << "Enter the number of requests: ";
cin >> totalRequests;
cout << "Enter the head position: ";
cin >> headPosition;
cout << "Enter the number of cylinders: ";
cin >> cylinders;
cout << "Does the track number decrease or increase ???";
bool direction;
cin >> direction;
// Adding requests
for (int i = 0; i < totalRequests; i++)
cin >> requests[i];
cout<<"\nHead Position: "<<headPosition;
cout<<"\nSeek Sequence: ";
Aaditya M. Salgaonkar 22B-CO-001 Batch A|
cout << "\nTotal Seek Time: " << SCAN(headPosition, totalRequests, requests, direction) <<
endl;
return 0;
}
Output:
Conclusion:
SCAN disk scheduling algorithm code was studied and implemented in C++ and the seek
sequence was displayed and total seek time was printed at the end.