- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Operating System Processes
- Process Control Block
- Operations on Processes
- Inter Process Communication
- Context Switching
- Multi-threading
- Scheduling Algorithms
- Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Peterson's Solution
- Monitors
- Sleep and Wake
- Race Condition
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Memory Management
- Memory Management
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Fragmentation
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - FCFS Disk Scheduling
The disk scheduling algorithms are used to determine the order in which input and output (I/O) requests of the disk are to be processed. In this chapter, we will discuss the First-Come, First-Served (FCFS) disk scheduling algorithm, with examples, and practice questions.
- First-Come, First-Served (FCFS) Disk Scheduling
- Example of FCFS Disk Scheduling Algorithm
- Implementation of FCFS Disk Scheduling
- Practice Questions in FCFS Disk Scheduling
- Pros and Cons of FCFS Disk Scheduling
First-Come, First-Served (FCFS) Disk Scheduling
The FIFO (First-In, First-Out) or FCFS (First-Come, First-Served) disk scheduling algorithm is the simplest form of disk scheduling. Here, the requests are processed in the order they arrive. Meaning the request that came first will be served first. This will not depend on the current position of the disk head.
This algorithm will treat all requests equally without any prioritization. So there is no starvation happen in this algorithm. However, there will be lots of head movement which will increase the average seek time.
Algorithm Steps
- Create a queue to hold the disk requests.
- When a new i/o request arrives, add the request to the end of the queue.
- Wait until the disk head is free. If the disk head is free, move the disk head to the track of the first request in the queue.
- Process the request by reading or writing data.
- Now remove the request from the queue.
- Repeat these steps until all requests are processed and the queue is empty.
Example of FCFS Disk Scheduling Algorithm
Consider the following disk requests arriving in the order: 98, 183, 37, 122, 14, 124, 65, 67. The image below shows how the disk head moves for the above request sequence using FCFS disk scheduling algorithm −
The initial position of the disk head is at track 53. We can calculate the total head movement as follows:
$$\mathrm{\text{Initial Position of Head} = 53}$$
$$\mathrm{\text{Request Sequence} = 98,\,183,\,37,\,122,\,14,\,124,\,65,\,67}$$
$$\mathrm{\text{Move from 53 to 98:}\quad |53-98| = 45}$$
$$\mathrm{\text{Move from 98 to 183:}\quad |98-183| = 85}$$
$$\mathrm{\text{Move from 183 to 37:}\quad |183-37| = 146}$$
$$\mathrm{\text{Move from 37 to 122:}\quad |37-122| = 85}$$
$$\mathrm{\text{Move from 122 to 14:}\quad |122-14| = 108}$$
$$\mathrm{\text{Move from 14 to 124:}\quad |14-124| = 110}$$
$$\mathrm{\text{Move from 124 to 65:}\quad |124-65| = 59}$$
$$\mathrm{\text{Move from 65 to 67:}\quad |65-67| = 2}$$
$$\mathrm{\text{Total Head Movement} = 45 + 85 + 146 + 85 + 108 + 110 + 59 + 2 = 640\ \text{tracks}}$$
Implementation of FCFS Disk Scheduling Algorithm
Here is how we can implement the FCFS disk scheduling algorithm in Python/CPP/Java −
def calculate_total_head_movement(requests, initial_head):
total_movement = 0
current_head = initial_head
for request in requests:
total_movement += abs(current_head - request)
current_head = request
return total_movement
# Example usage
initial_head = 53
requests = [98, 183, 37, 122, 14, 124, 65, 67]
total_movement = calculate_total_head_movement(requests, initial_head)
print("Total Head Movement:", total_movement)
The output of the above code is as follows −
Total Head Movement: 640
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int calculateTotalHeadMovement(const vector<int>& requests, int initialHead) {
int totalMovement = 0;
int currentHead = initialHead;
for (int request : requests) {
totalMovement += abs(currentHead - request);
currentHead = request;
}
return totalMovement;
}
int main() {
int initialHead = 53;
vector<int> requests = {98, 183, 37, 122, 14, 124, 65, 67};
int totalMovement = calculateTotalHeadMovement(requests, initialHead);
cout << "Total Head Movement: " << totalMovement << endl;
return 0;
}
The output of the above code is as follows −
Total Head Movement: 640
import java.util.List;
import java.util.Arrays;
public class FCFS {
public static int calculateTotalHeadMovement(List<Integer> requests, int initialHead) {
int totalMovement = 0;
int currentHead = initialHead;
for (int request : requests) {
totalMovement += Math.abs(currentHead - request);
currentHead = request;
}
return totalMovement;
}
public static void main(String[] args) {
int initialHead = 53;
List<Integer> requests = Arrays.asList(98, 183, 37, 122, 14, 124, 65, 67);
int totalMovement = calculateTotalHeadMovement(requests, initialHead);
System.out.println("Total Head Movement: " + totalMovement);
}
}
The output of the above code is as follows −
Total Head Movement: 640
Practice Questions in FCFS Disk Scheduling
Question 1: Given the following disk requests: 45, 23, 89, 12, 67, and the initial position of the disk head is at track 30. Calculate the total head movement using FCFS disk scheduling algorithm.
Answer: We have,
$$\mathrm{\text{Initial Position of Head} = 30}$$
$$\mathrm{\text{Request Sequence} = 45,\,23,\,89,\,12,\,67}$$
Now, we can calculate the total head movement as follows −
$$\mathrm{\text{Move from 30 to 45:}\quad |30-45| = 15}$$
$$\mathrm{\text{Move from 45 to 23:}\quad |45-23| = 22}$$
$$\mathrm{\text{Move from 23 to 89:}\quad |23-89| = 66}$$
$$\mathrm{\text{Move from 89 to 12:}\quad |89-12| = 77}$$
$$\mathrm{\text{Move from 12 to 67:}\quad |12-67| = 55}$$
So the total head movement will be −
$$\mathrm{\text{Total Head Movement} = 15 + 22 + 66 + 77 + 55 = 235\ \text{tracks}}$$
Question 2: Consider a disk queue with requests for cylinders 55, 58, 39, 18, 90, 160. The FCFS scheduling algorithm is used. The head is initially at cylinder number 100, moving towards last cylinder. The cylinders are numbered from 0 to 199. The average seek length is?
Answer: We have,
$$\mathrm{\text{Initial Position of Head} = 100}$$
$$\mathrm{\text{Request Sequence} = 55,\,58,\,39,\,18,\,90,\,160}$$
Now, the seek length for each request can be calculated as follows −
$$\mathrm{\text{Move from 100 to 55:}\quad |100-55| = 45}$$
$$\mathrm{\text{Move from 55 to 58:}\quad |55-58| = 3}$$
$$\mathrm{\text{Move from 58 to 39:}\quad |58-39| = 19}$$
$$\mathrm{\text{Move from 39 to 18:}\quad |39-18| = 21}$$
$$\mathrm{\text{Move from 18 to 90:}\quad |18-90| = 72}$$
$$\mathrm{\text{Move from 90 to 160:}\quad |90-160| = 70}$$
Now take the average of these values to get the average seek length −
$$\mathrm{\text{Total Seek Length} = 45 + 3 + 19 + 21 + 72 + 70 = 230}$$
$$\mathrm{\text{Average Seek Length} = \frac{\text{Total Seek Length}}{\text{Number of Requests}} }$$
$$\mathrm{\quad = \frac{230}{6} \approx 38.33\ \text{cylinders}}$$
Pros and Cons of FCFS Disk Scheduling Algorithm
The advantages of this algorithm are −
- Simple and easy to implement.
- Every request gets a fair chance to be served.
- No starvation occurs.
The drawbacks of this algorithm are −
- The average seek time is very high.
- It does not consider the current position of the disk head, So not always best choice of movement.
- Can lead to long wait times for some requests.