201 lines
		
	
	
	
		
			5.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			201 lines
		
	
	
	
		
			5.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#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;
 | 
						|
}
 |