|
| 1 | +// SSTF disk scheduling |
| 2 | + |
| 3 | +#include <bits/stdc++.h> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +// Calculates difference of each |
| 7 | +// track number with the head position |
| 8 | +void calculatedifference(int request[], int head, |
| 9 | + int diff[][2], int n) |
| 10 | +{ |
| 11 | + for(int i = 0; i < n; i++) |
| 12 | + { |
| 13 | + diff[i][0] = abs(head - request[i]); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// Find unaccessed track which is |
| 18 | +// at minimum distance from head |
| 19 | +int findMIN(int diff[][2], int n) |
| 20 | +{ |
| 21 | + int index = -1; |
| 22 | + int minimum = 1e9; |
| 23 | + |
| 24 | + for(int i = 0; i < n; i++) |
| 25 | + { |
| 26 | + if (!diff[i][1] && minimum > diff[i][0]) |
| 27 | + { |
| 28 | + minimum = diff[i][0]; |
| 29 | + index = i; |
| 30 | + } |
| 31 | + } |
| 32 | + return index; |
| 33 | +} |
| 34 | + |
| 35 | +void shortestSeekTimeFirst(int request[], |
| 36 | + int head, int n) |
| 37 | +{ |
| 38 | + if (n == 0) |
| 39 | + { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Create array of objects of class node |
| 44 | + int diff[n][2] = { { 0, 0 } }; |
| 45 | + |
| 46 | + // Count total number of seek operation |
| 47 | + int seekcount = 0; |
| 48 | + |
| 49 | + // Stores sequence in which disk access is done |
| 50 | + int seeksequence[n + 1] = {0}; |
| 51 | + |
| 52 | + for(int i = 0; i < n; i++) |
| 53 | + { |
| 54 | + seeksequence[i] = head; |
| 55 | + calculatedifference(request, head, diff, n); |
| 56 | + int index = findMIN(diff, n); |
| 57 | + diff[index][1] = 1; |
| 58 | + |
| 59 | + // Increase the total count |
| 60 | + seekcount += diff[index][0]; |
| 61 | + |
| 62 | + // Accessed track is now new head |
| 63 | + head = request[index]; |
| 64 | + } |
| 65 | + seeksequence[n] = head; |
| 66 | + |
| 67 | + cout << "Total number of seek operations = " |
| 68 | + << seekcount << endl; |
| 69 | + cout << "Seek sequence is : " << "\n"; |
| 70 | + |
| 71 | + // Print the sequence |
| 72 | + for(int i = 0; i <= n; i++) |
| 73 | + { |
| 74 | + cout << seeksequence[i] << "\n"; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Driver code |
| 79 | +int main() |
| 80 | +{ |
| 81 | + int n = 8; |
| 82 | + int proc[n] = { 176, 79, 34, 60, 92, 11, 41, 114 }; |
| 83 | + |
| 84 | + shortestSeekTimeFirst(proc, 50, n); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | + |
0 commit comments