Oslf Exp9
Oslf Exp9
9.2 Course Outcome: Understand, implement, and compare the performance of different disk
scheduling algorithms (FCFS, SCAN, and C-SCAN) in C based on seek time and total head movement.
9.4 Requirement:
1 . C compiler (e.g., GCC)
2. Basic understanding of C programming.
3. Knowledge of disk scheduling algorithms.
iskschedulingalgorithmsareusedtodeterminetheorderinwhichdiskI/Orequestsareprocessed.The
D
primary goal is to minimize the total seek time or the total head movement. The common algorithms
include:
F
● CFS (First Come First Serve): Processes requests in the order they arrive.
● SCAN(ElevatorAlgorithm):Movesthediskarmtowardsoneend,servicingrequestsuntil
it reaches the end, then reverses direction.
● C-SCAN (Circular SCAN):SimilartoSCAN,butafterreachingoneend,itreturnsimmediately
to the start without servicing requests on the return trip.
9.6 Procedure:
1 . I nitialize and Input Requests: Get the sequence of disk requests and the initial head position.
2. Implement FCFS: Process requests in the order they arrive and calculate total head movement.
3. Implement SCAN: Move the disk arm towards the nearest end, servicing requests, then reverse.
4. Implement C-SCAN: Move the disk arm in one direction only, jump to the start when the end is
reached.
5. Compare and Output: Calculate and compare the total head movement for each algorithm.
#include <stdio.h>
#include <stdlib.h>
sort(requests, n);
printf("\nSCAN Disk Scheduling:\n");
printf("| Request | Distance | Head Position |\n");
printf("|---------|----------|----------------|\n");
if (direction == 0) { // Left
for (i = n - 1; i >= 0; i--) {
if (requests[i] < head) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
total_head_movement += abs(current_position - 0); // Move to the start
current_position = 0;
for (i = 0; i < n; i++) {
if (requests[i] > 0) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
} else { // Right
for (i = 0; i < n; i++) {
if (requests[i] > head) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
total_head_movement += abs(current_position - 200); // Move to the end (Assuming disk
size is 200)
current_position = 200;
for (i = n - 1; i >= 0; i--) {
if (requests[i] < 200) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
}
printf("|---------|----------|----------------|\n");
printf("Total head movement: %d\n", total_head_movement);
}
void cscan(int requests[], int n, int head, int direction) {
int total_head_movement = 0;
int current_position = head;
int i; // Loop variable
sort(requests, n);
printf("\nC-SCAN Disk Scheduling:\n");
printf("| Request | Distance | Head Position |\n");
printf("|---------|----------|----------------|\n");
if (direction == 0) { // Left
for (i = n - 1; i >= 0; i--) {
if (requests[i] < head) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
total_head_movement += abs(current_position - 0); // Move to the start
current_position = 0;
total_head_movement += abs(current_position - 200); // Jump to end
current_position = 200;
for (i = 0; i < n; i++) {
if (requests[i] > 0) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
} else { // Right
for (i = 0; i < n; i++) {
if (requests[i] > head) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
total_head_movement += abs(current_position - 200); // Move to the end
current_position = 200;
for (i = n - 1; i >= 0; i--) {
if (requests[i] < 200) {
int distance = abs(current_position - requests[i]);
total_head_movement += distance;
current_position = requests[i];
printf("| %5d | %5d | %5d |\n", requests[i], distance, current_position);
}
}
}
printf("|---------|----------|----------------|\n");
printf("Total head movement: %d\n", total_head_movement);
}
int main() {
int choice, n, head, direction;
int requests[100]; // Assuming a maximum of 100 requests
switch (choice) {
case 1:
fcfs(requests, n, head);
break;
case 2:
printf("Enter direction (0 for left, 1 for right): ");
scanf("%d", &direction);
scan(requests, n, head, direction);
break;
case 3:
printf("Enter direction (0 for left, 1 for right): ");
scanf("%d", &direction);
cscan(requests, n, head, direction);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Conclusion : Implementing disk scheduling algorithms such as First-Come, First-Served
(FCFS), SCAN, and C-SCAN in C language provides a practical understanding of how these
algorithms operate and their impact on system performance.
1. FCFS: This algorithm is straightforward, processing requests in the order they arrive.
While it is fair and simple to implement, it can lead to inefficiencies, particularly in
scenarios with varied request locations, resulting in longer average seek times.
2. SCAN: The SCAN algorithm improves upon FCFS by moving the disk arm in one
direction, servicing requests until it reaches the end of the disk, then reversing direction.
This reduces the average seek time compared to FCFS but can still suffer from delays if
requests are clustered at one end.
3. C-SCAN: Circular SCAN enhances SCAN by treating the disk as a circular buffer. After
reaching one end, the disk arm jumps back to the other end without servicing any
requests during this return trip. This method ensures a more uniform wait time for all
requests and minimizes delays for those at the opposite end of the disk.