From 6ff189473261af82a49054db58a07ca14febba72 Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 11 Apr 2025 08:30:13 +0530 Subject: [PATCH] modified and updated --- OS/C/Week11/q1.c | 201 ++++++++++++++++++++++++++++++++++++ OS/C/Week11/qq1.c | 234 ++++++++++++++++++++++++++++++++++++++++++ OS/C/Week11/test.c | 0 OS/C/Week12/rtos.c | 153 +++++++++++++++++++++++++++ OS/C/Week9/q1-smol.c | 81 +++++++++++++++ OS/C/practice/test.sh | 12 +-- 6 files changed, 674 insertions(+), 7 deletions(-) create mode 100644 OS/C/Week11/q1.c create mode 100644 OS/C/Week11/qq1.c create mode 100644 OS/C/Week11/test.c create mode 100644 OS/C/Week12/rtos.c create mode 100644 OS/C/Week9/q1-smol.c diff --git a/OS/C/Week11/q1.c b/OS/C/Week11/q1.c new file mode 100644 index 0000000..5a7cbfb --- /dev/null +++ b/OS/C/Week11/q1.c @@ -0,0 +1,201 @@ +#include +#include +#include +#define MAX 100 + +// Function for SSTF (Shortest Seek Time First) +void sstf(int arr[], int n, int head) { + int visited[MAX] = {0}, total_seek = 0, current = head; + printf("\nSSTF Sequence:\n%d ", current); + for (int count = 0; count < n; count++) { + int index = -1, minDist = 1e9; + for (int i = 0; i < n; i++) { + if (!visited[i]) { + int dist = abs(arr[i] - current); + if (dist < minDist) { + minDist = dist; + index = i; + } + } + } + visited[index] = 1; + total_seek += minDist; + current = arr[index]; + printf("-> %d ", current); + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +// Helper function: Bubble sort in ascending order +void sortAsc(int arr[], int n) { + for (int i = 0; i < n - 1; i++) + for (int j = i + 1; j < n; j++) + if (arr[i] > arr[j]) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +// Helper function: Bubble sort in descending order +void sortDesc(int arr[], int + for (int i = 0; i < n - 1; i++) + for (int j = i + 1; j < n; j++) + if (arr[i] < arr[j]) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +// Function for SCAN (Elevator Algorithm) +// Assumes movement is towards the left first, then reverses. +void scan(int arr[], int n, int head, int disk_size) { + int left[MAX], right[MAX], l = 0, r = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < head) + left[l++] = arr[i]; + else + right[r++] = arr[i]; + } + sortDesc(left, l); + sortAsc(right, r); + int total_seek = 0, current = head; + printf("\nSCAN Sequence:\n%d ", current); + // Service left side (moving toward 0) + for (int i = 0; i < l; i++) { + total_seek += abs(current - left[i]); + current = left[i]; + printf("-> %d ", current); + } + // If not already at 0, move to 0 + if (current != 0) { + total_seek += current; + current = 0; + printf("-> %d ", current); + } + // Then service right side (moving right) + for (int i = 0; i < r; i++) { + total_seek += abs(right[i] - current); + current = right[i]; + printf("-> %d ", current); + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +// Function for C-SCAN (Circular SCAN) +// Assumes movement to the right; after reaching the end, the head jumps to 0. +void cscan(int arr[], int n, int head, int disk_size) { + int left[MAX], right[MAX], l = 0, r = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < head) + left[l++] = arr[i]; + else + right[r++] = arr[i]; + } + sortAsc(left, l); + sortAsc(right, r); + int total_seek = 0, current = head; + printf("\nC-SCAN Sequence:\n%d ", current); + // Service requests to the right + for (int i = 0; i < r; i++) { + total_seek += abs(right[i] - current); + current = right[i]; + printf("-> %d ", current); + } + // Go to the end if not reached + if (current != disk_size - 1) { + total_seek += abs(disk_size - 1 - current); + current = disk_size - 1; + printf("-> %d ", current); + } + // Jump to beginning (simulate wrap-around) + total_seek += (disk_size - 1); + current = 0; + printf("-> %d ", current); + // Service the left side + for (int i = 0; i < l; i++) { + total_seek += abs(left[i] - current); + current = left[i]; + printf("-> %d ", current); + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +// Function for C-LOOK +// Assumes movement to the right; after the furthest request, it jumps to the smallest request. +void clook(int arr[], int n, int head) { + int left[MAX], right[MAX], l = 0, r = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < head) + left[l++] = arr[i]; + else + right[r++] = arr[i]; + } + sortAsc(left, l); + sortAsc(right, r); + int total_seek = 0, current = head; + printf("\nC-LOOK Sequence:\n%d ", current); + // Service the right side + for (int i = 0; i < r; i++) { + total_seek += abs(right[i] - current); + current = right[i]; + printf("-> %d ", current); + } + // Jump to the leftmost request if any exist on the left + if (l > 0) { + total_seek += abs(current - left[0]); + current = left[0]; + printf("-> %d ", current); + for (int i = 1; i < l; i++) { + total_seek += abs(left[i] - current); + current = left[i]; + printf("-> %d ", current); + } + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +int main() { + int choice, n, head, disk_size; + int arr[MAX]; + printf("Menu:\n"); + printf("1. SSTF\n"); + printf("2. SCAN\n"); + printf("3. C-SCAN\n"); + printf("4. C-LOOK\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + printf("Enter number of requests: "); + scanf("%d", &n); + printf("Enter the request queue (space separated): "); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + printf("Enter initial head position: "); + scanf("%d", &head); + + if (ce == 2 || choice == 3) { // SCAN and C-SCAN require the disk size + printf("Enter disk size: "); + scanf("%d", &disk_size); + } + + switch (choice) { + case 1: + sstf(arr, n, head); + break; + case 2: + scan(arr, n, head, disk_size); + break; + case 3: + cscan(arr, n, head, disk_size); + break; + case 4: + clook(arr, n, head); + break; + default: + printf("Invalid choice!\n"); + } + return 0; +} diff --git a/OS/C/Week11/qq1.c b/OS/C/Week11/qq1.c new file mode 100644 index 0000000..53d3daa --- /dev/null +++ b/OS/C/Week11/qq1.c @@ -0,0 +1,234 @@ +#include +#include +#include + +void sstf(int requests[], int n, int head) { + int total_seek = 0; + int completed = 0; + int visited[100] = {0}; + int current = head; + + printf("\nSSTF Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + while (completed < n) { + int min_distance = INT_MAX; + int min_index = -1; + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + int distance = abs(requests[i] - current); + if (distance < min_distance) { + min_distance = distance; + min_index = i; + } + } + } + + visited[min_index] = 1; + current = requests[min_index]; + total_seek += min_distance; + completed++; + printf(" -> %d", current); + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +void scan(int requests[], int n, int head, int disk_size) { + int total_seek = 0; + int direction = 1; // 1 for moving right, 0 for moving left + int current = head; + + printf("\nSCAN Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + // Sort requests + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (requests[j] > requests[j + 1]) { + int temp = requests[j]; + requests[j] = requests[j + 1]; + requests[j + 1] = temp; + } + } + } + + // Find position of head in sorted array + int index; + for (index = 0; index < n; index++) { + if (requests[index] >= head) + break; + } + + // Move right + for (int i = index; i < n; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + // Move to the end of disk + printf(" -> %d", disk_size - 1); + total_seek += abs(disk_size - 1 - head); + head = disk_size - 1; + + // Move left + for (int i = index - 1; i >= 0; i--) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +void cscan(int requests[], int n, int head, int disk_size) { + int total_seek = 0; + int current = head; + + printf("\nC-SCAN Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + // Sort requests + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (requests[j] > requests[j + 1]) { + int temp = requests[j]; + requests[j] = requests[j + 1]; + requests[j + 1] = temp; + } + } + } + + // Find position of head in sorted array + int index; + for (index = 0; index < n; index++) { + if (requests[index] >= head) + break; + } + + // Move right + for (int i = index; i < n; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + // Move to the end of disk + printf(" -> %d", disk_size - 1); + total_seek += abs(disk_size - 1 - head); + + // Move to the beginning + printf(" -> 0"); + total_seek += disk_size - 1; + head = 0; + + // Move right again + for (int i = 0; i < index; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +void clook(int requests[], int n, int head) { + int total_seek = 0; + int current = head; + + printf("\nC-LOOK Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + // Sort requests + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (requests[j] > requests[j + 1]) { + int temp = requests[j]; + requests[j] = requests[j + 1]; + requests[j + 1] = temp; + } + } + } + + // Find position of head in sorted array + int index; + for (index = 0; index < n; index++) { + if (requests[index] >= head) + break; + } + + // Move right + for (int i = index; i < n; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + // Move to first request + for (int i = 0; i < index; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +int main() { + int requests[100], n, head, disk_size, choice; + + 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 head position: "); + scanf("%d", &head); + + printf("Enter the disk size (0 to size-1): "); + scanf("%d", &disk_size); + + do { + printf("\n\nDisk Scheduling Algorithms\n"); + printf("1. SSTF (Shortest Seek Time First)\n"); + printf("2. SCAN\n"); + printf("3. C-SCAN\n"); + printf("4. C-LOOK\n"); + printf("5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + sstf(requests, n, head); + break; + case 2: + scan(requests, n, head, disk_size); + break; + case 3: + cscan(requests, n, head, disk_size); + break; + case 4: + clook(requests, n, head); + break; + case 5: + printf("Exiting program...\n"); + break; + default: + printf("Invalid choice!\n"); + } + } while (choice != 5); + + return 0; +} diff --git a/OS/C/Week11/test.c b/OS/C/Week11/test.c new file mode 100644 index 0000000..e69de29 diff --git a/OS/C/Week12/rtos.c b/OS/C/Week12/rtos.c new file mode 100644 index 0000000..13afc5a --- /dev/null +++ b/OS/C/Week12/rtos.c @@ -0,0 +1,153 @@ +#include + +// Define Task structure (simplified for memorization) +typedef struct { + int id; // Task ID + int period; // Period (also deadline for simplicity) + int execution_time; // Worst-case execution time (WCET) + // --- Simulation State --- + int remaining_execution; // Remaining execution time for current instance + int absolute_deadline; // Absolute deadline for current instance + int time_to_arrival; // Time until the next instance arrives/is released +} Task; + +// --- Global Variables --- +// Define the tasks for the simulation (Example Set) +// Format: {id, Period, ExecutionTime, 0, 0, 0} <-- Initial state values +Task tasks[] = { + {1, 5, 2, 0, 0, 0}, // Task 1: Period=5, Exec Time=2 + {2, 8, 3, 0, 0, 0} // Task 2: Period=8, Exec Time=3 + // Add more tasks here if needed +}; +// Calculate number of tasks automatically +int num_tasks = sizeof(tasks) / sizeof(Task); +// Set simulation duration (e.g., Hyperperiod or a fixed time) +// LCM(5, 8) = 40 +int simulation_time = 40; + +// --- Rate Monotonic (RM) Simulation --- +void simulate_rm() { + printf("--- Rate Monotonic Scheduling ---\n"); + // Reset task states for the simulation run + for (int i = 0; i < num_tasks; i++) { + tasks[i].remaining_execution = 0; + tasks[i].absolute_deadline = 0; + tasks[i].time_to_arrival = 0; // All tasks start at time 0 + } + + // Main simulation loop + for (int time = 0; time < simulation_time; time++) { + // 1. Check for task arrivals (release time) + for (int i = 0; i < num_tasks; i++) { + if (tasks[i].time_to_arrival == 0) { + // Check if the previous instance of this task missed its deadline + if (tasks[i].remaining_execution > 0) { + printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id); + // Simple handling: Continue with the new instance, old one is lost + } + // Release new instance of the task + tasks[i].remaining_execution = tasks[i].execution_time; + tasks[i].absolute_deadline = time + tasks[i].period; // Deadline = Period + tasks[i].time_to_arrival = tasks[i].period; // Set timer for the *next* arrival + } + tasks[i].time_to_arrival--; // Decrement time until the next arrival for all tasks + } + + // 2. Select highest priority task to run (RM: Shortest Period has highest priority) + int task_to_run = -1; // -1 indicates CPU Idle + int highest_priority = 10000; // Initialize with a low priority (large period) + + for (int i = 0; i < num_tasks; i++) { + // Check if task is ready (has arrived and needs execution) + if (tasks[i].remaining_execution > 0) { + // RM priority check: Lower period value means higher priority + if (tasks[i].period < highest_priority) { + highest_priority = tasks[i].period; + task_to_run = i; // Select this task + } + } + } + + // 3. Execute the selected task (or remain idle) + if (task_to_run != -1) { + // Task selected to run + printf("Time %d: Task %d running\n", time, tasks[task_to_run].id); + tasks[task_to_run].remaining_execution--; // Execute for one time unit + + // Optional: Check if task just finished + // if (tasks[task_to_run].remaining_execution == 0) { + // printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id); + // } + } else { + // No task ready to run + printf("Time %d: CPU Idle\n", time); + } + } + printf("--- RM Simulation Complete ---\n"); +} + +// --- Earliest Deadline First (EDF) Simulation --- +void simulate_edf() { + printf("\n--- Earliest Deadline First Scheduling ---\n"); + // Reset task states + for (int i = 0; i < num_tasks; i++) { + tasks[i].remaining_execution = 0; + tasks[i].absolute_deadline = 0; + tasks[i].time_to_arrival = 0; + } + + // Main simulation loop + for (int time = 0; time < simulation_time; time++) { + // 1. Check for task arrivals (same as RM) + for (int i = 0; i < num_tasks; i++) { + if (tasks[i].time_to_arrival == 0) { + if (tasks[i].remaining_execution > 0) { + printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id); + } + tasks[i].remaining_execution = tasks[i].execution_time; + tasks[i].absolute_deadline = time + tasks[i].period; + tasks[i].time_to_arrival = tasks[i].period; + } + tasks[i].time_to_arrival--; + } + + // 2. Select highest priority task to run (EDF: Earliest Absolute Deadline has highest priority) + int task_to_run = -1; + int earliest_deadline = 10000; // Initialize with a late deadline + + for (int i = 0; i < num_tasks; i++) { + // Check if task is ready + if (tasks[i].remaining_execution > 0) { + // EDF priority check: Lower deadline value means higher priority (earlier deadline) + if (tasks[i].absolute_deadline < earliest_deadline) { + earliest_deadline = tasks[i].absolute_deadline; + task_to_run = i; // Select this task + } + } + } + + // 3. Execute the selected task (same as RM) + if (task_to_run != -1) { + printf("Time %d: Task %d running\n", time, tasks[task_to_run].id); + tasks[task_to_run].remaining_execution--; + // Optional: Check finish + // if (tasks[task_to_run].remaining_execution == 0) { + // printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id); + // } + } else { + printf("Time %d: CPU Idle\n", time); + } + } + printf("--- EDF Simulation Complete ---\n"); +} + +// --- Main Function --- +int main() { + // Run Rate Monotonic simulation + simulate_rm(); + + // Run Earliest Deadline First simulation + simulate_edf(); + + return 0; // Indicate successful execution +} diff --git a/OS/C/Week9/q1-smol.c b/OS/C/Week9/q1-smol.c new file mode 100644 index 0000000..a3aac36 --- /dev/null +++ b/OS/C/Week9/q1-smol.c @@ -0,0 +1,81 @@ +#include +#include + +#define BLOCKS 4 +#define REQUESTS 5 + +// Memory configuration +int memory[BLOCKS] = {100, 50, 25, 10}; +int allocated[BLOCKS] = {0, 0, 0, 0}; + +// Helper: Reset allocation state +void resetAllocation() { + for(int i = 0; i < BLOCKS; i++) { + allocated[i] = 0; + } +} + +// Print memory status +void printMemory() { + printf("\nMemory Status:\n"); + for(int i = 0; i < BLOCKS; i++) { + printf("[Size: %d, %s] -> ", memory[i], + allocated[i] ? "Allocated" : "Free"); + } + printf("NULL\n\n"); +} + +// First Fit allocation +void firstFit(int size) { + for(int i = 0; i < BLOCKS; i++) { + if (!allocated[i] && memory[i] >= size) { + allocated[i] = 1; + printf("Allocated %d bytes using First Fit\n", size); + return; + } + } + printf("First Fit: No suitable block found for %d bytes\n", size); +} + +// Best Fit allocation +void bestFit(int size) { + int best = -1; + int bestSize = 999999; + + for(int i = 0; i < BLOCKS; i++) { + if(!allocated[i] && memory[i] >= size && memory[i] < bestSize) { + bestSize = memory[i]; + best = i; + } + } + + if(best != -1) { + allocated[best] = 1; + printf("Allocated %d bytes using Best Fit\n", size); + } else { + printf("Best Fit: No suitable block found for %d bytes\n", size); + } +} + +// Main function: run allocation sequence +int main() { + int requests[REQUESTS] = {15, 35, 60, 10, 5}; + + printf("=== FIRST FIT ===\n"); + printMemory(); + for(int i = 0; i < REQUESTS; i++) { + firstFit(requests[i]); + printMemory(); + } + + resetAllocation(); + + printf("=== BEST FIT ===\n"); + printMemory(); + for(int i = 0; i < REQUESTS; i++) { + bestFit(requests[i]); + printMemory(); + } + + return 0; +} diff --git a/OS/C/practice/test.sh b/OS/C/practice/test.sh index 57fa256..e94be5a 100755 --- a/OS/C/practice/test.sh +++ b/OS/C/practice/test.sh @@ -1,8 +1,6 @@ -read -p "Enter a number: " num -if [[ $num -gt 10 ]]; then - echo "Number is greater than 10" -elif [[ $num -eq 10 ]]; then - echo "Number is exactly 10" -else - echo "Number is less than 10" +if [[ $# -lt 3 ]]; then + echo "Usage: $0 " + exit 1 fi + +if