OS Experiment 10
OS Experiment 10
2. Objective
• Purpose: To understand and implement the three main disk scheduling algorithms: FCFS,
SCAN, and C-SCAN. These algorithms determine the order in which disk requests are
processed, aiming to minimize the total head movement and optimize system performance.
• Example: The objective is to implement a simulation where the disk access requests are
handled based on these algorithms and calculate the total movement of the disk head.
3. Problem Statement
• Purpose: The problem is to simulate the working of the three disk scheduling algorithms and
calculate the total head movement based on different strategies:
o FCFS (First Come First Serve): The disk requests are processed in the order they
arrive.
o SCAN: The disk head moves in one direction, serving requests in that direction, and
then reverses its direction after reaching the end.
o C-SCAN: The disk head moves in one direction, but once it reaches the end, it jumps
to the other end of the disk and continues servicing requests in the same direction.
• Example: Design a program where the user provides disk requests and the initial head
position, and the program calculates the total head movement for all three scheduling
algorithms.
4. Algorithm / Pseudocode
• Purpose: To outline the logical steps for each of the disk scheduling algorithms.
FCFS Algorithm
• Create a queue for the disk requests.
• Process the requests in the order they are received.
• Calculate the total head movement by finding the distance between each request and the
current head position.
• Pseudocode:
o Initialize the head position and request queue.
o For each request, calculate the head movement from the current position and add it
to the total movement.
o Update the current position to the request's position after each service.
SCAN Algorithm
• Move the head in one direction, servicing requests in that direction.
• Once the head reaches the end, reverse direction and continue serving requests.
• Pseudocode:
o Sort the request queue in ascending order.
o Move the head towards the highest request, servicing all requests in that direction.
o Once the highest request is processed, reverse direction and serve the remaining
requests.
C-SCAN Algorithm
• Similar to SCAN, but when the head reaches the end, it jumps back to the beginning to
continue servicing the requests.
• Pseudocode:
o Sort the request queue in ascending order.
o Move the head towards the highest request, servicing all requests in that direction.
o Once the head reaches the end, jump back to the lowest request and continue
servicing.
5. Code Implementation
FCFS Algorithm Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int requestQueue[100], initialHeadPosition, totalHeadMovement = 0, numRequests;
printf("Enter the number of requests: ");
scanf("%d", &numRequests);
printf("Enter the initial head position: ");
scanf("%d", &initialHeadPosition);
printf("Enter the request queue: ");
for (int i = 0; i < numRequests; i++) {
scanf("%d", &requestQueue[i]);
}
int currentPosition = initialHeadPosition;
for (int i = 0; i < numRequests; i++) {
totalHeadMovement += abs(requestQueue[i] - currentPosition);
currentPosition = requestQueue[i];
}
printf("Total head movement: %d\n", totalHeadMovement);
return 0;
}
7. Discussion
• Purpose: The FCFS algorithm serves requests in the order they arrive, which can result in
high total head movement and inefficient disk usage. SCAN and C-SCAN provide better
efficiency by minimizing back-and-forth movements, especially in scenarios where requests
are spread across the disk.
• Example: In a real-world scenario, SCAN and C-SCAN can be compared to elevators moving in
one direction until they reach the top or bottom and then reversing or resetting to continue
servicing requests.
8. Conclusion
• Purpose: We learned how FCFS, SCAN, and C-SCAN work in servicing disk requests, each
algorithm optimizing the head movement in different ways. While FCFS is simpler, SCAN and
C-SCAN tend to be more efficient in terms of total head movement, particularly when disk
requests are unevenly distributed.
• Example: Real-world applications like disk drive management, CPU scheduling, and elevator
systems make use of these algorithms to ensure efficiency in resource management.