Skip to content

Commit

Permalink
Merge pull request #1867 from AshmitaBarthwal/main
Browse files Browse the repository at this point in the history
Adaptive Disk Reordering (ADR) Algorithm
  • Loading branch information
pankaj-bind authored Nov 10, 2024
2 parents e19e888 + 8cc9203 commit b23de62
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Miscellaneous Algorithms/Adaptive Disk Reordering Algorithm/ADR.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>

#define MAX_REQUESTS 100

// Structure to store each disk request with track number and status
typedef struct {
int track;
int is_serviced;
} DiskRequest;

// Function to calculate absolute difference
int abs_diff(int a, int b) {
return a > b ? a - b : b - a;
}

// Function to reorder requests dynamically based on current disk position
void reorder_requests(DiskRequest requests[], int num_requests, int current_head) {
for (int i = 0; i < num_requests - 1; i++) {
for (int j = i + 1; j < num_requests; j++) {
// Sort requests based on proximity to current head position
if (abs_diff(requests[i].track, current_head) > abs_diff(requests[j].track, current_head)) {
DiskRequest temp = requests[i];
requests[i] = requests[j];
requests[j] = temp;
}
}
}
}

// Adaptive Disk Reordering (ADR) algorithm implementation
void adaptive_disk_reordering(DiskRequest requests[], int num_requests, int initial_head) {
int current_head = initial_head;
int total_seek_time = 0;

printf("Seek Sequence: %d", current_head);

for (int i = 0; i < num_requests; i++) {
reorder_requests(requests, num_requests, current_head);

// Find the nearest unserviced request
for (int j = 0; j < num_requests; j++) {
if (!requests[j].is_serviced) {
int seek_time = abs_diff(current_head, requests[j].track);
total_seek_time += seek_time;
current_head = requests[j].track;
requests[j].is_serviced = 1;

printf(" -> %d", current_head);
break;
}
}
}

printf("\nTotal Seek Time: %d\n", total_seek_time);
printf("Average Seek Time: %.2f\n", (float)total_seek_time / num_requests);
}

int main() {
int num_requests, initial_head;

printf("Enter number of disk requests: ");
scanf("%d", &num_requests);

DiskRequest requests[MAX_REQUESTS];
printf("Enter track numbers for the requests:\n");
for (int i = 0; i < num_requests; i++) {
printf("Request %d: ", i + 1);
scanf("%d", &requests[i].track);
requests[i].is_serviced = 0;
}

printf("Enter initial head position: ");
scanf("%d", &initial_head);

adaptive_disk_reordering(requests, num_requests, initial_head);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Adaptive Disk Reordering (ADR) Algorithm

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.

## Table of Contents
- [Overview](#overview)
- [Features](#features)
- [Algorithm Explanation](#algorithm-explanation)
- [Input and Output](#input-and-output)
- [Code Structure](#code-structure)
- [Example](#example)

## Overview
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.

## Features
- **Dynamic Request Reordering**: Orders pending I/O requests based on current disk head position to reduce seek time.
- **Seek Time Optimization**: Calculates total and average seek time, providing metrics to evaluate performance.
- **Efficient Disk Head Movement**: Minimizes disk head movement by servicing the nearest unserviced requests first.
- **Simple and Adaptable**: Easy to integrate into systems where frequent disk I/O occurs.

## Algorithm Explanation
The ADR algorithm follows these steps:
1. **Current Head Position Check**: Starts by checking the current head position and orders requests based on their proximity to this position.
2. **Request Sorting**: Requests are sorted dynamically during each iteration, ensuring that the closest unserviced request is serviced next.
3. **Service Requests**:
- Services requests sequentially, moving the disk head to the nearest unserviced request and updating the seek time.
4. **Seek Time Calculation**: After all requests are serviced, the algorithm outputs the total and average seek times for evaluation.

## Input and Output

### Input
- **Number of Requests**: The total number of track requests (integer).
- **Track Requests**: An array containing track numbers of each request.
- **Initial Head Position**: The starting position of the disk head (integer).

### Output
- **Seek Sequence**: The order in which tracks are accessed by the disk head.
- **Total Seek Time**: The cumulative distance the disk head traveled to service all requests.
- **Average Seek Time**: Average distance traveled per request.

## Code Structure
The code for the ADR algorithm is organized as follows:
.
├── ADR.c # Main C file with the ADR implementation
└── Readme.md # Project documentation

### Example

Enter number of disk requests: 5
Enter track numbers for the requests:
Request 1: 55
Request 2: 14
Request 3: 37
Request 4: 98
Request 5: 25
Enter initial head position: 50

Seek Sequence: 50 -> 55 -> 37 -> 25 -> 14 -> 98
Total Seek Time: 96
Average Seek Time: 19.20

0 comments on commit b23de62

Please sign in to comment.