0% found this document useful (0 votes)
10 views9 pages

Oslf Exp9

Uploaded by

Smitt Dandekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Oslf Exp9

Uploaded by

Smitt Dandekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

‭Experiment No: 9‬

‭Perform disk scheduling - FCFS, SCAN, C-SCAN using C language.‬

‭9.1 Aim:‬‭Perform disk scheduling - FCFS, SCAN, C-SCAN using C language‬

‭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.3‬ ‭Learning‬ ‭Objectives:‬ ‭Learn‬‭to‬‭implement‬‭FCFS,‬‭SCAN,‬‭and‬‭C-SCAN‬‭algorithms,‬‭analyze‬‭their‬


‭performance, and understand their impact on system performance.‬

‭9.4 Requirement:‬
1‭ .‬ C ‭ compiler (e.g., GCC)‬
‭2.‬ ‭Basic understanding of C programming.‬
‭3.‬ ‭Knowledge of disk scheduling algorithms.‬

‭9.5 Related Theory‬‭:‬

‭ isk‬‭scheduling‬‭algorithms‬‭are‬‭used‬‭to‬‭determine‬‭the‬‭order‬‭in‬‭which‬‭disk‬‭I/O‬‭requests‬‭are‬‭processed.‬‭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‬‭(Elevator‬‭Algorithm)‬‭:‬‭Moves‬‭the‬‭disk‬‭arm‬‭towards‬‭one‬‭end,‬‭servicing‬‭requests‬‭until‬
‭it reaches the end, then reverses direction.‬
‭●‬ ‭C-SCAN‬ ‭(Circular‬ ‭SCAN)‬‭:‬‭Similar‬‭to‬‭SCAN,‬‭but‬‭after‬‭reaching‬‭one‬‭end,‬‭it‬‭returns‬‭immediately‬
‭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>

void fcfs(int requests[], int n, int head) {


int total_head_movement = 0;
int current_position = head;
int i; // Loop variable

printf("\nFCFS Disk Scheduling:\n");


printf("| Request | Distance | Head Position |\n");
printf("|---------|----------|----------------|\n");

for (i = 0; i < n; i++) {


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 sort(int arr[], int n) {


int i, j, temp; // Loop variables
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

void scan(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("\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

printf("Enter the number of disk requests: ");


scanf("%d", &n);

printf("Enter the disk requests: ");


for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}

printf("Enter the initial head position: ");


scanf("%d", &head);

printf("Choose the scheduling algorithm:\n");


printf("1. FCFS\n2. SCAN\n3. C-SCAN\n");
scanf("%d", &choice);

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.

You might also like