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

C-LOOK Algorithm - C Program by Nived Kannada

In the previous articles, we have seen that the SCAN algorithm and the LOOK algorithm are very similar and have only small differences in servicing the disk read requests. It is pretty much like that in the case of the Circular LOOK algorithm and the Circular Scan algorithm. If the LOOK algorithm is a slightly modified and more efficient version of the SCAN algorithm, we can consider C-LOOK (Circular LOOK) algorithm as a slightly modified and more efficient version of the C-SCAN (Circular SCAN) algorithm

That is, it has the properties of the LOOK algorithm which is mainly that the disk head does not go all the way to the extreme end cylinders of the disk unless it has to read those portions. In the case of SCAN and C-SCAN algorithms, we saw that the disk head moves to the outermost and the innermost cylinders(not necessarily goes to the innermost always(depending on the initial direction of disk head movement)) even if they didn't have to read those disk cylinders. 

So, in the case of the C-LOOK algorithm, the disk head moves to the outermost cylinder which is in the request queue, then it switches direction and goes to the innermost cylinder which is in the request queue and then it moves outwards again. 

For example, assume that the max range is 100, the request queue is [35, 75, 15, 80] and the disk head initial position is 50. Also assume that the disk head initially moves outwards initially for the sake of simplicity.

Then the requests will be served in the following order: 50 → 75 → 80 → 15 → 35

ie. The disk head does not go to 100 or 0 (which are the extreme ends of the disk range) unless those parts are requested. 

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

The Logic of the code below is pretty simple,

  • There is the main array called queue[] and two sub-arrays queue1[] and queue2[]
  • Requests are read from user input. 
  • Requests larger than the initial head position are stored in the array queue1[] and the requests smaller than the initial head position are stored in the array queue2[]
  • Two variables temp1 and temp2 are used for the arrays queue1[] and queue2[] respectively.
  • The main difference from the LOOK algorithm is that in the LOOK algorithm, we sort the first array queue1[] in the ascending order and the second array queue2[] in the descending order, but in the C-LOOK algorithm, we sort both of them in the ascending order.
  • A small difference comes in the steps when copying arrays queue1[] and queue2[] to the main array queue[]
  • After copying first array queue1[] to queue[], we set next position of queue[] as the first element of array queue2[] (which is already sorted in the ascending order, so the least value will be the first element of queue2[])
    ie. We set queue[i]=queue2[0]
  • After that we have to copy the rest of the array queue2[] to the main array queue[]
  • You can understand the rest of the logic from the comments given on the code itself.
  • Please go through the comments and try to understand them.

Open in OnlineGDB

// C Program to Simulate C-LOOK 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[]
    //Moving Disk head to the inner most requested cylinder,
    //because this is Circular LOOK.
    //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-LOOK 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; 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.