Skip to content

Commit b23de62

Browse files
authored
Merge pull request #1867 from AshmitaBarthwal/main
Adaptive Disk Reordering (ADR) Algorithm
2 parents e19e888 + 8cc9203 commit b23de62

File tree

2 files changed

+140
-0
lines changed
  • Miscellaneous Algorithms/Adaptive Disk Reordering Algorithm

2 files changed

+140
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Adaptive Disk Reordering (ADR) Algorithm
2+
3+
This project provides a C implementation of the Adaptive Disk Reordering (ADR) algorithm, which dynamically adjusts the order of disk I/O requests to optimize disk access times. By prioritizing requests based on their proximity to the current disk head position, this algorithm minimizes seek time, making it ideal for applications requiring efficient disk scheduling.
4+
5+
## Table of Contents
6+
- [Overview](#overview)
7+
- [Features](#features)
8+
- [Algorithm Explanation](#algorithm-explanation)
9+
- [Input and Output](#input-and-output)
10+
- [Code Structure](#code-structure)
11+
- [Example](#example)
12+
13+
## Overview
14+
The Adaptive Disk Reordering (ADR) algorithm reduces disk seek time by dynamically reordering I/O requests based on the disk head's current position. It enhances performance in environments with frequent disk access patterns by minimizing the back-and-forth movement of the disk arm, thus lowering the overall seek time and improving response time.
15+
16+
## Features
17+
- **Dynamic Request Reordering**: Orders pending I/O requests based on current disk head position to reduce seek time.
18+
- **Seek Time Optimization**: Calculates total and average seek time, providing metrics to evaluate performance.
19+
- **Efficient Disk Head Movement**: Minimizes disk head movement by servicing the nearest unserviced requests first.
20+
- **Simple and Adaptable**: Easy to integrate into systems where frequent disk I/O occurs.
21+
22+
## Algorithm Explanation
23+
The ADR algorithm follows these steps:
24+
1. **Current Head Position Check**: Starts by checking the current head position and orders requests based on their proximity to this position.
25+
2. **Request Sorting**: Requests are sorted dynamically during each iteration, ensuring that the closest unserviced request is serviced next.
26+
3. **Service Requests**:
27+
- Services requests sequentially, moving the disk head to the nearest unserviced request and updating the seek time.
28+
4. **Seek Time Calculation**: After all requests are serviced, the algorithm outputs the total and average seek times for evaluation.
29+
30+
## Input and Output
31+
32+
### Input
33+
- **Number of Requests**: The total number of track requests (integer).
34+
- **Track Requests**: An array containing track numbers of each request.
35+
- **Initial Head Position**: The starting position of the disk head (integer).
36+
37+
### Output
38+
- **Seek Sequence**: The order in which tracks are accessed by the disk head.
39+
- **Total Seek Time**: The cumulative distance the disk head traveled to service all requests.
40+
- **Average Seek Time**: Average distance traveled per request.
41+
42+
## Code Structure
43+
The code for the ADR algorithm is organized as follows:
44+
.
45+
├── ADR.c # Main C file with the ADR implementation
46+
└── Readme.md # Project documentation
47+
48+
### Example
49+
50+
Enter number of disk requests: 5
51+
Enter track numbers for the requests:
52+
Request 1: 55
53+
Request 2: 14
54+
Request 3: 37
55+
Request 4: 98
56+
Request 5: 25
57+
Enter initial head position: 50
58+
59+
Seek Sequence: 50 -> 55 -> 37 -> 25 -> 14 -> 98
60+
Total Seek Time: 96
61+
Average Seek Time: 19.20

0 commit comments

Comments
 (0)