modified and updated

This commit is contained in:
sherlock 2025-04-11 08:30:13 +05:30
parent 44e766514e
commit 6ff1894732
6 changed files with 674 additions and 7 deletions

201
OS/C/Week11/q1.c Normal file
View file

@ -0,0 +1,201 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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;
}

234
OS/C/Week11/qq1.c Normal file
View file

@ -0,0 +1,234 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
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;
}

0
OS/C/Week11/test.c Normal file
View file