C Program to Simulate C-SCAN Disk Scheduling Algorithm | Logic Explained

C-SCAN Algorithm - C Program by Nived Kannada

C SCAN algorithm is actually a slightly modified version of SCAN algorithm which are both used for disk scheduling in the operating system.
There is only a few changes from the SCAN algorithm.
If you haven't read our article on the SCAN algorithm, please read and understand the logic of it before reading this post.

Web Design for Beginners in Malayalam - Full Course (മലയാളം)

Anyway, the logic is pretty simple,
  • Array queue[] is our main array
  • Array queue1[] and queue2[] are used to store the requested positions greater than initial head position and less than initial head position respectively.
  • Variables temp1 and temp2 are used as the index variables of queue1[] and queue2[] respectively.
  • The main difference from SCAN algorithm is that in SCAN algorithm, we sort the first array queue1[] in the ascending order and the second array queue2[] in the descending order, but in C-SCAN algorithm, we sort both of them in their ascending order. 
  • One slight difference comes in the steps when copying arrays queue1[] and queue2[] to the main array queue[]
  • After copying first array queue1[] to queue[], we have to set maxrange position as the next request position. ie. We set queue[i]=maxrange.
  • After that, since it is Circular scan, after reaching the outermost cylinder/track, the head moves to the innermost track/cylinder, ie. the position 0. So, after pushing maxrange, increment i and put the value 0 in there.
  • We have to copy the next array queue2[] after doing these. 
  • You can understand the rest of the logic from the comments given on the code itself.
  • Please go through the comments and understand them. 

Recommended Article: C Program to Simulate C-LOOK Disk Scheduling Algorithm | Logic Explained

Open in OnlineGDB

If you find this article helpful, please like this article and support us by sharing it with your friends:

// C Program to Simulate C-SCAN Disk Scheduling Algorithm
// Visit www.nanogalaxy.org for more programs.

int absoluteValue(int); // Declaring function absoluteValue

void main()
    int queue[25],n,headposition,i,j,k,seek=0, maxrange,
    float averageSeekTime;

    // Reading the maximum Range of the Disk. 
    printf("Enter the maximum range of Disk: ");
    // Reading the number of Queue Requests(Disk access requests)
    printf("Enter the number of queue requests: ");
    // Reading the initial head position.(ie. the starting point of execution)
    printf("Enter the initial head position: ");
    // Reading disk positions to be read in the order of arrival
    printf("Enter the disk positions to be read(queue): ");
    for(i=1;i<=n;i++)   // Note that i varies from 1 to n instead of 0 to n-1
        scanf("%d",&temp);  //Reading position value to a temporary variable
        //Now if the requested position is greater than current headposition,
        //then pushing that to array queue1
            queue1[temp1]=temp; //temp1 is the index variable of queue1[]
            temp1++; //incrementing temp1
        else    //else if temp < current headposition,then push to array queue2[]
            queue2[temp2]=temp; //temp2 is the index variable of queue2[]
    //Now we have to sort the two arrays
    //SORTING array queue1[] in ascending order
    //SORTING array queue2[] in ascending order
    //Copying first array queue1[] into queue[]
    //Setting queue[i] to maxrange because the head goes to
    //end of disk and comes back in scan Algorithm
    //Moving Disk head to the inner most cylinder, because this is Circular Scan.
    //Copying second array queue2[] after that first one is copied, into queue[]
    //At this point, we have the queue[] with the requests in the 
    //correct order of execution as per C-SCAN algorithm.
    //Now we have to set 0th index of queue[] to be the initial headposition. 
    // Calculating SEEK TIME. seek is initially set to 0 in the declaration part.
    for(j=0; j<=n+1; j++) //Loop starts from headposition. (ie. 0th index of queue) 
        // Finding the difference between next position and current position.
        difference = absoluteValue(queue[j+1]-queue[j]);
        // Adding difference to the current seek time value
        seek = seek + difference;
        // Displaying a message to show the movement of disk head
        printf("Disk head moves from position %d to %d with Seek %d \n",
 queue[j], queue[j+1], difference);
    // Calculating Average Seek time 
    averageSeekTime = seek/(float)n;
    //Display Total and Average Seek Time(s)
    printf("Total Seek Time= %d\n", seek);
    printf("Average Seek Time= %f\n", averageSeekTime);

// Defining function absoluteValue
int absoluteValue(int x)
        return x;
        return x*-1;

//Code by Nived Kannada


Sample Output of the program

If you have any doubts regarding this program, please let us know in the comments section below.
Happy coding!

No comments:

Post a Comment


Nanogalaxy is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for website owners to earn advertising fees by advertising and linking to amazon.com and any other website that may be affiliated with Amazon Service LLC Associates Program.