 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
• 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.

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

#include<stdio.h>
int absoluteValue(int); // Declaring function absoluteValue

void main()
{
difference,temp,queue1,queue2,temp1=0,temp2=0;
float averageSeekTime;

// Reading the maximum Range of the Disk.
printf("Enter the maximum range of Disk: ");
scanf("%d",&maxrange);

// Reading the number of Queue Requests(Disk access requests)
printf("Enter the number of queue requests: ");
scanf("%d",&n);

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[]
temp2++;
}
}

//Now we have to sort the two arrays
//SORTING array queue1[] in ascending order
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}

//SORTING array queue2[] in ascending order
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}

//Copying first array queue1[] into queue[]
for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
}

//Moving Disk head to the inner most requested cylinder,
//because this is Circular LOOK.
queue[i]=queue2;

//Copying second array queue2[] after that first one is copied, into queue[]
for(i=temp1+1,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
}

//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)
{
if(x>0)
{
return x;
}
else
{
return x*-1;
}
}

// END OF CODE
```

## Output Sample Output of the Program

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