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

C Scan

This C code implements the C-SCAN disk scheduling algorithm. It takes disk requests as input, sorts them in ascending order, and calculates the total seek time by moving the disk head to service each request from left to right and then right to left, minimizing head movement. It outputs the total seek time calculated using this algorithm.

Uploaded by

lawen94360
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)
77 views2 pages

C Scan

This C code implements the C-SCAN disk scheduling algorithm. It takes disk requests as input, sorts them in ascending order, and calculates the total seek time by moving the disk head to service each request from left to right and then right to left, minimizing head movement. It outputs the total seek time calculated using this algorithm.

Uploaded by

lawen94360
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

#include<stdio.

h>
#include<stdlib.h>

#define DISK_SIZE 200

void c_scan(int *requests, int num_requests, int start_pos) {


int i, j, temp, seek_time = 0, curr_pos = start_pos;
int visited[num_requests];
int left = 0, right = DISK_SIZE - 1;

// Initialize all requests as not visited


for (i = 0; i < num_requests; i++) {
visited[i] = 0;
}

// Sort requests in ascending order


for (i = 0; i < num_requests - 1; i++) {
for (j = i + 1; j < num_requests; j++) {
if (requests[i] > requests[j]) {
temp = requests[i];
requests[i] = requests[j];
requests[j] = temp;
}
}
}

// Move disk head from start position towards right end


for (i = start_pos; i <= right; i++) {
for (j = 0; j < num_requests; j++) {
if (requests[j] == i && visited[j] == 0) {
visited[j] = 1;
seek_time += abs(curr_pos - requests[j]);
curr_pos = requests[j];
}
}
}

// Move disk head from right end to left end


seek_time += abs(curr_pos - right);
curr_pos = right;
for (i = right; i >= left; i--) {
for (j = 0; j < num_requests; j++) {
if (requests[j] == i && visited[j] == 0) {
visited[j] = 1;
seek_time += abs(curr_pos - requests[j]);
curr_pos = requests[j];
}
}
}

printf("C-SCAN Total Seek Time: %d\n", seek_time);


}

int main() {
int num_requests, start_pos, i;
int requests[100];

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


scanf("%d", &num_requests);
printf("Enter the start position: ");
scanf("%d", &start_pos);

printf("Enter the requests:\n");


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

c_scan(requests, num_requests, start_pos);

return 0;
}

You might also like