# FCFS Disk Scheduling Program in C/C++

Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk and the algorithm used for the disk scheduling is called Disk Scheduling Algorithm.

In this post, we will discuss the First Come First Serve, also known as FCFS Disk Scheduling Algorithm, and also write a program for FCFS disk scheduling algorithm. As the name suggests, the I/O requests are addressed in the order of their arrival.

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of FCFS in C++, although, it’s very similar to C.

INPUT:
The first line is the size of the disk (m).
The second line is the number of I/O requests (n).
The third line is an array of I/O requests (a[n]).
The fourth line is the head position (h).

OUTPUT:

The following is the FCFS disk scheduling program in C++.

```#include<bits/stdc++.h>
using namespace std;
int main(){

int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";

// creating an array of size n
vector <int> a(n);
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cin>>h;

// head will be at h at the starting
int temp=h;
cout<<temp;
for(i=0;i<n;i++){
cout<<" -> "<<a[i]<<' ';
// calculating the difference for the head movement
sum+=abs(a[i]-temp);
// head is now at the next I/O request
temp=a[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
return 0;
}```

OUTPUT:

```Enter the size of disk
199
Enter number of requests
8
Enter the requests
98 183 37 122 14 124 65 67