0% found this document useful (0 votes)
1 views2 pages

Os 11

The document contains a C program that implements three disk scheduling algorithms: FCFS (First-Come, First-Served), SCAN, and C-SCAN (Circular SCAN). Each algorithm calculates the total seek count and average seek count based on user-provided disk request positions and the initial position of the disk head. The program prompts the user for the total number of disk locations, the initial disk head position, and the number of disk requests before executing the algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Os 11

The document contains a C program that implements three disk scheduling algorithms: FCFS (First-Come, First-Served), SCAN, and C-SCAN (Circular SCAN). Each algorithm calculates the total seek count and average seek count based on user-provided disk request positions and the initial position of the disk head. The program prompts the user for the total number of disk locations, the initial disk head position, and the number of disk requests before executing the algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#define MAX 100
void FCFS(int requests[], int n, int start) { int seek_count = 0; int distance,
cur_track;
for (int i = 0; i < n; i++) {
cur_track = requests[i];
distance = abs(cur_track - start);
seek_count += distance;
start = cur_track;
}
printf("Total Seek Count for FCFS: %d\n", seek_count);
printf("Average Seek Count for FCFS: %.2f\n", (float)seek_count / n);
}
void SCAN(int requests[], int n, int start, int total_tracks) { int seek_count = 0;
int left[MAX],
right[MAX]; int l = 0, r = 0;
for (int i = 0; i < n; i++) {
if (requests[i] < start) {
left[l++] = requests[i];
} else {
right[r++] = requests[i];
}
}
for (int i = 0; i < l - 1; i++) {
for (int j = i + 1; j < l; j++) {
if (left[i] > left[j]) {
int temp = left[i];
left[i] = left[j];
left[j] = temp;
}
}
}
for (int i = 0; i < r - 1; i++) {
for (int j = i + 1; j < r; j++) {
if (right[i] > right[j]) {
int temp = right[i];
right[i] = right[j];
right[j] = temp;
}
}
}
for (int i = l - 1; i >= 0; i--) {
int cur_track = left[i];
seek_count += abs(cur_track - start);
start = cur_track;
}
for (int i = 0; i < r; i++) {
int cur_track = right[i];
seek_count += abs(cur_track - start);
start = cur_track;
}
printf("Total Seek Count for SCAN: %d\n", seek_count);
printf("Average Seek Count for SCAN: %.2f\n", (float)seek_count / n);
}
void CSCAN(int requests[], int n, int start, int total_tracks) { int seek_count =
0; int left[MAX],
right[MAX]; int l = 0, r = 0;
for (int i = 0; i < n; i++) {
if (requests[i] < start) {
left[l++] = requests[i];
} else {
}
for (int i = 0; i < l - 1; i++) {
right[r++] = requests[i];
}
for (int j = i + 1; j < l; j++) {
if (left[i] > left[j]) {
int temp = left[i];
left[i] = left[j];
left[j] = temp;
}
}
}
for (int i = 0; i < r - 1; i++) {
for (int j = i + 1; j < r; j++) {
if (right[i] > right[j]) {
int temp = right[i];
right[i] = right[j];
right[j] = temp;
}
}
}
for (int i = 0; i < r; i++) {
int cur_track = right[i];
seek_count += abs(cur_track - start);
start = cur_track;
}
seek_count += abs(total_tracks - 1 - start);
start = 0;
for (int i = l - 1; i >= 0; i--) {
int cur_track = left[i];
seek_count += abs(cur_track - start);
start = cur_track;
}
printf("Total Seek Count for C-SCAN: %d\n", seek_count);
printf("Average Seek Count for C-SCAN: %.2f\n", (float)seek_count / n);
}
int main() { int requests[MAX], n, start, total_tracks;
printf("Enter the total number of disk locations (total tracks): ");
scanf("%d", &total_tracks);
printf("Enter the initial position of the disk head: ");
scanf("%d", &start);
printf("Enter the number of disk requests: ");
scanf("%d", &n);
printf("Enter the disk request positions: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
FCFS(requests, n, start);
SCAN(requests, n, start, total_tracks);
CSCAN(requests, n, start, total_tracks);
return 0;
}

You might also like