# C-LOOK Disk Scheduling Algorithm

Prerequisite: Disk Scheduling Algorithms

Given an array of disk track numbers and initial head position, our task is to find the total number of seek operations done to access all the requested tracks if C-LOOK disk scheduling algorithm is used. Also, write a program to find the seek sequence using C-LOOK disk scheduling algorithm.

C-LOOK (Circular LOOK) Disk Scheduling Algorithm:
C-LOOK is an enhanced version of both SCAN as well as LOOK disk scheduling algorithms. This algorithm also uses the idea of wrapping the tracks as a circular cylinder as C-SCAN algorithm but the seek time is better than C-SCAN algorithm. We know that C-SCAN is used to avoid starvation and services all the requests more uniformly, the same goes for C-LOOK.

In this algorithm, the head services requests only in one direction(either left or right) until all the requests in this direction are not serviced and then jumps back to the farthest request on the other direction and service the remaining requests which gives a better uniform servicing as well as avoids wasting seek time for going till the end of the disk.

Algorithm-

1. Let Request array represents an array storing indexes of the tracks that have been requested in ascending order of their time of arrival and head is the position of the disk head.
2. The initial direction in which the head is moving is given and it services in the same direction.
3. The head services all the requests one by one in the direction it is moving.
4. The head continues to move in the same direction until all the requests in this direction have been serviced.
5. While moving in this direction, calculate the absolute distance of the tracks from the head.
6. Increment the total seek count with this distance.
7. Currently serviced track position now becomes the new head position.
8. Go to step 5 until we reach the last request in this direction.
9. If we reach the last request in the current direction then reverse the direction and move the head in this direction until we reach the last request that is needed to be serviced in this direction without servicing the intermediate requests.
10. Reverse the direction and go to step 3 until all the requests have not been serviced.

Examples:

Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Direction = right (Moving from left to right)
Output:
Total number of seek operations = 156
Seek Sequence is
60
79
92
114
176
11
34
41

The following chart shows the sequence in which requested tracks are serviced using C-LOOK.

Therefore, the total seek count = (60 – 50) + (79 – 60) + (92 – 79) + (114 – 92) + (176 – 114) + (176 – 11) + (34 – 11) + (41 – 34) = 321

Implementation:
The implementation of the C-LOOK algorithm is given below. Note that the distance variable is used to store the absolute distance between the head and the current track position, disk_size is the size of the disk. Vectors left and right store all the request tracks on the left-hand side and the right-hand side of the initial head position respectively.

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `int` `size = 8; ` `int` `disk_size = 200; ` ` `  `// Function to perform C-LOOK on the request ` `// array starting from the given head ` `void` `CLOOK(``int` `arr[], ``int` `head) ` `{ ` `    ``int` `seek_count = 0; ` `    ``int` `distance, cur_track; ` `    ``vector<``int``> left, right; ` `    ``vector<``int``> seek_sequence; ` ` `  `    ``// Tracks on the left of the ` `    ``// head will be serviced when ` `    ``// once the head comes back ` `    ``// to the beggining (left end) ` `    ``for` `(``int` `i = 0; i < size; i++) { ` `        ``if` `(arr[i] < head) ` `            ``left.push_back(arr[i]); ` `        ``if` `(arr[i] > head) ` `            ``right.push_back(arr[i]); ` `    ``} ` ` `  `    ``// Sorting left and right vectors ` `    ``std::sort(left.begin(), left.end()); ` `    ``std::sort(right.begin(), right.end()); ` ` `  `    ``// First service the requests ` `    ``// on the right side of the ` `    ``// head ` `    ``for` `(``int` `i = 0; i < right.size(); i++) { ` `        ``cur_track = right[i]; ` ` `  `        ``// Appending current track to seek sequence ` `        ``seek_sequence.push_back(cur_track); ` ` `  `        ``// Calculate absolute distance ` `        ``distance = ``abs``(cur_track - head); ` ` `  `        ``// Increase the total count ` `        ``seek_count += distance; ` ` `  `        ``// Accessed track is now new head ` `        ``head = cur_track; ` `    ``} ` ` `  `    ``// Once reached the right end ` `    ``// jump to the last track that ` `    ``// is needed to be serviced in ` `    ``// left direction ` `    ``seek_count += ``abs``(head - left[0]); ` `    ``head = left[0]; ` ` `  `    ``// Now service the requests again ` `    ``// which are left ` `    ``for` `(``int` `i = 0; i < left.size(); i++) { ` `        ``cur_track = left[i]; ` ` `  `        ``// Appending current track to seek sequence ` `        ``seek_sequence.push_back(cur_track); ` ` `  `        ``// Calculate absolute distance ` `        ``distance = ``abs``(cur_track - head); ` ` `  `        ``// Increase the total count ` `        ``seek_count += distance; ` ` `  `        ``// Accessed track is now the new head ` `        ``head = cur_track; ` `    ``} ` ` `  `    ``cout << ``"Total number of seek operations = "` `         ``<< seek_count << endl; ` ` `  `    ``cout << ``"Seek Sequence is"` `<< endl; ` ` `  `    ``for` `(``int` `i = 0; i < seek_sequence.size(); i++) { ` `        ``cout << seek_sequence[i] << endl; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Request array ` `    ``int` `arr[size] = { 176, 79, 34, 60, ` `                      ``92, 11, 41, 114 }; ` `    ``int` `head = 50; ` ` `  `    ``cout << ``"Initial position of head: "` `<< head << endl; ` ` `  `    ``CLOOK(arr, head); ` ` `  `    ``return` `0; ` `} `

Output:

```Initial position of head: 50
Total number of seek operations = 321
Seek Sequence is
60
79
92
114
176
11
34
41
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.