0% found this document useful (0 votes)
6 views2 pages

Osexp 8 B

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Osexp 8 B

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Aaditya M.

Salgaonkar 22B-CO-001 Batch A|

Experiment No : 8 Date :
Disk Scheduling Algorithm
Aim : b) To implement SSTF disk scheduling algorithm.
Theory:
SSTF (Shortest Seek Time First)
SSTF selects the request closest to the disk head’s current position, minimizing seek time by
choosing the shortest possible distance for each move. By reducing the distance traveled,
SSTF improves upon FCFS in terms of overall seek time. However, SSTF can lead to starvation
when a constant stream of nearby requests prevents distant requests from being serviced.
This makes SSTF suboptimal for real-time systems where all requests need guaranteed
service.
Advantages:
• Reduces average seek time compared to FCFS.
• More efficient for systems with moderate to high load.
Disadvantages:
• Risk of starvation for requests far from the current head position.
• Increased complexity compared to FCFS.
Example:
With the disk head initially at track 50 and requests at [10, 40, 60, 90], SSTF will serve the
requests in this order:
• 50 → 60 (seek time = 10)
• 60 → 40 (seek time = 20)
• 40 → 10 (seek time = 30)
• 10 → 90 (seek time = 80)
Total seek time = 10 + 20 + 30 + 80 = 140
Algorithm:
1. Start with the current disk head position.
2. From the list of pending requests, find the one with the minimum seek distance to the
current head position.
3. Move the disk head to this request’s position and mark it as completed.
4. Repeat steps 2-3 until all requests are served.

Code:
#include <bits/stdc++.h>
#define N 50
using namespace std;
int SSTF(int headPosition, int totalRequests, int requests[]) {
int seekTime = 0, currentPosition = headPosition;
sort(requests, requests + totalRequests);
int i = 0; while (i < totalRequests && requests[i] < headPosition)
i++; cout << headPosition << "->"; int left = i - 1, right = i;
Aaditya M. Salgaonkar 22B-CO-001 Batch A|

while (left >= 0 && right < totalRequests) {


if (abs(currentPosition - requests[left]) < abs(currentPosition - requests[right])) {
seekTime += abs(currentPosition - requests[left]);
currentPosition = requests[left]; cout << currentPosition << "->"; left--;
} else {
seekTime += abs(currentPosition - requests[right]);
currentPosition = requests[right]; cout << currentPosition << "->"; right++;
} } while (left >= 0) {
seekTime += abs(currentPosition - requests[left]);
currentPosition = requests[left];
cout << currentPosition << "->"; left--;
} while (right < totalRequests) {
seekTime += abs(currentPosition - requests[right]);
currentPosition = requests[right];
cout << currentPosition << "->"; right++;
} cout << "End";
return seekTime;
}
int main() {
int totalRequests, headPosition, requests[N];
cout << "Enter the number of requests: "; cin >> totalRequests;
cout << "Enter the head position: "; cin >> headPosition;
cout << "Enter the requests:\n";
for (int i = 0; i < totalRequests; i++)
cin >> requests[i];
cout<<"\nHead Position: "<<headPosition;
cout<<"\nSeek Sequence: ";
cout << "\nTotal Seek Time: " << SSTF(headPosition, totalRequests, requests) << endl;
return 0;
}
Output:

Conclusion:
SSTF 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.

You might also like