-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1828 from AshmitaBarthwal/main
The Power-Aware Disk Scheduling (PADS) Algorithm
- Loading branch information
Showing
2 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/PADS.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
#define MAX_REQUESTS 100 | ||
#define DISK_SIZE 200 // Represents the number of tracks on the disk | ||
#define LOW_POWER_THRESHOLD 20 // Threshold for "low-power" mode | ||
|
||
// Function to sort requests | ||
void sort_requests(int requests[], int n) { | ||
for (int i = 0; i < n - 1; i++) { | ||
for (int j = i + 1; j < n; j++) { | ||
if (requests[i] > requests[j]) { | ||
int temp = requests[i]; | ||
requests[i] = requests[j]; | ||
requests[j] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int power_aware_scan(int requests[], int n, int head, int direction) { | ||
int seek_time = 0; | ||
int total_seek_time = 0; | ||
int distance; | ||
int current_head = head; | ||
bool low_power_mode = false; | ||
|
||
// Sort requests for efficient scan-based scheduling | ||
sort_requests(requests, n); | ||
|
||
// Separate requests to the left and right of the head | ||
int left[MAX_REQUESTS], right[MAX_REQUESTS]; | ||
int left_count = 0, right_count = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
if (requests[i] < head) { | ||
left[left_count++] = requests[i]; | ||
} else { | ||
right[right_count++] = requests[i]; | ||
} | ||
} | ||
|
||
// Implement the SCAN algorithm with power-awareness | ||
if (direction == 1) { // Moving towards higher tracks | ||
for (int i = 0; i < right_count; i++) { | ||
distance = abs(current_head - right[i]); | ||
if (distance > LOW_POWER_THRESHOLD) { | ||
printf("Entering low-power mode to save energy.\n"); | ||
low_power_mode = true; | ||
} else { | ||
printf("Servicing request at track %d\n", right[i]); | ||
seek_time = distance; | ||
current_head = right[i]; | ||
total_seek_time += seek_time; | ||
low_power_mode = false; | ||
} | ||
} | ||
// Reverse direction after reaching the highest track | ||
for (int i = left_count - 1; i >= 0; i--) { | ||
distance = abs(current_head - left[i]); | ||
if (distance > LOW_POWER_THRESHOLD) { | ||
printf("Entering low-power mode to save energy.\n"); | ||
low_power_mode = true; | ||
} else { | ||
printf("Servicing request at track %d\n", left[i]); | ||
seek_time = distance; | ||
current_head = left[i]; | ||
total_seek_time += seek_time; | ||
low_power_mode = false; | ||
} | ||
} | ||
} else { // Moving towards lower tracks | ||
for (int i = left_count - 1; i >= 0; i--) { | ||
distance = abs(current_head - left[i]); | ||
if (distance > LOW_POWER_THRESHOLD) { | ||
printf("Entering low-power mode to save energy.\n"); | ||
low_power_mode = true; | ||
} else { | ||
printf("Servicing request at track %d\n", left[i]); | ||
seek_time = distance; | ||
current_head = left[i]; | ||
total_seek_time += seek_time; | ||
low_power_mode = false; | ||
} | ||
} | ||
// Reverse direction after reaching the lowest track | ||
for (int i = 0; i < right_count; i++) { | ||
distance = abs(current_head - right[i]); | ||
if (distance > LOW_POWER_THRESHOLD) { | ||
printf("Entering low-power mode to save energy.\n"); | ||
low_power_mode = true; | ||
} else { | ||
printf("Servicing request at track %d\n", right[i]); | ||
seek_time = distance; | ||
current_head = right[i]; | ||
total_seek_time += seek_time; | ||
low_power_mode = false; | ||
} | ||
} | ||
} | ||
|
||
return total_seek_time; | ||
} | ||
|
||
int main() { | ||
int n, head, direction; | ||
int requests[MAX_REQUESTS]; | ||
|
||
// Get input for requests | ||
printf("Enter the number of disk requests: "); | ||
scanf("%d", &n); | ||
printf("Enter the disk requests: "); | ||
for (int i = 0; i < n; i++) { | ||
scanf("%d", &requests[i]); | ||
} | ||
printf("Enter the initial position of the disk head: "); | ||
scanf("%d", &head); | ||
printf("Enter the initial direction (1 for high, 0 for low): "); | ||
scanf("%d", &direction); | ||
|
||
// Run the power-aware scan algorithm | ||
int total_seek_time = power_aware_scan(requests, n, head, direction); | ||
|
||
printf("Total seek time: %d\n", total_seek_time); | ||
printf("Average seek time: %.2f\n", (float)total_seek_time / n); | ||
|
||
return 0; | ||
} |
73 changes: 73 additions & 0 deletions
73
Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Power-Aware Disk Scheduling (PADS) Algorithm | ||
|
||
This project implements the Power-Aware Disk Scheduling (PADS) algorithm in C, a modified disk scheduling technique designed to reduce power consumption during disk I/O operations. The PADS algorithm optimizes disk head movement by balancing request servicing with energy-saving mechanisms, simulating a "low-power" mode when disk head movement would be excessive. | ||
|
||
## 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 Power-Aware Disk Scheduling (PADS) algorithm aims to enhance energy efficiency in systems by reducing unnecessary disk head movements. By implementing a modified SCAN (Elevator) scheduling algorithm, PADS can selectively enter a "low-power" mode if the next request is far from the current disk head position. This reduces power consumption while still achieving acceptable seek times and balancing I/O performance. | ||
|
||
## Features | ||
|
||
- **Efficient Disk I/O**: Services requests in a sequence to minimize disk head movement. | ||
- **Power-Aware Mode**: Enters "low-power" mode when the disk head movement exceeds a specified threshold. | ||
- **Flexible Direction Control**: Allows initial direction specification (toward higher or lower track numbers). | ||
- **Seek Time Calculation**: Outputs total and average seek time for performance analysis. | ||
|
||
## Algorithm Explanation | ||
|
||
1. **Initial Direction**: The disk head starts moving in a specified direction (either toward higher or lower track numbers). | ||
2. **Sorting Requests**: Requests are sorted to prioritize servicing those in the head's current path, ensuring minimal head movement. | ||
3. **Low-Power Mode Activation**: If the distance to the next request exceeds a defined threshold, the algorithm simulates a "low-power" mode, reducing head movement until closer requests are available. | ||
4. **Seek Time Calculation**: The algorithm calculates the total distance the head moved to service all requests, helping evaluate performance. | ||
|
||
## Input and Output | ||
|
||
### Input | ||
|
||
- **Number of Requests**: Total number of track requests. | ||
- **Track Requests**: Array of requested track positions. | ||
- **Initial Head Position**: Starting position of the disk head. | ||
- **Disk Size**: Total size of the disk (maximum track number). | ||
- **Initial Direction**: Starting direction of the head (1 for high, 0 for low). | ||
|
||
### Output | ||
|
||
- **Seek Sequence**: The sequence of tracks serviced by the disk head. | ||
- **Total Seek Time**: Total distance the disk head moved. | ||
- **Average Seek Time**: Average seek time per request, useful for performance evaluation. | ||
|
||
## Code Structure | ||
|
||
├── pads_algorithm.c # Contains the PADS algorithm implementation | ||
├── README.md # Project documentation | ||
└── LICENSE # License information | ||
|
||
## Example | ||
|
||
### Input: | ||
|
||
Enter the number of disk requests: 5 | ||
Enter the disk requests: 45 130 10 180 90 | ||
Enter the initial position of the disk head: 50 | ||
Enter the initial direction (1 for high, 0 for low): 1 | ||
|
||
### Output: | ||
|
||
Servicing request at track 90 | ||
Servicing request at track 130 | ||
Servicing request at track 180 | ||
Entering low-power mode to save energy. | ||
Servicing request at track 45 | ||
Servicing request at track 10 | ||
|
||
Total seek time: 250 | ||
Average seek time: 50.00 |