Skip to content
doubtly

2 answers

Best answer

FCFS Disk Scheduling Algorithm: 

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int queue[20], n, head, i, seek = 0;
    float avg;
 
    printf("Enter the size of Queue : ");
    scanf("%d", &n);
 
    printf("Enter the Queue : ");
    for(i=1;i<=n;i++)
        scanf("%d",&queue[i]);
 
    printf("Enter the initial head position : ");
    scanf("%d", &head);
 
    queue[0] = head;
 
    for(i=0;i<n;i++)
    {
        seek += abs(queue[i+1] - queue[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue[i],queue[i+1],abs(queue[i+1] - queue[i]));
    }
 
    printf("Total seek time = %d\n", seek);
    avg = seek/(float)n;
    printf("Average seek time = %f\n", avg);
 
    return 0;
}

SCAN Disk Scheduling Algorithm:

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20], queue2[20], temp1 = 0, temp2 = 0;
    float avg;
 
    printf("Enter the size of Queue : ");
    scanf("%d", &n);
 
    printf("Enter the Queue : ");
    for(i=1;i<=n;i++)
        scanf("%d",&queue[i]);
 
    printf("Enter the initial head position : ");
    scanf("%d", &head);
 
    queue[0] = head;
 
    for(i=0;i<n;i++)
    {
        max = 0;
        for(j=i+1;j<=n;j++)
        {
            if(queue[i]>=queue[j])
                diff = queue[i]-queue[j];
            else
                diff = queue[j]-queue[i];
            if(diff>max)
            {
                max = diff;
                temp = j;
            }
        }
        queue1[temp1] = queue[temp];
        temp1++;
        for(k=temp;k<=n-1;k++)
        {
            queue[k] = queue[k+1];
        }
        n--;
    }
 
    queue2[temp2] = head;
    for(i=0;i<temp1-1;i++)
    {
        queue2[i+1] = queue1[i];
    }
 
    for(i=0;i<temp1;i++)
    {
        seek += abs(queue2[i+1] - queue2[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue2[i],queue2[i+1],abs(queue2[i+1] - queue2[i]));
    }
 
    printf("Total seek time = %d\n", seek);
    avg = seek/(float)temp1;
    printf("Average seek time = %f\n", avg);
 
    return 0;
}

C-SCAN Disk Scheduling Algorithm:  

 







#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20], queue2[20], temp1 = 0, temp2 = 0;
    float avg;
 
    printf("Enter the size of Queue : ");
    scanf("%d", &n);
 
    printf("Enter the Queue : ");
    for(i=1;i<=n;i++)
        scanf("%d",&queue[i]);
 
    printf("Enter the initial head position : ");
    scanf("%d", &head);
 
    queue[0] = head;
 
    for(i=0;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(queue[i]>queue[j])
            {
                temp = queue[i];
                queue[i] = queue[j];
                queue[j] = temp;
            }
        }
    }
 
    max = queue[n];
 
    for(i=head;i<=max;i++)
    {
        queue1[temp1] = queue[i];
        temp1++;
    }
 
    queue1[temp1] = max;
    temp1++;
 
    for(i=max-1;i>=0;i--)
    {
        queue2[temp2] = queue[i];
        temp2++;
    }
 
    queue2[temp2] = 0;
    temp2++;
 
    seek = head + max;
 
    for(i=0;i<temp1-1;i++)
    {
        seek += abs(queue1[i+1] - queue1[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue1[i],queue1[i+1],abs(queue1[i+1] - queue1[i]));
    }
 
    for(i=0;i<temp2-1;i++)
    {
        seek += abs(queue2[i+1] - queue2[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue2[i],queue2[i+1],abs(queue2[i+1] - queue2[i]));
    }
 
    printf("Total seek time = %d\n", seek);
    avg = seek/(float)(temp1+temp2-2);
    printf("Average seek time = %f\n", avg);
 
    return 0;
}
Outputs :: 

 
FCFS Disk Scheduling Algorithm: 

Enter the size of Queue : 4
Enter the Queue : 98 183 37 122
Enter the initial head position : 53
Disk head moves from 53 to 98 with seek 45
Disk head moves from 98 to 183 with seek 85
Disk head moves from 183 to 37 with seek 146
Disk head moves from 37 to 122 with seek 85
Total seek time = 361
Average seek time = 90.250000

SCAN Disk Scheduling Algorithm: 

Enter the size of Queue : 8
Enter the Queue : 98 183 37 122 14 124 65 67
Enter the initial head position : 53
Disk head moves from 53 to 65 with seek 12
Disk head moves from 65 to 67 with seek 2
Disk head moves from 67 to 98 with seek 31
Disk head moves from 98 to 122 with seek 24
Disk head moves from 122 to 124 with seek 2
Disk head moves from 124 to 183 with seek 59
Disk head moves from 183 to 14 with seek 169
Disk head moves from 14 to 37 with seek 23
Total seek time = 322
Average seek time = 40.250000
C-SCAN Disk Scheduling Algorithm: 
Enter the size of Queue : 8
Enter the Queue : 98 183 37 122 14 124 65 67
Enter the initial head position : 53
Disk head moves from 53 to 65 with seek 12
Disk head moves from 65 to 98 with seek 33
Disk head moves from 98 to 122 with seek 24
Disk head moves from 122 to 124 with seek 2
Disk head moves from 124 to 183 with seek 59
Disk head moves from 183 to 0 with seek 183
Disk head moves from 0 to 14 with seek 14
Disk head moves from 14 to 37 with seek 23
Disk head moves from 37 to 65 with seek 28
Total seek time = 398
Average seek time = 49.750000
Doubtly · 23 Apr 2023 Report

Answer: A C program that prints the order of service and the total head movement for FCFS, SCAN and C-SCAN.

#include <stdio.h>
#include <stdlib.h>
#define MAX 50

int fcfs(int req[], int n, int head) {
    int seek = 0, cur = head;
    printf("FCFS order: %d", head);
    for (int i = 0; i < n; i++) {
        seek += abs(req[i] - cur);
        cur = req[i];
        printf(" -> %d", cur);
    }
    return seek;
}

/* requests sorted in ascending order in s[] */
int scan(int s[], int n, int head, int disk) {
    int seek = 0, cur = head;
    printf("SCAN order: %d", head);
    for (int i = 0; i < n; i++)            /* move towards higher tracks */
        if (s[i] >= head) {
            seek += s[i] - cur; cur = s[i]; printf(" -> %d", cur);
        }
    seek += (disk - 1) - cur; cur = disk - 1;  /* go to the last track */
    printf(" -> %d", cur);
    for (int i = n - 1; i >= 0; i--)       /* reverse and come back */
        if (s[i] < head) {
            seek += cur - s[i]; cur = s[i]; printf(" -> %d", cur);
        }
    return seek;
}

int cscan(int s[], int n, int head, int disk) {
    int seek = 0, cur = head;
    printf("C-SCAN order: %d", head);
    for (int i = 0; i < n; i++)
        if (s[i] >= head) {
            seek += s[i] - cur; cur = s[i]; printf(" -> %d", cur);
        }
    seek += (disk - 1) - cur;              /* to the last track */
    seek += disk - 1;                      /* jump back to track 0 */
    cur = 0;
    printf(" -> %d -> 0", disk - 1);
    for (int i = 0; i < n; i++)            /* serve the rest upward */
        if (s[i] < head) {
            seek += s[i] - cur; cur = s[i]; printf(" -> %d", cur);
        }
    return seek;
}

int main(void) {
    int n, head, disk, req[MAX], s[MAX];
    printf("Number of requests: ");
    scanf("%d", &n);
    printf("Request queue: ");
    for (int i = 0; i < n; i++) { scanf("%d", &req[i]); s[i] = req[i]; }
    printf("Initial head position: ");
    scanf("%d", &head);
    printf("Total tracks on disk (e.g. 200 for 0-199): ");
    scanf("%d", &disk);

    for (int i = 0; i < n - 1; i++)        /* sort a copy for SCAN/C-SCAN */
        for (int j = 0; j < n - 1 - i; j++)
            if (s[j] > s[j + 1]) { int t = s[j]; s[j] = s[j + 1]; s[j + 1] = t; }

    int t;
    printf("\n");
    t = fcfs(req, n, head);
    printf("\nTotal head movement = %d\n\n", t);
    t = scan(s, n, head, disk);
    printf("\nTotal head movement = %d\n\n", t);
    t = cscan(s, n, head, disk);
    printf("\nTotal head movement = %d\n", t);
    return 0;
}

Logic

  • FCFS: serve requests in the order they arrive.
  • SCAN (elevator): move towards the higher tracks serving requests, go to the last track, then reverse and serve the remaining requests on the way back.
  • C-SCAN: move towards the higher tracks serving requests, go to the last track, jump back to track 0, then serve the remaining requests while moving up again.

Sample run: queue 98 183 37 122 14 124 65 67, head at 53, 200 tracks (0–199)

FCFS   : total head movement = 640
SCAN   : 53 65 67 98 122 124 183 199 37 14       -> 331
C-SCAN : 53 65 67 98 122 124 183 199 0 14 37     -> 382

In C-SCAN this program counts the return jump 199 → 0 (199 tracks). Some textbooks leave that jump out, which gives 183. Check which convention your syllabus uses.

The earlier answer on this page has a correct FCFS part. Its SCAN code does not sweep in one direction, and its C-SCAN code uses track numbers as array indexes (queue[i] for i from head to max), so those two parts do not work.

Tutor AI · 23 Oct 2026 AI-verified AI-generated Report

Your answer

Log in or create a free account to answer this question.