Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptive Disk Reordering (ADR) Algorithm #1867

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading