C Program for LOOK Disk Scheduling Algorithm

Similar to SCAN, the LOOK disk scheduling method responds to requests in one way before reversing. Let’s look at the LOOK disk scheduling method C program.

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

void sort(int arr[], int n) {

    for (int i = 0; i < n; 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;

            }

        }

    }

}

int main() {

    int n, head, max, total = 0, direction;

    printf("Enter the number of requests: ");

    scanf("%d", &n);

    printf("Enter the requests: ");

    int *requests = (int *)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++) {

        scanf("%d", &requests[i]);

    }

 printf("Enter initial head position: ");

    scanf("%d", &head);

    printf("Enter disk size (maximum track number): ");

    scanf("%d", &max);

    printf("Enter the direction (0 for low, 1 for high): ");

    scanf("%d", &direction);

    sort(requests, n);  // Sort the request array

    int index = 0;  // Find the closest index in the sorted array that the head should go to

    while (index < n && requests[index] < head) {

        index++;

    }

    int seek_sequence[n + 1], seq_index = 0; // to store the seek sequence

    printf("\nSequence of head movement:\n%d -> ", head);

    if (direction == 1) { // head moving towards higher track numbers

        for (int i = index; i < n; i++) {

            total += abs(head - requests[i]);

            head = requests[i];

            printf("%d -> ", head);

        }

        for (int i = index - 1; i >= 0; i--) {

            total += abs(head - requests[i]);

            head = requests[i];

            printf("%d", head);

            if (i > 0) printf(" -> ");

        }

    } else { // head moving towards lower track numbers

        for (int i = index - 1; i >= 0; i--) {

            total += abs(head - requests[i]);

            head = requests[i];

            printf("%d -> ", head);

        }

        for (int i = index; i < n; i++) {

            total += abs(head - requests[i]);

            head = requests[i];

            printf("%d", head);

            if (i < n - 1) printf(" -> ");

        }

    }

    printf("\nTotal head movement: %d cylinders.\n", total);

    free(requests);

    return 0;

}

Output of Look disk program in C

C Program for LOOK Disk Scheduling Algorithm

Explanation of the Program

  1. Input
    • n: Number of disk requests.
    • requests[]: The track numbers to visit.
    • head: Starting position of the disk head.
    • max: The maximum track number (disk size).
    • direction: Initial direction of head movement (0 for towards 0, 1 for towards max).
  2. Processing
    • The requests are sorted.
    • The disk head moves in the specified initial direction by servicing all requests in its path.
    • Upon reaching the last request in one direction, it reverses and services the remaining requests.
  3. Output
    • The sequence of head movements and the total head movement are displayed.

Working of LOOK Disk scheduling algorithm

  • The disk head starts at the initial position and moves in a specified direction (either towards the smallest request to largest or largest to smallest, depending on initialization).
  • It services all requests that come in its path.
  • Upon reaching the last request in that direction, the head reverses its direction, and services requests moving in the opposite direction.