From 8d9d7059d70be84b6320b563b5a916e4990cbac8 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:02:34 +0530 Subject: [PATCH 1/6] Create PADS.c --- .../PADS.c | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/PADS.c diff --git a/Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/PADS.c b/Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/PADS.c new file mode 100644 index 00000000..6214cbff --- /dev/null +++ b/Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/PADS.c @@ -0,0 +1,129 @@ +#include +#include +#include + +#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; +} From 824ced72b583837563d3e852cd04bb7dd0731089 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:04:09 +0530 Subject: [PATCH 2/6] Create Readme.md --- .../Readme.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/Readme.md diff --git a/Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/Readme.md b/Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/Readme.md new file mode 100644 index 00000000..7714a6ec --- /dev/null +++ b/Miscellaneous Algorithms/Power-Aware Disk Scheduling algorithm/Readme.md @@ -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 From 0fd9d131a2cddcaf9277261e9b88961f40f55f4b Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:56:34 +0530 Subject: [PATCH 3/6] Create MTC.c --- .../Multi-Tiered Caching Algorithm/MTC.c | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c diff --git a/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c b/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c new file mode 100644 index 00000000..d8f6e9bd --- /dev/null +++ b/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c @@ -0,0 +1,91 @@ +#include +#include + +#define L1_CACHE_SIZE 3 +#define L2_CACHE_SIZE 5 + +typedef struct Cache { + int *data; + int size; + int count; +} Cache; + +// Initialize cache +Cache *initialize_cache(int size) { + Cache *cache = (Cache *)malloc(sizeof(Cache)); + cache->data = (int *)malloc(size * sizeof(int)); + cache->size = size; + cache->count = 0; + return cache; +} + +// Check if a value exists in cache and return its position +int find_in_cache(Cache *cache, int value) { + for (int i = 0; i < cache->count; i++) { + if (cache->data[i] == value) { + return i; + } + } + return -1; +} + +// Add value to cache with FIFO replacement +void add_to_cache(Cache *cache, int value) { + if (cache->count < cache->size) { + cache->data[cache->count++] = value; + } else { + // Shift data and add new value at the end + for (int i = 1; i < cache->size; i++) { + cache->data[i - 1] = cache->data[i]; + } + cache->data[cache->size - 1] = value; + } +} + +// Multi-tiered caching function +void multi_tiered_cache(Cache *L1, Cache *L2, int value) { + int pos_in_L1 = find_in_cache(L1, value); + int pos_in_L2 = find_in_cache(L2, value); + + if (pos_in_L1 != -1) { + printf("Value %d found in L1 cache.\n", value); + } else if (pos_in_L2 != -1) { + printf("Value %d found in L2 cache. Moving to L1.\n", value); + // Move from L2 to L1 cache + add_to_cache(L1, value); + // Remove from L2 (by shifting) + for (int i = pos_in_L2; i < L2->count - 1; i++) { + L2->data[i] = L2->data[i + 1]; + } + L2->count--; + } else { + printf("Value %d not found in L1 or L2. Adding to L1 and L2.\n", value); + add_to_cache(L1, value); + add_to_cache(L2, value); + } +} + +// Free allocated memory for cache +void free_cache(Cache *cache) { + free(cache->data); + free(cache); +} + +// Main function to test multi-tiered caching +int main() { + Cache *L1 = initialize_cache(L1_CACHE_SIZE); + Cache *L2 = initialize_cache(L2_CACHE_SIZE); + + int requests[] = {10, 20, 10, 30, 40, 50, 20, 60, 70, 10}; + int num_requests = sizeof(requests) / sizeof(requests[0]); + + for (int i = 0; i < num_requests; i++) { + multi_tiered_cache(L1, L2, requests[i]); + } + + // Free memory + free_cache(L1); + free_cache(L2); + + return 0; +} From 5b1f93ef41d87cfd09f6f93c756259ecf9914050 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:58:51 +0530 Subject: [PATCH 4/6] Create Readme.md --- .../Multi-Tiered Caching Algorithm/Readme.md | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md diff --git a/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md b/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md new file mode 100644 index 00000000..3e81d7e4 --- /dev/null +++ b/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md @@ -0,0 +1,66 @@ +# Multi-Tiered Caching (MTC) Algorithm + +This project implements a Multi-Tiered Caching (MTC) algorithm in C. The MTC algorithm manages multiple cache levels to improve data retrieval efficiency by storing frequently accessed data in faster, higher-priority caches. It dynamically moves data between cache levels based on access patterns, reducing retrieval time and optimizing memory utilization in systems with large data workloads. + +## Table of Contents + +- [Overview](#overview) +- [Features](#features) +- [Algorithm Explanation](#algorithm-explanation) +- [Input and Output](#input-and-output) +- [Example](#example) + +## Overview + +The Multi-Tiered Caching (MTC) algorithm uses multiple cache levels (e.g., L1, L2) to store frequently accessed data closer to the processor, reducing data retrieval time. This approach is particularly useful for systems with limited memory and a high volume of data requests, as it minimizes access time and improves memory management. + +## Features + +- Multi-tiered caching system with multiple cache levels (e.g., L1 and L2). +- Caching based on access frequency, moving data between levels as needed. +- Simple FIFO (First-In-First-Out) replacement policy in each cache tier. +- Efficient data access management for large datasets and high-throughput applications. + +## Algorithm Explanation + +1. **Cache Initialization**: Allocate memory for each cache level with a predefined size (L1 and L2). +2. **Data Lookup**: + - Check if the data exists in the higher-priority cache (L1). + - If not in L1, search the lower-priority cache (L2). +3. **Data Movement**: + - If found in L2, move the data to L1 for quicker access in future requests. + - If not found in either cache, add it to both L1 and L2 caches. +4. **Replacement Policy**: Uses a First-In-First-Out (FIFO) approach for data replacement, removing the oldest entry when the cache is full. + + +### Input + +- **Data Requests**: An array of integers representing the data access requests. +- **L1 and L2 Cache Sizes**: Fixed cache sizes for each level (e.g., L1 with 3 slots, L2 with 5 slots). + +### Output + +The program will output the following for each request: +- Whether the requested data was found in L1, L2, or was not found. +- Any movement between cache levels when data is accessed. + +### Example Input and Output + +#### Example Input + +Requests: {10, 20, 10, 30, 40, 50, 20, 60, 70, 10} +L1 Cache Size: 3 +L2 Cache Size: 5 + +### Example Output + +Value 10 added to L1 and L2. +Value 20 added to L1 and L2. +Value 10 found in L1 cache. +Value 30 added to L1 and L2. +Value 40 added to L1 and L2. +Value 50 added to L1 and L2. +Value 20 found in L1 cache. +Value 60 added to L1 and L2. +Value 70 added to L1 and L2. +Value 10 moved from L2 to L1. From e5c7e8298dbb4d2d4f8886d9e583642727a3c678 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sun, 10 Nov 2024 15:01:33 +0530 Subject: [PATCH 5/6] Delete Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c --- .../Multi-Tiered Caching Algorithm/MTC.c | 91 ------------------- 1 file changed, 91 deletions(-) delete mode 100644 Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c diff --git a/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c b/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c deleted file mode 100644 index d8f6e9bd..00000000 --- a/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/MTC.c +++ /dev/null @@ -1,91 +0,0 @@ -#include -#include - -#define L1_CACHE_SIZE 3 -#define L2_CACHE_SIZE 5 - -typedef struct Cache { - int *data; - int size; - int count; -} Cache; - -// Initialize cache -Cache *initialize_cache(int size) { - Cache *cache = (Cache *)malloc(sizeof(Cache)); - cache->data = (int *)malloc(size * sizeof(int)); - cache->size = size; - cache->count = 0; - return cache; -} - -// Check if a value exists in cache and return its position -int find_in_cache(Cache *cache, int value) { - for (int i = 0; i < cache->count; i++) { - if (cache->data[i] == value) { - return i; - } - } - return -1; -} - -// Add value to cache with FIFO replacement -void add_to_cache(Cache *cache, int value) { - if (cache->count < cache->size) { - cache->data[cache->count++] = value; - } else { - // Shift data and add new value at the end - for (int i = 1; i < cache->size; i++) { - cache->data[i - 1] = cache->data[i]; - } - cache->data[cache->size - 1] = value; - } -} - -// Multi-tiered caching function -void multi_tiered_cache(Cache *L1, Cache *L2, int value) { - int pos_in_L1 = find_in_cache(L1, value); - int pos_in_L2 = find_in_cache(L2, value); - - if (pos_in_L1 != -1) { - printf("Value %d found in L1 cache.\n", value); - } else if (pos_in_L2 != -1) { - printf("Value %d found in L2 cache. Moving to L1.\n", value); - // Move from L2 to L1 cache - add_to_cache(L1, value); - // Remove from L2 (by shifting) - for (int i = pos_in_L2; i < L2->count - 1; i++) { - L2->data[i] = L2->data[i + 1]; - } - L2->count--; - } else { - printf("Value %d not found in L1 or L2. Adding to L1 and L2.\n", value); - add_to_cache(L1, value); - add_to_cache(L2, value); - } -} - -// Free allocated memory for cache -void free_cache(Cache *cache) { - free(cache->data); - free(cache); -} - -// Main function to test multi-tiered caching -int main() { - Cache *L1 = initialize_cache(L1_CACHE_SIZE); - Cache *L2 = initialize_cache(L2_CACHE_SIZE); - - int requests[] = {10, 20, 10, 30, 40, 50, 20, 60, 70, 10}; - int num_requests = sizeof(requests) / sizeof(requests[0]); - - for (int i = 0; i < num_requests; i++) { - multi_tiered_cache(L1, L2, requests[i]); - } - - // Free memory - free_cache(L1); - free_cache(L2); - - return 0; -} From 90ffbc824431a89963bbcf0c6d9ce94452d006fe Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sun, 10 Nov 2024 15:01:52 +0530 Subject: [PATCH 6/6] Delete Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md --- .../Multi-Tiered Caching Algorithm/Readme.md | 66 ------------------- 1 file changed, 66 deletions(-) delete mode 100644 Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md diff --git a/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md b/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md deleted file mode 100644 index 3e81d7e4..00000000 --- a/Miscellaneous Algorithms/Multi-Tiered Caching Algorithm/Readme.md +++ /dev/null @@ -1,66 +0,0 @@ -# Multi-Tiered Caching (MTC) Algorithm - -This project implements a Multi-Tiered Caching (MTC) algorithm in C. The MTC algorithm manages multiple cache levels to improve data retrieval efficiency by storing frequently accessed data in faster, higher-priority caches. It dynamically moves data between cache levels based on access patterns, reducing retrieval time and optimizing memory utilization in systems with large data workloads. - -## Table of Contents - -- [Overview](#overview) -- [Features](#features) -- [Algorithm Explanation](#algorithm-explanation) -- [Input and Output](#input-and-output) -- [Example](#example) - -## Overview - -The Multi-Tiered Caching (MTC) algorithm uses multiple cache levels (e.g., L1, L2) to store frequently accessed data closer to the processor, reducing data retrieval time. This approach is particularly useful for systems with limited memory and a high volume of data requests, as it minimizes access time and improves memory management. - -## Features - -- Multi-tiered caching system with multiple cache levels (e.g., L1 and L2). -- Caching based on access frequency, moving data between levels as needed. -- Simple FIFO (First-In-First-Out) replacement policy in each cache tier. -- Efficient data access management for large datasets and high-throughput applications. - -## Algorithm Explanation - -1. **Cache Initialization**: Allocate memory for each cache level with a predefined size (L1 and L2). -2. **Data Lookup**: - - Check if the data exists in the higher-priority cache (L1). - - If not in L1, search the lower-priority cache (L2). -3. **Data Movement**: - - If found in L2, move the data to L1 for quicker access in future requests. - - If not found in either cache, add it to both L1 and L2 caches. -4. **Replacement Policy**: Uses a First-In-First-Out (FIFO) approach for data replacement, removing the oldest entry when the cache is full. - - -### Input - -- **Data Requests**: An array of integers representing the data access requests. -- **L1 and L2 Cache Sizes**: Fixed cache sizes for each level (e.g., L1 with 3 slots, L2 with 5 slots). - -### Output - -The program will output the following for each request: -- Whether the requested data was found in L1, L2, or was not found. -- Any movement between cache levels when data is accessed. - -### Example Input and Output - -#### Example Input - -Requests: {10, 20, 10, 30, 40, 50, 20, 60, 70, 10} -L1 Cache Size: 3 -L2 Cache Size: 5 - -### Example Output - -Value 10 added to L1 and L2. -Value 20 added to L1 and L2. -Value 10 found in L1 cache. -Value 30 added to L1 and L2. -Value 40 added to L1 and L2. -Value 50 added to L1 and L2. -Value 20 found in L1 cache. -Value 60 added to L1 and L2. -Value 70 added to L1 and L2. -Value 10 moved from L2 to L1.