|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#define MAX_REQUESTS 100 |
| 5 | + |
| 6 | +// Structure to store each disk request with track number and status |
| 7 | +typedef struct { |
| 8 | + int track; |
| 9 | + int is_serviced; |
| 10 | +} DiskRequest; |
| 11 | + |
| 12 | +// Function to calculate absolute difference |
| 13 | +int abs_diff(int a, int b) { |
| 14 | + return a > b ? a - b : b - a; |
| 15 | +} |
| 16 | + |
| 17 | +// Function to reorder requests dynamically based on current disk position |
| 18 | +void reorder_requests(DiskRequest requests[], int num_requests, int current_head) { |
| 19 | + for (int i = 0; i < num_requests - 1; i++) { |
| 20 | + for (int j = i + 1; j < num_requests; j++) { |
| 21 | + // Sort requests based on proximity to current head position |
| 22 | + if (abs_diff(requests[i].track, current_head) > abs_diff(requests[j].track, current_head)) { |
| 23 | + DiskRequest temp = requests[i]; |
| 24 | + requests[i] = requests[j]; |
| 25 | + requests[j] = temp; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Adaptive Disk Reordering (ADR) algorithm implementation |
| 32 | +void adaptive_disk_reordering(DiskRequest requests[], int num_requests, int initial_head) { |
| 33 | + int current_head = initial_head; |
| 34 | + int total_seek_time = 0; |
| 35 | + |
| 36 | + printf("Seek Sequence: %d", current_head); |
| 37 | + |
| 38 | + for (int i = 0; i < num_requests; i++) { |
| 39 | + reorder_requests(requests, num_requests, current_head); |
| 40 | + |
| 41 | + // Find the nearest unserviced request |
| 42 | + for (int j = 0; j < num_requests; j++) { |
| 43 | + if (!requests[j].is_serviced) { |
| 44 | + int seek_time = abs_diff(current_head, requests[j].track); |
| 45 | + total_seek_time += seek_time; |
| 46 | + current_head = requests[j].track; |
| 47 | + requests[j].is_serviced = 1; |
| 48 | + |
| 49 | + printf(" -> %d", current_head); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + printf("\nTotal Seek Time: %d\n", total_seek_time); |
| 56 | + printf("Average Seek Time: %.2f\n", (float)total_seek_time / num_requests); |
| 57 | +} |
| 58 | + |
| 59 | +int main() { |
| 60 | + int num_requests, initial_head; |
| 61 | + |
| 62 | + printf("Enter number of disk requests: "); |
| 63 | + scanf("%d", &num_requests); |
| 64 | + |
| 65 | + DiskRequest requests[MAX_REQUESTS]; |
| 66 | + printf("Enter track numbers for the requests:\n"); |
| 67 | + for (int i = 0; i < num_requests; i++) { |
| 68 | + printf("Request %d: ", i + 1); |
| 69 | + scanf("%d", &requests[i].track); |
| 70 | + requests[i].is_serviced = 0; |
| 71 | + } |
| 72 | + |
| 73 | + printf("Enter initial head position: "); |
| 74 | + scanf("%d", &initial_head); |
| 75 | + |
| 76 | + adaptive_disk_reordering(requests, num_requests, initial_head); |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
0 commit comments