0% found this document useful (0 votes)
17 views5 pages

Look and Clook

The document provides C implementations for the LOOK, CLOOK, and Shortest Seek Time First (SSTF) disk scheduling algorithms. It includes functions for sorting requests, servicing them based on the chosen algorithm, and calculating the total seek time. The main function allows user input for disk requests, initial head position, and algorithm selection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Look and Clook

The document provides C implementations for the LOOK, CLOOK, and Shortest Seek Time First (SSTF) disk scheduling algorithms. It includes functions for sorting requests, servicing them based on the chosen algorithm, and calculating the total seek time. The main function allows user input for disk requests, initial head position, and algorithm selection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C implementation of the LOOK and CLOOK disk

scheduling algorithms:

#include <stdio.h>
#include <stdlib.h>

// Function to sort the requests in ascending order


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

// LOOK Disk Scheduling Algorithm


void look(int requests[], int n, int head, int direction) {
int seek_count = 0;
int current_track;
int left[n], right[n];
int left_count = 0, right_count = 0;

// Segregating requests based on head position


for (int i = 0; i < n; i++) {
if (requests[i] < head) {
left[left_count++] = requests[i];
} else {
right[right_count++] = requests[i];
}
}

// Sorting left and right arrays


sort(left, left_count);
sort(right, right_count);

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

// Servicing requests based on direction


if (direction == 1) { // Moving right
for (int i = 0; i < right_count; i++) {
current_track = right[i];
seek_count += abs(current_track - head);
head = current_track;
printf("Track %d, Seek %d\n", current_track, seek_count);
}
for (int i = left_count - 1; i >= 0; i--) {
current_track = left[i];
seek_count += abs(current_track - head);
head = current_track;
printf("Track %d, Seek %d\n", current_track, seek_count);
}
} else { // Moving left
for (int i = left_count - 1; i >= 0; i--) {
current_track = left[i];
seek_count += abs(current_track - head);
head = current_track;
printf("Track %d, Seek %d\n", current_track, seek_count);
}
for (int i = 0; i < right_count; i++) {
current_track = right[i];
seek_count += abs(current_track - head);
head = current_track;
printf("Track %d, Seek %d\n", current_track, seek_count);
}
}
printf("Total Seek Count: %d\n", seek_count);
}

// CLOOK Disk Scheduling Algorithm


void clook(int requests[], int n, int head) {
int seek_count = 0;
int current_track;
int left[n], right[n];
int left_count = 0, right_count = 0;

// Segregating requests based on head position


for (int i = 0; i < n; i++) {
if (requests[i] < head) {
left[left_count++] = requests[i];
} else {
right[right_count++] = requests[i];
}
}

// Sorting left and right arrays


sort(left, left_count);
sort(right, right_count);

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

// Servicing requests in circular manner


for (int i = 0; i < right_count; i++) {
current_track = right[i];
seek_count += abs(current_track - head);
head = current_track;
printf("Track %d, Seek %d\n", current_track, seek_count);
}
for (int i = 0; i < left_count; i++) {
current_track = left[i];
seek_count += abs(current_track - head);
head = current_track;
printf("Track %d, Seek %d\n", current_track, seek_count);
}

printf("Total Seek Count: %d\n", seek_count);


}

int main() {
int n, head, direction, choice;

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


scanf("%d", &n);
int requests[n];

printf("Enter the disk request sequence:\n");


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

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


scanf("%d", &head);

printf("Enter the initial direction (1 for right, 0 for left): ");


scanf("%d", &direction);

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


printf("1. LOOK\n");
printf("2. CLOOK\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
look(requests, n, head, direction);
break;
case 2:
clook(requests, n, head);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}

C program that implements the Shortest Seek Time First


(SSTF) disk scheduling algorithm:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
int requests[20], n, head, total_seek_time = 0;
int completed[20] = {0};

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


scanf("%d", &n);

printf("Enter the disk request sequence: ");


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

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


scanf("%d", &head);

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


int min_distance = INT_MAX, next_index = -1;
for (int j = 0; j < n; j++) {
if (!completed[j]) {
int distance = abs(requests[j] - head);
if (distance < min_distance) {
min_distance = distance;
next_index = j;
}
}
}
if (next_index != -1) {
total_seek_time += min_distance;
head = requests[next_index];
completed[next_index] = 1;
}
}

printf("Total seek time: %d\n", total_seek_time);


printf("Average seek time: %.2f\n", (float)total_seek_time / n);

return 0;
}

You might also like