LOOK Disk Scheduling Algorithm

 

LOOK Disk Scheduling Algorithm

Aim

To simulate the LOOK disk scheduling algorithm and calculate the total head movement with an example.


Theory

LOOK is an improved version of SCAN.

  • In SCAN, the head moves to the disk end (0 or max track) even if no request exists there.

  • In LOOK, the head only goes as far as the last request in that direction, then reverses.

👉 It “looks ahead” for pending requests before moving further — hence the name LOOK.


Key Idea

SCANLOOK
Goes to disk end always        Stops at last request
Extra unnecessary movement        Avoids unnecessary movement
Less efficient        More efficient

Algorithm

  1. Read disk requests.

  2. Read initial head position.

  3. Sort the request queue.

  4. Choose direction (LEFT or RIGHT).

  5. If RIGHT:

    • Service all requests greater than head in ascending order.

    • Reverse direction.

    • Service remaining smaller requests in descending order.

  6. If LEFT:

    • Service smaller requests first.

    • Reverse and service larger ones.

  7. Sum total head movement.


Program: C-SCAN Disk Scheduling (C)

#include <stdio.h>
#include <stdlib.h>

void sort(int a[], int n){
    for(int i=0;i<n-1;i++)
        for(int j=0;j<n-i-1;j++)
            if(a[j]>a[j+1]){
                int t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
}

int main(){
    int n, head, dir;
    int total = 0;

    printf("Enter number of requests: ");
    scanf("%d",&n);

    int req[n];

    printf("Enter the requests:\n");
    for(int i=0;i<n;i++)
        scanf("%d",&req[i]);

    printf("Enter initial head position: ");
    scanf("%d",&head);

    printf("Enter direction (1 = RIGHT, 0 = LEFT): ");
    scanf("%d",&dir);

    sort(req,n);

    printf("\n-------------------------------------------\n");
    printf("Step\tFrom\tTo\tMovement\n");
    printf("-------------------------------------------\n");

    int cur = head, step = 1;

    if(dir==1){   // RIGHT FIRST

        for(int i=0;i<n;i++){
            if(req[i] >= head){
                int move = abs(cur - req[i]);
                printf("%d\t%d\t%d\t%d\n", step, cur, req[i], move);
                total += move;
                cur = req[i];
                step++;
            }
        }

        for(int i=n-1;i>=0;i--){
            if(req[i] < head){
                int move = abs(cur - req[i]);
                printf("%d\t%d\t%d\t%d\n", step, cur, req[i], move);
                total += move;
                cur = req[i];
                step++;
            }
        }

    } else {   // LEFT FIRST

        for(int i=n-1;i>=0;i--){
            if(req[i] <= head){
                int move = abs(cur - req[i]);
                printf("%d\t%d\t%d\t%d\n", step, cur, req[i], move);
                total += move;
                cur = req[i];
                step++;
            }
        }

        for(int i=0;i<n;i++){
            if(req[i] > head){
                int move = abs(cur - req[i]);
                printf("%d\t%d\t%d\t%d\n", step, cur, req[i], move);
                total += move;
                cur = req[i];
                step++;
            }
        }
    }

    printf("-------------------------------------------\n");
    printf("Total Head Movement = %d\n", total);
    printf("-------------------------------------------\n");

    return 0;
}

Example Run

Input

Enter number of requests: 8 Enter the requests: 98 183 37 122 14 124 65 67 Enter initial head position: 53 Enter direction (1 = RIGHT, 0 = LEFT): 1

Step 1: Sort Requests

14 37 65 67 98 122 124 183

Step 2: Service RIGHT side first

Requests greater than 53:

65 → 67 → 98 → 122 → 124 → 183

Head moves:

53 → 65 → 67 → 98 → 122 → 124 → 183


Step 3: Reverse direction

Remaining requests:

37 → 14

Head moves:

183 → 37 → 14


Sample Run Table

Step From To Movement -------------------------------- 1 53 65 12 2 65 67 2 3 67 98 31 4 98 122 24 5 122 124 2 6 124 183 59 7 183 37 146 8 37 14 23 -------------------------------- Total head movement = 299

Comparison with SCAN

Algorithm    Movement
SCAN        331
LOOK        299

👉 LOOK saves movement because it does NOT go to track 199.


Advantages

  • Avoids unnecessary movement to disk ends

  • Better performance than SCAN

  • Fair servicing of requests

  • Reduced seek time


Disadvantages

  • Slightly more complex than FCFS

  • Still direction-based (not optimal like SSTF)


Result

The LOOK disk scheduling algorithm was successfully simulated.
For the given example, the total head movement = 299 tracks, which is less than SCAN.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

FCFS First Come First Serve - CPU Scheduling