Header Ads Widget

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.


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:-