C program for C-SCAN disk Scheduling algorithm

C program for C-SCAN disk Scheduling algorithm

What is C-scan disk scheduling:-

In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its direction. So, it may be possible that too many requests are waiting at the other end or there may be zero or few requests pending at the scanned area. These situations are avoided in CSAN algorithm in which the disk arm instead of reversing its direction goes to the other end of the disk and starts servicing the requests from there. So, the disk arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and hence it is known as C-SCAN (Circular SCAN). 

Example:-  Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-write head initially at the track 50 and the tail track being at 199. head movement is towards low values.

C-scan disk scheduling

The C-SCAN algorithm dictates that the head moves in only one direction (in this case, towards the low-value end, 0) and services all requests in its path. When it reaches the end (0), it jumps immediately to the opposite end (199) without servicing any requests on the return jump. It then continues servicing from the opposite end in the same direction (towards low values) if any unserviced requests are present.

1. Sort the Requests

First, arrange all track requests in ascending order:
   Sorted Requests = {11, 34, 62, 64, 95, 119, 123, 180}

2. Service Requests Towards Low Values (Towards 0)

The head starts at 50 and moves towards 0. It services all requests in that direction, including the boundary track 0.
Current PositionNext Service/TrackHead Movement (Absolute Difference)
5034$
3411$
11→ 0 (End)$
Subtotal for Sweep 116 + 23 + 11 = 50$

3. Circular Jump to the High End

After reaching the low end (0), the head immediately jumps to the high end (199) without servicing any requests. This jump is counted in the total head movement in C-SCAN.

Current PositionNext Service/TrackHead Movement (Absolute Difference)
0→199 (Jump)$
Subtotal for Jump199

4. Service Remaining Requests (Continuing Towards Low Values)

The head is now at 199 and continues its sweep towards 0. It services the remaining requests in descending order.

Current PositionNext Service/TrackHead Movement (Absolute Difference)
199→180$
180 123$
123→ 119$
119 95$
95 64$
64 62$

5. Calculate Total Head Movement

The Total Head Movement is the sum of the movements from all steps.

Total Head Movement = (Movement to 0) + (Jump to 199) + (Movement from 199)
Total Head Movement = 50 + 199 + 137
Total Head Movement = 386 tracks

Advantage:-

  1. Uniform waiting time
  2. Better response time

Disadvantage:-

  1. In C-SCAN even if there are no requests left to be serviced the Head will still travel to the end of the disk unlike SCAN algorithm.

Code:-


#include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
        printf("Enter the number of Requests\n");
        scanf("%d",&n);
        printf("Enter the Requests sequence\n");
        for(i=0;i<n;i++)
         scanf("%d",&RQ[i]);
        printf("Enter initial head position\n");
        scanf("%d",&initial);
        printf("Enter total disk size\n");
        scanf("%d",&size);
        printf("Enter the head movement direction for high 1 and for low 0\n");
        scanf("%d",&move);
       
        // logic for C-Scan disk scheduling
       
            /*logic for sort the request array */
        for(i=0;i<n;i++)
        {
            for( j=0;j<n-i-1;j++)
            {
                if(RQ[j]>RQ[j+1])
                {
                    int temp;
                    temp=RQ[j];
                    RQ[j]=RQ[j+1];
                    RQ[j+1]=temp;
                }
   
            }
        }
   
        int index;
        for(i=0;i<n;i++)
        {
            if(initial<RQ[i])
            {
                index=i;
                break;
            }
        }
       
        // if movement is towards high value
        if(move==1)
        {
            for(i=index;i<n;i++)
            {
                TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
                initial=RQ[i];
            }
            //  last movement for max size
            TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
            /*movement max to min disk */
            TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
            initial=0;
            for( i=0;i<index;i++)
            {
                 TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
                 initial=RQ[i];
               
            }
        }
        // if movement is towards low value
        else
        {
            for(i=index-1;i>=0;i--)
            {
                TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
                initial=RQ[i];
            }
            //  last movement for min size
            TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
            /*movement min to max disk */
            TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
            initial =size-1;
            for(i=n-1;i>=index;i--)
            {
                 TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
                 initial=RQ[i];
               
            }
        }
       
        printf("Total head movement is %d",TotalHeadMoment);
        return 0;
    }
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;

    cout << "Enter the number of Requests" << endl;
    cin >> n;

    cout << "Enter the Requests sequence" << endl;
    for (i = 0; i < n; i++)
        cin >> RQ[i];

    cout << "Enter initial head position" << endl;
    cin >> initial;

    cout << "Enter total disk size" << endl;
    cin >> size;

    cout << "Enter the head movement direction for high 1 and for low 0" << endl;
    cin >> move;

    // Logic for C-Scan disk scheduling

    // Logic for sorting the request array
    for (i = 0; i < n; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (RQ[j] > RQ[j + 1]) {
                int temp;
                temp = RQ[j];
                RQ[j] = RQ[j + 1];
                RQ[j + 1] = temp;
            }
        }
    }

    int index;
    for (i = 0; i < n; i++) {
        if (initial < RQ[i]) {
            index = i;
            break;
        }
    }

    // If movement is towards high value
    if (move == 1) {
        for (i = index; i < n; i++) {
            TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
            initial = RQ[i];
        }
        // Last movement for max size
        TotalHeadMoment = TotalHeadMoment + abs(size - RQ[i - 1] - 1);
        // Movement max to min disk
        TotalHeadMoment = TotalHeadMoment + abs(size - 1 - 0);
        initial = 0;
        for (i = 0; i < index; i++) {
            TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
            initial = RQ[i];
        }
    }
    // If movement is towards low value
    else {
        for (i = index - 1; i >= 0; i--) {
            TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
            initial = RQ[i];
        }
        // Last movement for min size
        TotalHeadMoment = TotalHeadMoment + abs(RQ[i + 1] - 0);
        // Movement min to max disk
        TotalHeadMoment = TotalHeadMoment + abs(size - 1 - 0);
        initial = size - 1;
        for (i = n - 1; i >= index; i--) {
            TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
            initial = RQ[i];
        }
    }

    cout << "Total head movement is " << TotalHeadMoment << endl;
    return 0;
}

def main():
    RQ = []
    TotalHeadMoment = 0

    n = int(input("Enter the number of Requests\n"))
    print("Enter the Requests sequence")
    for _ in range(n):
        RQ.append(int(input()))

    initial = int(input("Enter initial head position\n"))
    size = int(input("Enter total disk size\n"))
    move = int(input("Enter the head movement direction for high 1 and for low 0\n"))

    # Logic for C-Scan disk scheduling

    # Logic for sorting the request array
    RQ.sort()

    index = 0
    for i in range(n):
        if initial < RQ[i]:
            index = i
            break

    # If movement is towards high value
    if move == 1:
        for i in range(index, n):
            TotalHeadMoment += abs(RQ[i] - initial)
            initial = RQ[i]
        # Last movement for max size
        TotalHeadMoment += abs(size - RQ[i - 1] - 1)
        # Movement max to min disk
        TotalHeadMoment += abs(size - 1 - 0)
        initial = 0
        for i in range(index):
            TotalHeadMoment += abs(RQ[i] - initial)
            initial = RQ[i]
    # If movement is towards low value
    else:
        for i in range(index - 1, -1, -1):
            TotalHeadMoment += abs(RQ[i] - initial)
            initial = RQ[i]
        # Last movement for min size
        TotalHeadMoment += abs(RQ[i + 1] - 0)
        # Movement min to max disk
        TotalHeadMoment += abs(size - 1 - 0)
        initial = size - 1
        for i in range(n - 1, index - 1, -1):
            TotalHeadMoment += abs(RQ[i] - initial)
            initial = RQ[i]

    print(f"Total head movement is {TotalHeadMoment}")


if __name__ == "__main__":
    main()

Text Copied!

Output:-


Enter the number of Request
8
Enter the Requests Sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0
1
Total head movement is 382

easycodingzone

Program of disk scheduling algorithm


easycodingzone

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-