/**/ FCFS Algorithm Program in C - Dextutor

FCFS Algorithm Program in C

Disk scheduling algorithms are used to come with an order in which the I/O read/write requests by the different process should be satisfied. The aim is to minimize the seek time i.e., the time to move from one sector to another. First Come First Serve (FCFS) disk scheduling algorithm serves the incoming requests in order i.e., the request which came first will be satisfied first. For example, if the order of I/O requests is 10, 40, 20, 50, 35 and the current head position is 45, then the order of fulfilling the requests will be
45 -> 10 -> 40 -> 20 -> 50 ->35
because the first request was for sector 10, then 40 and so on. For more details refer Page Replacement Algorithms. FCFS algorithm program in C is implemented below

Variable description for the program for FCFS disk scheduling algorithm

req[] – array for taking the request, mov – to calculate total head movement

FCFS Disk Scheduling Algorithm Program in C

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i,n,req[50],mov=0,cp;
    printf("enter the current position\n");
    scanf("%d",&cp);
    printf("enter the number of requests\n");
    scanf("%d",&n);
    printf("enter the request order\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&req[i]);
    }
    mov=mov+abs(cp-req[0]); // abs is used to calculate the absolute value
    printf("%d -> %d",cp,req[0]);
    for(i=1;i<n;i++)
    {
        mov=mov+abs(req[i]-req[i-1]);
        printf(" -> %d",req[i]);
    }
    printf("\n");
    printf("total head movement = %d\n",mov);
}

Output

FCFS Algorithm Program in C

How it works?

In FCFS disk scheduling algorithm program the order of requests is stored in the req[] array. The movement between the requests is calculated by taking the absolute value of the difference between the values at consecutive index positions of the array. Finally, all the movements are added to get the total head movement.

Relevant Programs