先决条件:磁盘调度算法
给定一组磁盘磁道号和初始磁头位置,如果使用C-LOOK磁盘调度算法,我们的任务是找到为访问所有请求的磁道而执行的寻道操作总数。另外,编写一个程序来使用C-LOOK磁盘调度算法查找查找序列。
C-LOOK(Circular LOOK)磁盘调度算法:
C-LOOK是SCAN和LOOK磁盘调度算法的增强版本。该算法也使用了像C-SCAN算法一样将磁道包裹成圆柱体的思想,但寻道时间比C-SCAN算法要好。我们知道 C-SCAN 是用来避免饥饿和更统一地服务所有请求的,C-LOOK 也是如此。
在这个算法中,头部只在一个方向(左或右)服务请求,直到这个方向的所有请求都没有得到服务,然后跳回到另一个方向上最远的请求并服务剩余的请求,这提供了更好的统一服务以及避免浪费寻找时间直到磁盘结束。
算法-
- 令 Request array 表示一个数组,存储已被请求的磁道的索引,按到达时间的升序排列, head是磁盘磁头的位置。
- 头部运动的初始方向是给定的,它以相同的方向服务。
- 头部在其移动的方向上一一处理所有请求。
- 头部继续朝同一个方向移动,直到这个方向的所有请求都得到了服务。
- 在此方向上移动时,计算磁道与磁头的绝对距离。
- 使用此距离增加总寻道计数。
- 当前服务的轨道位置现在成为新的磁头位置。
- 转到第 5 步,直到我们到达此方向的最后一个请求。
- 如果我们到达当前方向的最后一个请求,则反转方向并沿此方向移动头部,直到到达需要在此方向上提供服务的最后一个请求,而不为中间请求提供服务。
- 反转方向并转到步骤 3,直到所有请求都没有得到服务。
例子:
Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
Direction = right (Moving from left to right)
Output:
Initial position of head: 50
Total number of seek operations = 156
Seek Sequence is
60
79
92
114
176
11
34
41
下图显示了使用 C-LOOK 服务请求的轨道的顺序。
因此,总寻道次数 = (60 – 50) + (79 – 60) + (92 – 79) + (114 – 92) + (176 – 114) + (176 – 11) + (34 – 11) + ( 41 – 34) = 321
执行:
C-LOOK算法的实现如下。注意distance 变量用于存储磁头与当前磁道位置的绝对距离,disk_size 是磁盘的大小。向量 left 和 right 分别存储初始头部位置左侧和右侧的所有请求轨迹。
C++
// 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 left, right;
vector seek_sequence;
// Tracks on the left of the
// head will be serviced when
// once the head comes back
// to the beginning (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;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG{
static int size = 8;
static int disk_size = 200;
// Function to perform C-LOOK on the request
// array starting from the given head
public static void CLOOK(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
Vector left = new Vector();
Vector right = new Vector();
Vector seek_sequence = new Vector();
// Tracks on the left of the
// head will be serviced when
// once the head comes back
// to the beginning (left end)
for(int i = 0; i < size; i++)
{
if (arr[i] < head)
left.add(arr[i]);
if (arr[i] > head)
right.add(arr[i]);
}
// Sorting left and right vectors
Collections.sort(left);
Collections.sort(right);
// First service the requests
// on the right side of the
// head
for(int i = 0; i < right.size(); i++)
{
cur_track = right.get(i);
// Appending current track
// to seek sequence
seek_sequence.add(cur_track);
// Calculate absolute distance
distance = Math.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 += Math.abs(head - left.get(0));
head = left.get(0);
// Now service the requests again
// which are left
for(int i = 0; i < left.size(); i++)
{
cur_track = left.get(i);
// Appending current track to
// seek sequence
seek_sequence.add(cur_track);
// Calculate absolute distance
distance = Math.abs(cur_track - head);
// Increase the total count
seek_count += distance;
// Accessed track is now the new head
head = cur_track;
}
System.out.println("Total number of seek " +
"operations = " + seek_count);
System.out.println("Seek Sequence is");
for(int i = 0; i < seek_sequence.size(); i++)
{
System.out.println(seek_sequence.get(i));
}
}
// Driver code
public static void main(String []args)
{
// Request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
System.out.println("Initial position of head: " +
head);
CLOOK(arr, head);
}
}
// This code is contributed by divyesh072019
Python3
# Python3 implementation of the approach
size = 8
disk_size = 200
# Function to perform C-LOOK on the request
# array starting from the given head
def CLOOK(arr, head):
seek_count = 0
distance = 0
cur_track = 0
left = []
right = []
seek_sequence = []
# Tracks on the left of the
# head will be serviced when
# once the head comes back
# to the beginning (left end)
for i in range(size):
if (arr[i] < head):
left.append(arr[i])
if (arr[i] > head):
right.append(arr[i])
# Sorting left and right vectors
left.sort()
right.sort()
# First service the requests
# on the right side of the
# head
for i in range(len(right)):
cur_track = right[i]
# Appending current track
# seek sequence
seek_sequence.append(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 i in range(len(left)):
cur_track = left[i]
# Appending current track to
# seek sequence
seek_sequence.append(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
print("Total number of seek operations =",
seek_count)
print("Seek Sequence is")
for i in range(len(seek_sequence)):
print(seek_sequence[i])
# Driver code
# Request array
arr = [ 176, 79, 34, 60,
92, 11, 41, 114 ]
head = 50
print("Initial position of head:", head)
CLOOK(arr, head)
# This code is contributed by rag2127
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG{
static int size = 8;
// Function to perform C-LOOK on the request
// array starting from the given head
static void CLOOK(int[] arr, int head)
{
int seek_count = 0;
int distance, cur_track;
List left = new List();
List right = new List();
List seek_sequence = new List();
// Tracks on the left of the
// head will be serviced when
// once the head comes back
// to the beginning (left end)
for(int i = 0; i < size; i++)
{
if (arr[i] < head)
left.Add(arr[i]);
if (arr[i] > head)
right.Add(arr[i]);
}
// Sorting left and right vectors
left.Sort();
right.Sort();
// First service the requests
// on the right side of the
// head
for(int i = 0; i < right.Count; i++)
{
cur_track = right[i];
// Appending current track
// to seek sequence
seek_sequence.Add(cur_track);
// Calculate absolute distance
distance = Math.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 += Math.Abs(head - left[0]);
head = left[0];
// Now service the requests again
// which are left
for(int i = 0; i < left.Count; i++)
{
cur_track = left[i];
// Appending current track to
// seek sequence
seek_sequence.Add(cur_track);
// Calculate absolute distance
distance = Math.Abs(cur_track - head);
// Increase the total count
seek_count += distance;
// Accessed track is now the new head
head = cur_track;
}
Console.WriteLine("Total number of seek " +
"operations = " + seek_count);
Console.WriteLine("Seek Sequence is");
for(int i = 0; i < seek_sequence.Count; i++)
{
Console.WriteLine(seek_sequence[i]);
}
}
// Driver code
static void Main()
{
// Request array
int[] arr = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
Console.WriteLine("Initial position of head: " +
head);
CLOOK(arr, head);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
Initial position of head: 50
Total number of seek operations = 321
Seek Sequence is
60
79
92
114
176
11
34
41