# 算法设计：如何实现FCFS磁盘调度算法？

2021年3月23日14:58:33 发表评论 453 次浏览

## 本文概述

FCFS是最简单的

。顾名思义, 该算法按请求到达磁盘队列的顺序来接受请求。该算法看起来非常公平, 没有饥饿(所有请求都按顺序处理), 但是通常它不能提供最快的服务。

1. 让Request数组表示一个数组, 该数组存储已按其到达时间的升序请求的音轨的索引。 "磁头"是磁盘磁头的位置。
2. 让我们一一按照默认顺序获取轨迹, 并计算轨迹到头部的绝对距离。
3. 以此距离增加总寻道数。
4. 当前服务的轨道位置现在变为新的头位置。
5. 转到步骤2, 直到未维修请求数组中的所有轨道。

``````Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}

Output:
Total number of seek operations = 510
Seek Sequence is
176
79
34
60
92
11
41
114``````

``````= (176-50)+(176-79)+(79-34)+(60-34)+(92-60)+(92-11)+(41-11)+(114-41)
= 510``````

FCFS

## C ++

``````// C++ program to demonstrate
// FCFS Disk Scheduling algorithm

#include <bits/stdc++.h>
using namespace std;

int size = 8;

void FCFS( int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;

for ( int i = 0; i < size; i++) {
cur_track = arr[i];

// calculate absolute distance
distance = abs (cur_track - head);

// increase the total count
seek_count += distance;

// accessed track is now new head
}

cout << "Total number of seek operations = "
<< seek_count << endl;

// Seek sequence would be the same
// as request array sequence
cout << "Seek Sequence is" << endl;

for ( int i = 0; i < size; i++) {
cout << arr[i] << endl;
}
}

// Driver code
int main()
{

// request array
int arr[size] = { 176, 79, 34, 60, 92, 11, 41, 114 };

return 0;
}``````

## Java

``````// Java program to demonstrate
// FCFS Disk Scheduling algorithm
class GFG
{
static int size = 8 ;

static void FCFS( int arr[], int head)
{
int seek_count = 0 ;
int distance, cur_track;

for ( int i = 0 ; i < size; i++)
{
cur_track = arr[i];

// calculate absolute distance

// increase the total count
seek_count += distance;

// accessed track is now new head
}

System.out.println( "Total number of " +
"seek operations = " +
seek_count);

// Seek sequence would be the same
// as request array sequence
System.out.println( "Seek Sequence is" );

for ( int i = 0 ; i < size; i++)
{
System.out.println(arr[i]);
}
}

// Driver code
public static void main(String[] args)
{
// request array
int arr[] = { 176 , 79 , 34 , 60 , 92 , 11 , 41 , 114 };

}
}

// This code is contributed by 29AjayKumar``````

## Python3

``````# Python program to demonstrate
# FCFS Disk Scheduling algorithm

size = 8 ;

seek_count = 0 ;
distance, cur_track = 0 , 0 ;

for i in range (size):
cur_track = arr[i];

# calculate absolute distance
distance = abs (cur_track - head);

# increase the total count
seek_count + = distance;

# accessed track is now new head

print ( "Total number of seek operations = " , seek_count);

# Seek sequence would be the same
# as request array sequence
print ( "Seek Sequence is" );

for i in range (size):
print (arr[i]);

# Driver code
if __name__ = = '__main__' :

# request array
arr = [ 176 , 79 , 34 , 60 , 92 , 11 , 41 , 114 ];

# This code contributed by Rajput-Ji``````

## C#

``````// C# program to demonstrate
// FCFS Disk Scheduling algorithm
using System;

class GFG
{
static int size = 8;

static void FCFS( int []arr, int head)
{
int seek_count = 0;
int distance, cur_track;

for ( int i = 0; i < size; i++)
{
cur_track = arr[i];

// calculate absolute distance

// increase the total count
seek_count += distance;

// accessed track is now new head
}

Console.WriteLine( "Total number of " +
"seek operations = " +
seek_count);

// Seek sequence would be the same
// as request array sequence
Console.WriteLine( "Seek Sequence is" );

for ( int i = 0; i < size; i++)
{
Console.WriteLine(arr[i]);
}
}

// Driver code
public static void Main(String[] args)
{
// request array
int []arr = { 176, 79, 34, 60, 92, 11, 41, 114 };

}
}

// This code is contributed by PrinciRaj1992``````

``````Total number of seek operations = 510
Seek Sequence is
176
79
34
60
92
11
41
114``````