# 算法设计：C-LOOK磁盘调度算法指南

2021年3月13日16:12:44 发表评论 610 次浏览

C-LOOK(循环查找)磁盘调度算法：

C语言

1. 令Request数组代表一个数组, 该数组存储按其到达时间和头是磁盘头的位置。
2. 给出了磁头移动的初始方向, 并且它以相同的方向运行。
3. 头部在其移动方向上一一处理所有请求。
4. 磁头继续沿相同方向移动, 直到已满足该方向上的所有请求。
5. 沿该方向移动时, 计算磁道与磁头的绝对距离。
6. 以此距离增加总寻道数。
7. 当前服务的轨道位置现在变为新的头位置。
8. 转到第5步, 直到我们达到此方向的最后一个请求。
9. 如果我们在当前方向上到达最后一个请求, 则反转方向, 并朝该方向移动磁头, 直到我们到达需要在该方向上进行服务的最后一个请求, 而无需服务中间请求。
10. 反转方向, 然后转到步骤3, 直到未满足所有请求。

``````// C++ implementation of the approach
#include <bits/stdc++.h>
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++) {
left.push_back(arr[i]);
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
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
}

// Once reached the right end
// is needed to be serviced in
// left direction
seek_count += abs (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
}

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 };

return 0;
}``````

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